1
+ from datetime import datetime
1
2
import grpc
3
+ import random
2
4
import time
3
5
4
6
from locust import User , TaskSet , between , events , task
7
9
import demo_bank_pb2_grpc
8
10
9
11
10
- def init_tracer ():
11
- config = Config (
12
- config = {
13
- 'sampler' : {
14
- 'type' : 'const' ,
15
- 'param' : 1 ,
16
- },
17
- 'logging' : True ,
18
- 'propagation' : 'b3' , # zipkin headers
19
- },
20
- validate = True ,
21
- service_name = 'load-generator' )
22
- tracer = config .initialize_tracer ()
23
- interceptor = open_tracing_client_interceptor (tracer , log_payloads = True )
24
- return tracer , interceptor
12
+ class DataPool :
13
+ def __init__ (self , file ):
14
+ with open (file , 'r' ) as f :
15
+ self .id_pool = [line .rstrip () for line in f .readlines ()]
16
+ self .pool_size = len (self .id_pool )
17
+
18
+ random .seed (int (datetime .today ().timestamp () * 100 ))
19
+
20
+ def next_id (self ):
21
+ i = random .randrange (0 , self .pool_size - 1 , 1 )
22
+ return self .id_pool [i ]
23
+
24
+
25
+ pool = DataPool ('ids.txt' )
25
26
26
27
27
28
class DashboardTask (TaskSet ):
@@ -33,7 +34,7 @@ def on_start(self):
33
34
def get_dashboard (self ):
34
35
start_time = time .time ()
35
36
try :
36
- test_id = self . get_test_id ()
37
+ test_id = pool . next_id ()
37
38
req = demo_bank_pb2 .GetDashboardRequest (login_name = test_id )
38
39
self .stub .GetDashboard (req )
39
40
except grpc .RpcError as e :
@@ -54,19 +55,20 @@ def get_dashboard(self):
54
55
response_length = 0 ,
55
56
)
56
57
57
- def get_test_id (self ):
58
- return 'user001'
59
-
60
58
61
59
class MobileAppUser (User ):
62
60
tasks = [DashboardTask ]
63
61
wait_time = between (1 , 2 )
64
62
65
63
def __init__ (self , * args , ** kwargs ):
66
64
super (MobileAppUser , self ).__init__ (* args , ** kwargs )
65
+ DataPool ('ids.txt' )
67
66
68
67
69
68
if __name__ == '__main__' :
69
+ for i in range (100 ):
70
+ print (pool .next_id ())
71
+
70
72
print ('''
71
73
This script does not work as a standalone program, please use locust utility instead.
72
74
0 commit comments