/
tests.py
304 lines (221 loc) · 9.83 KB
/
tests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
'''
Created on May 26, 2016
@author: abhinav
'''
import json
from urllib_utils import get_data
import urllib
import hashlib
def test0():
import cookies
import config
trans_5C = "".join ([chr (x ^ 0x5C) for x in xrange(256)])
trans_36 = "".join ([chr (x ^ 0x36) for x in xrange(256)])
print config.SERVER_SECRET.translate(trans_5C)
auth_key = cookies.create_signed_value(config.SERVER_SECRET , config.SERVER_AUTH_KEY_STRING ,"fuck yo")
print auth_key
return True
def test_decode_key():
import cookies
import config
print cookies.decode_signed_value(config.SERVER_SECRET, config.SERVER_AUTH_KEY_STRING, "cmVzdGF1cmFudDFfX3JlY2VwdGlvbg==|1471796646|69861d7be34901217fffff5db5ae33a61a4c95fc")
def test1():
from websocket._core import create_connection
import json
import cookies
import config
import time
import urllib
import ssl
auth_key_client1 = urllib.quote(cookies.create_signed_value(config.SERVER_SECRET , config.SERVER_AUTH_KEY_STRING ,"abhinav")) #urllib.quote("gRT1avr5ZUaGyLlVkhBeas8QdLx5a4EJCgdhYmhpbmF2")
auth_key_client2 = urllib.quote(cookies.create_signed_value(config.SERVER_SECRET , config.SERVER_AUTH_KEY_STRING ,"sindhu")) #urllib.quote("gRS9lTlvtfZTBsTbVb3XH4c3EkPKwYEICgZzaW5kaHU=")
ws1 = create_connection("ws://localhost:8081/connectV3?auth_key="+auth_key_client1+"&data_type=json" , sslopt={"verify":False, "ca_certs":"/home/abhinav/any/allsparkrt_c/allspark_cert.pem"})
print "created_connection 1"
ws2 = create_connection("ws://localhost:8081/connectV3?auth_key="+auth_key_client2+"&data_type=json",sslopt={"verify":False, "ca_certs":"/home/abhinav/any/allsparkrt_c/allspark_cert.pem"})
print "created_connection 2"
time.sleep(1)
ws1.send(json.dumps({'dest_id':"sindhu", "payload":"Hello world" }))
print ws2.recv()
ws2.send(json.dumps({'dest_id':"abhinav", "payload":"Hello world 2" }))
print ws1.recv()
print ws1.recv()
# ws1.close()
# ws2.close()
def test2():
from websocket._core import create_connection
from bson import json_util
import cookies
from database import Db
import config
import time
db = Db()
db.init()
# this is where user connects to nodes
db.remove_client_nodes("abhinav")
client_node_id1 = db.create_node("abhinav", None, None, None)# client
client_node_id2 = db.create_node("abhinav", None, None , None)# client
print "creating nodes" , client_node_id1, client_node_id2
auth_key_client1 = cookies.create_signed_value(config.SERVER_SECRET , "auth", json_util.dumps({'node_id':client_node_id1}))
auth_key_client2 = cookies.create_signed_value(config.SERVER_SECRET , "auth",json_util.dumps({'node_id':client_node_id2}))
ws1 = create_connection("ws://192.168.1.146:8081/connect?auth_key="+auth_key_client1)
print "created_connection 1"
ws2 = create_connection("ws://192.168.1.146:8083/connect?auth_key="+auth_key_client2)
print "created_connection 2"
time.sleep(1)
ws1.send(json_util.dumps({'dest_id':client_node_id2, "payload":"Hello world" }))
time.sleep(0.1)
print ws2.recv()
time.sleep(0.1)
print ws1.recv()
ws1.send_close()
ws2.send_close()
def load_test_single_server(n=100, k_procs = 1, n_messages=1000):
#make n node_ids
#register n_node_ids
#parallel k_threads each of which send a message from src to dest , with random message ids for one minute
#stop sending after one minute and see if all messages recieved
import gevent
from gevent import monkey
monkey.patch_all(thread=False)
import cookies
import config
import time
from websocket._core import create_connection
from bson import json_util
import json
from time import sleep
import util_funcs
import random
import threading
import urllib
import os
node_ids = json_util.loads(open("node_ids.txt","r").read())
if(not node_ids or len(node_ids)<n):
if(node_ids==None):
node_ids = []
while(len(node_ids)<n):
node_ids.append(util_funcs.get_random_id(10))
open("node_ids.txt","w").write(json_util.dumps(node_ids))
node_connections = {}
def send_message_into_network(src_id, dest_id):
message = {"id":util_funcs.get_random_id(10), "src_id": src_id , "dest_id":dest_id, "payload":"same random payload"}
node_connections[src_id].send(json_util.dumps(message))
messages_recieved = []
last_message_recieved = [-1]
def recv_message(ws):
try:
while(True):
message = ws.recv()
messages_recieved.append(True)
last_message_recieved[0] = time.time()
except Exception as ex:
print "reading connection exception" , ex
tasklets = []
#called from threads
def open_connections(node_ids , i , j, id):
print "Spinning a thread ", id, "node_ids from ", i , j
for p in range(i , j):
node_id = node_ids[p]
try:
auth_key = cookies.create_signed_value(config.SERVER_SECRET , config.SERVER_AUTH_KEY_STRING ,node_id)
# ws = create_connection("ws://104.199.129.250:8081/connectV2?auth_key="+auth_key)
ws = create_connection("ws://35.xxxxxxxx/connectV3?auth_key="+auth_key)
#print "created connection", ws
node_connections[node_id] = ws
tasklets.append(gevent.spawn(recv_message , ws))
except Exception as e:
print "exception opening connection"
pass
def close_connections(node_ids , i , j, id):
for p in range(i , j):
node_id= node_ids[p]
node_connections[node_id].close()
process_count = 0
start = time.time()
while(process_count < k_procs):
c = n/k_procs
#each thread spins off new connections which inturn spawn gevent threads
pid = 1#os.fork()
if(pid != 0):
i = c*process_count
j = c*(process_count+1)
open_connections(node_ids, i , j, pid)
# start sending messages onto network
n= 0
while(n<n_messages):
try:
n+=1
src_id = None
dest_id = None
while(src_id==dest_id):
src_id = node_ids[random.randrange(i, j)]
dest_id = node_ids[random.randrange(i, j)]
send_message_into_network(src_id, dest_id)
except Exception as ex:
print "exception sending messages", ex
print "sent ", n , " messages onto network process::",pid, time.time()-start, "seconds"
p = 0
while(p<50 and len(messages_recieved)<n_messages):
gevent.sleep(1)
p+=1
print "messages recieved :: pid:: " , pid , len(messages_recieved), "in", last_message_recieved[0] -start, " seconds "
print " closing connections "
close_connections(node_ids, i , j , pid)
gevent.joinall(tasklets)
break
else:
process_count+=1
'''
#########mac
sudo launchctl limit maxfiles 1000000 1000000
$ sysctl kern.maxfiles
kern.maxfiles: 12288
$ sysctl kern.maxfilesperproc
kern.maxfilesperproc: 10240
$ sudo sysctl -w kern.maxfiles=1048600
kern.maxfiles: 12288 -> 1048600
$ sudo sysctl -w kern.maxfilesperproc=1048576
kern.maxfilesperproc: 10240 -> 1048576
$ sudo sysctl -w kern.ipc.somaxconn=2048
$ ulimit -S -n
256
$ ulimit -S -n 1048576
$ ulimit -S -n
1048576
'''
def mongo_db_test():
from database_mongo import Db
import time
db = Db()
db.init()
current_timestamp = time.time()*1000
db.add_pending_messages("asdasdasd", 1, "{}", current_timestamp)
db.add_pending_messages("asdasdasd", 2, "{}")
time.time()*1000
print db.fetch_inbox_messages("asdasdasd", -1, -1, current_timestamp-100)
def mongo_db_test_1():
# to test fetch node
from database_mongo import Db
import time
db = Db()
db.init(user_name="abhinav", password="$samosa$", host="104.199.129.250", namespace="samosa_messaging_v2")
# print db.fetch_inbox_messages("df44c88cdaa36e4de1c5bf9c36968078", -1, -1, 1468524512476)
#
db.update_node_info("g3cK4ljIyh1468240301", num_connections=1650, num_max_connections=1500)
print db.get_a_connection_node()
def send_push_routerapp():
import cookies
import config
auth_key = cookies.create_signed_value(config.SERVER_SECRET , config.SERVER_AUTH_KEY_STRING , "krDJDQIM14WgzGl")
print get_data("http://192.168.1.109:8081/push_message?auth_key="+auth_key+"&to_nodes="+json.dumps(["sindhu"]), post=json.dumps({"src_id": "abhinav", "timestamp": 1475238418310, "payload2": "", "payload1": "1475234654.001990079879761-QCyXLnaNtT__147536164876", "type": 4, "payload": "1475234654.001990079879761-QCyXLnaNtT"}), headers={"Connection":"keep-alive"}).read()
#print get_data("http://rt.getrouterapp.com/create_session?auth_key="+auth_key, post=urllib.urlencode({"_session_info_": json.dumps(['abhinav_sindhu', [['147536164876'], ['319815472031']] ])})).read()
if __name__ == "__main__":
load_test_single_server()
#test2()
#mongo_db_test()
#mongo_db_test_1()
# test1()
# send_push_routerapp()
# send_push_routerapp()
# test_decode_key()
#mongo_db_test_1()