forked from loop-godong/loopchain
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloadtool.py
More file actions
182 lines (148 loc) · 5.68 KB
/
loadtool.py
File metadata and controls
182 lines (148 loc) · 5.68 KB
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright 2017 theloop, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import sys
import logging
import getopt
import timeit
import grpc
import json
import loopchain.utils as util
from loopchain import configure as conf
from multiprocessing import Pool, current_process
sys.path.append("loopchain/protos")
import loopchain_pb2, loopchain_pb2_grpc
def client_process(argv):
peer_target = argv[0]
duration = argv[1]
method_name = argv[2]
count_tx = 0
process_id = str(current_process())
print("client process id: " + process_id)
print("duration: " + str(duration))
print("method_name: " + method_name)
channel = grpc.insecure_channel(peer_target)
peer_stub = loopchain_pb2_grpc.PeerServiceStub(channel)
method = getattr(peer_stub, method_name)
if method_name == "CreateTx":
param = loopchain_pb2.CreateTxRequest(data="TEST transaction data by loadtool")
elif method_name == "Query":
query = {
'param1': 1234,
'param2': 'this is just sample',
'param3': [
{'date': '2015-03-11', 'item': 'iPhone'},
{'date': '2016-02-23', 'item': 'Monitor'},
]
}
json_string = json.dumps(query)
param = loopchain_pb2.QueryRequest(params=json_string)
else:
print(method_name + " is not suitable name")
return
start_time = timeit.default_timer()
while duration > (timeit.default_timer() - start_time):
# print(process_id + " try: " + peer_target)
method(param, conf.GRPC_TIMEOUT)
count_tx += 1
duration_real = (timeit.default_timer() - start_time)
# 테스트 측정을 위한 수행 시간 외의 프로그램 수행 시간을 보정한다.
ops_times = count_tx
start_time_ops = timeit.default_timer()
while ops_times > 0:
# 무조건 성공하는 조건문, while duration > (timeit.default_timer() - start_time): 의 실행 시간 대응
if 0 < (timeit.default_timer() - start_time):
ops_times -= 1 # count_tx += 1 실행 시간 대응
duration_other_ops = timeit.default_timer() - start_time_ops
print("duration_other_ops: " + str(duration_other_ops))
duration_real -= duration_other_ops
print("duration_real: " + str(duration_real))
return count_tx, duration_real
def log_result(result):
print("log_result: " + str(result))
duration_total = 0
total_tx = 0
for count_tx, duration in result:
total_tx += count_tx
duration_total += duration
duration_average = duration_total / float(len(result))
tps_tx = float(total_tx) / duration_average
# print("total_tx: " + str(total_tx))
print(str(total_tx), ' transactions are created.')
print('Creating TXs duration: ', duration_average)
print('TPS to create Txs: ', tps_tx, 'number of transaction / sec')
def main(argv):
logging.info("loadtest tool got argv(list): " + str(argv))
try:
opts, args = getopt.getopt(argv, "c:p:m:w:t:hd",
["client=",
"peer=",
"mins=",
"help",
"debug"
])
except getopt.GetoptError as e:
logging.error(e)
usage()
sys.exit(1)
# default option values
client = 1
peer = "127.0.0.1:7100"
duration = 10 # seconds, but param is mins
test_way = 0
# apply option values
for opt, arg in opts:
if (opt == "-c") or (opt == "--client"):
client = int(arg)
elif (opt == "-p") or (opt == "--peer"):
peer = arg
elif (opt == "-m") or (opt == "--mins"):
duration = int(arg) * 60 # seconds, but param is mins
elif opt == "-d":
util.set_log_level_debug()
elif opt == "-t":
test_way = int(arg)
elif (opt == "-h") or (opt == "--help"):
usage()
return
method_name = ["CreateTx", "Query"][test_way]
# run peer service with parameters
logging.info("\nTry load test with: \nclient( " +
str(client) + " ) \npeer( " +
peer + " ) \nduration( " +
str(duration) + " seconds )\n")
pool = Pool(processes=client)
pool.map_async(client_process, (client*((peer, duration, method_name),)), callback=log_result)
logging.debug("wait for duration")
pool.close()
pool.join()
def usage():
print("\n====================================")
print("USAGE: LoopChain Load Test Tool")
print("python3 loadtool.py [option] [value] ...")
print("------------------------------------")
print("\noption list")
print("------------------------------------")
print("-d : set logging level to debug")
print("-c or --client : num of dummy client")
print("-p or --peer : peer target info (\"[IP]:[port]\")")
print("-m or --mins : test duration (mins)")
print("-t : test method (0:CreateTx, 1:Query)")
print("-h or --help : show usage\n")
if __name__ == "__main__":
if len(sys.argv) < 2:
usage()
else:
main(sys.argv[1:])