-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-script.py
More file actions
169 lines (129 loc) · 4.83 KB
/
test-script.py
File metadata and controls
169 lines (129 loc) · 4.83 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
import os
import json
import time
import requests
test_prepare_content = True
PORT = "9999"
post_url = "http://localhost:" + PORT + "/project/5620bece05509b0a7a3cbc61/doc/111122223330"
get_url = "http://localhost:" + PORT + "/project/5620bece05509b0a7a3cbc61/doc/111122223330"
headers = {'content-type': 'application/json'}
begin_with_large_file = False
test_numbers = 500
initial_file_lines = 100000
def request_get(url, param):
fails = 0
text = ""
while True:
try:
if fails >= 20:
break
ret = requests.get(url=url, params=param, timeout=10)
if ret.status_code == 200:
text = json.loads(ret.text)
else:
continue
except:
fails += 1
print('fails: ', fails)
else:
break
return text
def request_post(url, param):
# print("request_post")
fails = 0
text = ""
while True:
try:
if fails >= 20:
break
headers = {'content-type': 'application/json'}
ret = requests.post(url, json=param, headers=headers, timeout=10)
text = json.loads(ret.text)
if ret.status_code == 200:
text = json.loads(ret.text)
else:
continue
except:
fails += 1
print('fails: ', fails)
else:
break
return text
get_count = 0
get_time_cost = 0.0
post_count = 0
post_time_cost = 0.0
# in order to fit the format of shell curl command, does not use the str() in python, uses custom function instead.
def list_to_str(l):
ret = "["
list_size = len(l)
for i in range(list_size):
ret += "\"" + str(l[i]) + "\""
if i+1 != list_size:
ret += ", "
ret += "]"
return ret
# prepare original_file and write to storage
if test_prepare_content:
file = open("original_file", "w")
file.write('{"lines": ["')
for i in range(2):
file.write("")
file.write('"]}')
file.close()
# os.system("curl -X POST -H 'Content-Type: application/json' -d '@original_file' http://localhost:" + PORT + "/project/5620bece05509b0a7a3cbc61/doc/111122223330")
initial_content = '1234'
if begin_with_large_file:
for i in range(initial_file_lines):
initial_content += ">>>> large file <<<<"
request_param = {'lines': [initial_content]}
# ret = requests.post(post_url, json=request_param)
ret = requests.post(post_url, json=request_param, headers=headers, timeout=10)
# print("ret ", ret.status_code)
for i in range(test_numbers):
if i % 50 == 0:
print("\n\t>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> test round: ", i + 1, ", total round ", test_numbers)
# get previous_file from storage
start_time = time.time()
# get_result = os.popen("curl http://localhost:" + PORT + "/project/5620bece05509b0a7a3cbc61/doc/111122223330")
ret = requests.get(url=get_url, timeout=10)
get_time_cost += time.time() - start_time
get_count += 1
# output = get_result.read()
# print("|" + output + "|")
# print(type(output))
# print("-------------------")
# print(output)
# json_result = json.loads(output)
text = json.loads(ret.text)
lines = text["lines"]
# print(lines)
# generate new file and write to storage
lines.append(">>> newlines <<<")
# print(lines)
request_str = '{"lines": ' + list_to_str(lines) + '}'
# print(request_str)
# new_request = "curl -X POST -H 'Content-Type: application/json' -d " + request_str + " http://localhost:" + PORT + "/project/5620bece05509b0a7a3cbc61/doc/111122223330"
# print("\n" + new_request + "\n")
request_param = json.loads(request_str)
start_time = time.time()
# _ = os.popen("curl -X POST -H 'Content-Type: application/json' -d " + request_str + " http://localhost:" + PORT + "/project/5620bece05509b0a7a3cbc61/doc/111122223330")
ret = requests.post(post_url, json=request_param, headers=headers, timeout=10)
# print("ret ", ret.status_code)
post_time_cost += time.time() - start_time
post_count += 1
# # check the file from storage
# start_time = time.time()
# get_result = os.popen("curl http://localhost:" + PORT + "/project/5620bece05509b0a7a3cbc61/doc/111122223330")
# get_time_cost += time.time() - start_time
# get_count += 1
# print(get_result)
# compute time cost
os.system("sleep 1")
if test_numbers > 0:
print("\n\n\n\n")
print("**************************************************************************************************")
print("total round: ", test_numbers)
print("GET avg time: ", get_time_cost / get_count)
print("POST avg time: ", post_time_cost / post_count)
print("**************************************************************************************************")