-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlatency.py
executable file
·153 lines (125 loc) · 3.86 KB
/
latency.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
#!/usr/bin/python
import argparse
import json
import re
import subprocess
from datetime import datetime
PROJECT = "TO BE FILLED"
ZONE = "TO BE FILLED"
NEG_NAME = "TO BE FILLED"
BM_URL = 'TO BE FILLED'
SEARCH = 'TO BE FILLED'
VMs = {
"VM1": "IP1",
"VM2": "IP2"
}
NEG_ADD_DEL_FORMAT = 'curl -H "$(oauth2l header userinfo.email)" ' \
'https://www.googleapis.com/compute/beta/projects/{}/zones/{}/networkEndpointGroups/{}/{} ' \
'-H "Content-Type:application/json" -d \'{}\''
ATTACH = "attachNetworkEndpoints"
DETACH = "detachNetworkEndpoints"
parser = argparse.ArgumentParser(description='NEG API latency test')
parser.add_argument('--expect-count', metavar='expect_count', type=int, default=10000)
parser.add_argument('--port-start', metavar='port_start', type=int, default=10000)
parser.add_argument('--port-per-vm', metavar='port_per_vm', type=int, default=1000)
parser.add_argument('--vm-num', metavar='vm_num', type=int, default=-1)
group = parser.add_mutually_exclusive_group()
group.add_argument('--attach', action='store_true')
group.add_argument('--detach', action='store_true')
args = parser.parse_args()
print
args.attach
print
args.detach
OPERATION = ""
if args.detach:
OPERATION = DETACH
else:
OPERATION = ATTACH
EXPECT_COUNT = args.expect_count
PORT_START = args.port_start
PORT_NUM = args.port_per_vm
VM_NUM = args.vm_num
print
"OPERATION =", OPERATION
print
"EXPECT_COUNT =", EXPECT_COUNT
print
"PORT_START =", PORT_START
print
"PORT_NUM =", PORT_NUM
print
"VM_NUM =", VM_NUM
print
"TOTAL_ENDPOINT =", len(VMs) * PORT_NUM
def NEGCall(operation, body):
return NEG_ADD_DEL_FORMAT.format(PROJECT, ZONE, NEG_NAME, operation, body)
def attachNE(body):
return NEG_ADD_DEL_FORMAT.format(PROJECT, ZONE, NEG_NAME, ATTACH, body)
def detachNE(body):
return NEG_ADD_DEL_FORMAT.format(PROJECT, ZONE, NEG_NAME, DETACH, body)
class NetworkEndpoint:
def __init__(self, instance, ipAddress, port):
self.instance = instance
self.ipAddress = ipAddress
self.port = port
pass
def genBody(endpoints):
res = ', '.join(endpoints)
return '{"networkEndpoints": [' + res + ']}'
# Generated body
negCalls = []
if __name__ == "__main__":
print
"START"
endpointCount = 0
endpointList = []
curCount = 0
vmCount = 0
for vm, ip in VMs.items():
vmCount += 1
if VM_NUM != -1 and vmCount > VM_NUM:
break
for port in range(PORT_START, PORT_START + PORT_NUM):
endpointList.append(json.dumps(NetworkEndpoint(vm, ip, port).__dict__))
endpointCount += 1
curCount += 1
if curCount >= 500:
negCalls.append(genBody(endpointList))
endpointList = []
curCount = 0
if len(endpointList) > 0:
negCalls.append(genBody(endpointList))
print
"==========Number of calls=========:", len(negCalls)
print
"==========Number of Endpoints=========:", endpointCount
t1 = datetime.now()
for b in negCalls:
subprocess.Popen(NEGCall(OPERATION, b), stderr=subprocess.STDOUT, shell=True)
t2 = datetime.now()
delta = t2 - t1
combined1 = delta.seconds + delta.microseconds / 1E6
while True:
out = subprocess.check_output("gosso --url " + BM_URL, stderr=subprocess.STDOUT, shell=True)
# print out
m = re.search(SEARCH, out)
# print m.group(0)
if m:
found = m.group(1)
print
"Healthy endpoint count:" + found
if int(found) == EXPECT_COUNT:
break
else:
print
"no match"
t3 = datetime.now()
delta = t3 - t2
combined2 = delta.seconds + delta.microseconds / 1E6
print
"NEG API calls issued in: " + str(combined1)
print
"Backend programmed in: " + str(combined2)
print
"END"