-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathperftest.py
More file actions
233 lines (204 loc) · 10.1 KB
/
Copy pathperftest.py
File metadata and controls
233 lines (204 loc) · 10.1 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
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
# encoding: utf-8
# Author: Zhuangwei Kang
import os
import time
import re
from constants import *
import pandas as pd
import subprocess
import multiprocessing
import argparse
executionTime = 120
def build_cmd(role, eid, kwargs, args):
cmd = "./perftest_cpp -executionTime %d -cpu -nic eth0 " % executionTime
if args.noPrint:
cmd += "-noPrint "
if args.transport == 'TCP':
cmd += "-transport TCP "
elif args.transport == 'TLS':
cmd += "-transport TLS "
elif args.transport == 'DTLS':
cmd += '-transport DTLS '
if args.latencyTest:
cmd += "-latencyTest "
if args.sleep != 0:
cmd += '-sleep %d ' % args.sleep
if args.encryptionLevel == 0:
cmd += "-secureGovernanceFile ./resource/secure/signed_PerftestGovernance_RTPSSign.xml "
if args.encryptionLevel == 1:
cmd += "-secureGovernanceFile ./resource/secure/signed_PerftestGovernance_SignEncryptData.xml "
elif args.encryptionLevel == 2:
cmd += '-secureGovernanceFile ./resource/secure/signed_PerftestGovernance_SignEncryptSubmessage.xml '
elif args.encryptionLevel == 3:
cmd += '-secureGovernanceFile ./resource/secure/signed_PerftestGovernance_RTPSSignWithOrigAuthEncryptSubmessage.xml '
elif args.encryptionLevel == 4:
cmd += '-secureGovernanceFile ./resource/secure/signed_PerftestGovernance_RTPSEncrypt.xml '
if args.secureSign:
cmd += '-secureSign '
if args.asynchronous or (kwargs['dataLen'] >= 16384 and args.transport=='DTLS'): # set dds.transport.DTLS.dtls1.parent.message_size_max = 65536
cmd += "-asynchronous "
if role == 'pub':
cmd += '-pub -sendQueueSize %d ' % args.sendQueueSize
if row['numSubscribers'] > 1:
cmd += "-numSubscribers %d " % row['numSubscribers']
if args.peers:
# find all sub IP by pod name
find_subs = "kubectl get pods -o wide | grep sub | awk ' { print $6 } '"
all_subs = subprocess.check_output(find_subs, shell=True).decode().split('\n')[:-1]
for i in range(row['numSubscribers']):
cmd += '-peer %s ' % all_subs[i].strip()
else:
if eid > 0:
cmd += "-numPublishers 1 -sidMultiSubTest %d " % eid
if args.peers:
# find all pub IP by pod name
find_pubs = "kubectl get pods -o wide | grep pub | awk ' { print $6 } '"
all_pubs = subprocess.check_output(find_pubs, shell=True).decode().split('\n')[:-1]
for pub in all_pubs:
if args.transport in ['TCP', 'TLS']:
cmd += '-peer %s:7400 ' % pub
else:
cmd += '-peer %s ' % pub
for key in kwargs:
if type(kwargs[key]) is bool:
if kwargs[key]:
cmd += "-%s " % key
else:
if key == 'pubRate':
# get 2 latency samples every second
cmd += "-%s %s:sleep -latencyCount %s" % (key, str(kwargs[key]), str(int(kwargs[key]/2)))
else:
cmd += "-%s %s " % (key, str(kwargs[key]))
return cmd
def monitor(log_path):
with open(log_path, 'w') as f:
f.write('name,cpu(cores),cpu%,memory(bytes),memory%\n')
while True:
cmd = 'kubectl top nodes --no-headers'
metrics = subprocess.check_output(cmd, shell=True).decode().split('\n')
metrics = [line.split(' ') for line in metrics if len(line)>0]
metrics = list(map(lambda line: [item for item in line if len(item)>0], metrics))
for i in range(len(metrics)):
for j in range(1, len(metrics[i])):
metrics[i][j] = re.sub("[^0-9]", "", metrics[i][j])
metrics = pd.DataFrame(metrics)
metrics.columns = metrics.iloc[0]
metrics = metrics.drop(metrics.index[0])
with open(log_path, 'a') as f:
metrics.to_csv(f, index=None)
time.sleep(5)
def execute(pod, perftest_cmd, test):
retry_count = 0
while retry_count < 5:
k8s_cmd = 'nohup kubectl exec -t %s -- %s > logs/%s/%s.log 2>&1 &' % (pod, perftest_cmd, test, pod)
os.system(k8s_cmd)
while not os.path.exists('logs/%s/%s.log' % (test, pod)):
pass
time.sleep(1)
with open('logs/%s/%s.log' % (test, pod)) as f:
if 'command terminated with exit code' in f.read():
retry_count += 1
else:
break
def execute_baremetal(worker, participant, perftest_cmd, test):
retry_count = 0
perftest_cmd = ' '.join(perftest_cmd.split(' ')[1:]) + ' -qosFile app/perftest_qos_profiles.xml'
cmd = 'nohup ssh %s "app/perftest_cpp %s" > logs/%s/%s.log 2>&1 &' % (worker, perftest_cmd, test, participant)
while retry_count < 5:
os.system(cmd)
while not os.path.exists('logs/%s/%s.log' % (test, participant)):
pass
time.sleep(1)
with open('logs/%s/%s.log' % (test, participant)) as f:
if 'command terminated with exit code' in f.read():
retry_count += 1
else:
break
def process_monitor_data(test):
df = pd.read_csv('logs/%s/metrics.csv' % test)
df_mean = df.groupby(by=['name']).mean().round(2)
df_mean.columns = [n+'(avg)' for n in df.columns[1:]]
df_std = df.groupby(by=['name']).std().round(2)
df_std.columns = [n+'(std)' for n in df.columns[1:]]
df_95 = df.groupby(by=['name']).quantile(.95).round(2)
df_95.columns = [n+'(95%)' for n in df.columns[1:]]
df_90 = df.groupby(by=['name']).quantile(.9).round(2)
df_90.columns = [n+'(90%)' for n in df.columns[1:]]
df_80 = df.groupby(by=['name']).quantile(.8).round(2)
df_80.columns = [n+'(80%)' for n in df.columns[1:]]
df_50 = df.groupby(by=['name']).quantile(.5).round(2)
df_50.columns = [n+'(50%)' for n in df.columns[1:]]
df_max = df.groupby(by=['name']).max().round(2)
df_max.columns = [n+'(max)' for n in df.columns[1:]]
df_min = df.groupby(by=['name']).max().round(2)
df_min.columns = [n+'(min)' for n in df.columns[1:]]
df_mean.join(df_std).join(df_95).join(df_90).join(df_80).join(df_50).join(df_min).join(df_max).reset_index().to_csv('logs/%s/metrics.csv' % test, index=None)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
# Benchmarking parameters
parser.add_argument('--sch', type=str, default='schedule.csv', help='path of schedule file')
parser.add_argument('--fromI', type=int, default=0, help='start test from specified index')
parser.add_argument('--toI', type=int, default=100, help='end test at specified index')
parser.add_argument('--monitor', action='store_true', help="enable node resource monitor")
parser.add_argument('--baremetal', action='store_true', help='run perftest on baremetal')
parser.add_argument('--pubHosts', type=str, default='', help='Publisher Host IP, seperate by comma.')
parser.add_argument('--subHosts', type=str, default='', help='Subscriber Hosts IP, seperate by comma.')
# RTI-Perftest parameters
parser.add_argument('--domain', type=int, default=1, help='DDS domain ID')
parser.add_argument('--latencyTest', action='store_true', help='run latency test')
parser.add_argument('--noPrint', action='store_true', help='don\'t print perftest details')
parser.add_argument('--sendQueueSize', type=int, default=50, help='publisher send queue size')
parser.add_argument('--transport', type=str, choices=['UDP', 'TCP', 'TLS', 'DTLS'], default='UDP', help='the transport protocol will be used in the test')
parser.add_argument('--peers', action='store_true', help='make participants find each other using peer address')
parser.add_argument('--asynchronous', action='store_true', help='use asynchronous DataWriter')
parser.add_argument('--encryptionLevel', choices=[0,1,2,3,4], type=int, help='encryption granularity -- 1. Encrypt topic (user) data | 2. Encrypt RTPS submessages | 3. Encrypt RTPS messages')
parser.add_argument('--secureSign', action='store_true', help='Sign (HMAC) discovery and user data')
parser.add_argument('--sleep', type=int, default=0, help='the length of sleep period between subsequent sends in ms')
args = parser.parse_args()
schedule = pd.read_csv(args.sch)
for i, row in schedule.iterrows():
if i < args.fromI or i > args.toI:
continue
start = time.time()
print('test-%d started' % i)
os.mkdir('logs/test-%d' % i)
# start publisher
perftest_cmd = build_cmd('pub', 0, row.to_dict(), args)
participant = PERFTEST_PUB + '0'
if args.baremetal:
pubHost = args.pubHosts.split(',')[0]
execute_baremetal(pubHost, participant, perftest_cmd, 'test-%d' % i)
else:
execute(participant, perftest_cmd, 'test-%d'%i)
# start subscribers
subHosts = args.subHosts.split(',')
for j in range(row['numSubscribers']):
perftest_cmd = build_cmd('sub', j, row.to_dict(), args)
participant = PERFTEST_SUB + str(j)
if args.baremetal:
execute_baremetal(subHosts[j], participant, perftest_cmd, 'test-%d' % i)
else:
execute(participant, perftest_cmd, 'test-%d'%i)
# start host cpu/memory monitor
if args.monitor:
monitor_proc = multiprocessing.Process(target=monitor, args=('logs/test-%d/metrics.csv' % i,))
monitor_proc.start()
time.sleep(1)
while True:
try:
if args.baremetal:
host0 = args.pubHosts.split(',')[0]
pid = subprocess.check_output('ssh %s "pgrep perftest_cpp"' % host0, shell=True).decode()
if len(pid) == 0:
break
time.sleep(5)
else:
subprocess.check_output('pgrep kubectl', shell=True)
except:
# test finished
break
if args.monitor:
monitor_proc.terminate()
process_monitor_data('test-%d' % i)
print('test-%d end, elapsed time: %ss' % (i, time.time()-start))
print('-------------------------')