profile.py 8.65 KB
Newer Older
1
2
3
4
#!/usr/bin/env python

import subprocess as sp
import os
5
import shutil
Steve Tonneau's avatar
Steve Tonneau committed
6
import datetime
7

8
#~ scenarios = ['standing_hrp2']
9
scenarios = ['ground_crouch_hyq_hole']
10
#~ scenarios = ['stair_bauzil_hrp2']
11
n_trials = 1
12

13
14
15
stats = ['balance','collision','ik']
stats_optim = ['time_cwc','com_traj']
allstats = stats + stats_optim
16
17
18
19
20
21

python_script_extension = "_interp.py"
analysis = {}
lower = -100000
higher = 100000000

22
23
24
25
log_dir = "./logs"
if not os.path.exists(log_dir):
	os.makedirs(log_dir)

26
def avg(l):
Steve Tonneau's avatar
Steve Tonneau committed
27
28
	if(len(l) == 0):
		return -1
29
30
31
	return reduce(lambda x, y: x + y, l) / len(l)


Steve Tonneau's avatar
Steve Tonneau committed
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
def initdata(name, data):
	n = name
	data[n+'_min'] = higher
	data[n+'_max'] = lower
	data[n] = []
	data['total_'+n+'_min'] = higher
	data['total_'+n+'_max'] = lower
	data['total_'+n] = []
	data['minnum'+n] = higher
	data['maxnum'+n] = lower
	data['num'+n] = []

def parseLine(sep, name, line, data):
	if sep == 1:
		_1, _min, _avg, _max, _totaltime, _numiter = line.split()
	elif sep ==2:
		_1, _2, _min, _avg, _max, _totaltime, _numiter = line.split()
	data[name+'_min'] = min(data[name+'_min'],float(_min))
	data[name+'_max'] = max(data[name+'_max'],float(_max))
	data['minnum'+name] = min(data['minnum'+name],float(_numiter))
	data['maxnum'+name] = max(data['maxnum'+name],float(_numiter))		
	data['num'+name].append(float(_numiter))
	for i in range(0,int(_numiter)):				
		data[name].append(float(_avg))
	total_time = float(_totaltime)
	data['total_'+name+'_min'] = min(data['total_'+name+'_min'],total_time)
	data['total_'+name+'_max'] = max(data['total_'+name+'_max'],total_time)
	data['total_'+name].append(total_time)
	
61
62
def parseData(scenario):
	filename = scenario+"_log.txt";
63
64
	os.rename("log.txt", filename)	
	shutil.move("./"+filename, log_dir+"/"+filename)
65
66
67
68
69
70
71
72
73
74
75
	analysis[scenario] = {}
	data = analysis[scenario]
	data['no contact'] = 0
	data['contact'] = 0	
	data['unstable contact'] = 0	
	data['mingen_time'] = higher
	data['maxgen_time'] = lower
	data['gen_time'] = []
	data['failed'] = 0
	data['success'] = 0
	data['path_time'] = []
Steve Tonneau's avatar
Steve Tonneau committed
76
77
	data['min_path_time'] = higher
	data['max_path_time'] = lower
78
79
80
81
82
83
84
	#cwc data
	data["optim_error_optim_fail"] = 0
	data[  "optim_error_com_proj"] = 0
	data[   "optim_error_unknown"] = 0
	data[     "optim_num_success"] = 0
	data[      "optim_num_trials"] = 0
	data[            "num_errors"] = 0
85
	for stat in allstats:
Steve Tonneau's avatar
Steve Tonneau committed
86
		initdata(stat, data)	
87
	file = open(log_dir+"/"+filename,"r+");
88
89
90
91
92
93
94
	for line in file.readlines():
		if not (line.find('complete generation') == -1):
			time = float(line.split()[3])
			data['mingen_time'] = min(data['mingen_time'],float(time))
			data['maxgen_time'] = max(data['maxgen_time'],float(time))
			data['gen_time'].append(time);
		elif not (line.find('test balance') == -1):
Steve Tonneau's avatar
Steve Tonneau committed
95
96
97
98
99
			parseLine(2, 'balance', line, data)
		elif not (line.find('collision') == -1):
			parseLine(1, 'collision', line, data)
		elif not (line.find('ik') == -1):
			parseLine(1, 'ik', line, data)
100
101
102
103
104
105
106
107
108
109
110
		elif not (line.find('no contact:') == -1):
			data['no contact'] = data['no contact'] + float(line.rstrip("\n").split()[2]);
		elif not (line.find('unstable contact:') == -1):
			data['unstable contact'] = data['unstable contact'] + float(line.rstrip("\n").split()[2]);			
		elif not (line.find('contact:') == -1):
			data['contact'] = data['contact'] + float(line.rstrip("\n").split()[1]);				
		elif not (line.find('planner failed:') == -1):
			data['failed'] = data['failed'] + 1;				
		elif not (line.find('planner succeeded') == -1):
			data['success'] = data['success'] + 1;		
		elif not (line.find('path computation') == -1):
Steve Tonneau's avatar
Steve Tonneau committed
111
112
113
114
			time = float(line.rstrip("\n").split()[2])
			data['path_time'].append(time);
			data['min_path_time'] = min(data['min_path_time'],time)
			data['max_path_time'] = max(data['max_path_time'],time)
115
116
		#now, analyzing optimization
		elif not (line.find('optim_error_optim_fail') == -1):
117
			val = float(line.rstrip("\n").split()[1])
118
119
			data['optim_error_optim_fail']+= val;
		elif not (line.find('optim_error_com_proj') == -1):
120
			val = float(line.rstrip("\n").split()[1])
121
122
			data['optim_error_com_proj']+= val;
		elif not (line.find('optim_error_unknown') == -1):
123
			val = float(line.rstrip("\n").split()[1])
124
125
			data['optim_error_unknown']+= val;
		elif not (line.find('optim_num_success') == -1):
126
			val = float(line.rstrip("\n").split()[1])
127
128
			data['optim_num_success']+= val;
		elif not (line.find('optim_num_trials') == -1):
129
			val = float(line.rstrip("\n").split()[1])
130
131
			data['optim_num_trials']+= val;
		elif not (line.find('num_errors') == -1):
132
			val = float(line.rstrip("\n").split()[1])
133
			data['num_errors']+= val;
134
135
		elif not (line.find('time_cwc') == -1):
			parseLine(1, 'time_cwc', line, data)
136
137
		elif not (line.find('com_traj') == -1):
			parseLine(1, 'com_traj', line, data)
Steve Tonneau's avatar
Steve Tonneau committed
138

139

Steve Tonneau's avatar
Steve Tonneau committed
140
141
142
143
144
145
146
147
148
149
150
151
def printOneStat(f, name, data, g_time, tTime):
	n = name
	d = data
	print(n, len(d[n]))
	d[n] = avg(d[n])
	d['num'+n] = avg(d['num'+n])
	d['total_'+n] = avg(d['total_'+n])
	f.write (n +" tests: \n")
	f.write ("\t single operation time (min / avg / max):\n \t " + str(d[n+'_min']) +  "\t" + str(d[n]) + "\t" + str(d[n+'_max']) + "\t \n")
	f.write ("\t total time (min / avg / max / % of total / % of total wtht path planning):\n \t " + str(d['total_'+n+'_min']) +  "\t" + str(d['total_'+n]) + "\t" + str(d['total_'+n+'_max']) + "\t" +  str(float(d['total_'+n]) / tTime * 100) + "%" + "\t" +  str(float(d['total_'+n]) / g_time * 100) + "%\n")
	f.write ("\t number of tests (min / avg / max):\n \t " + str(d['minnum'+n]) +  "\t" + str(d['num'+n]) + "\t" + str(d['maxnum'+n]) + "\n")
		
152
153
154
155
156
157
158
159
160
161
162
163
def printOneStatOptim(f, name, data):
	n = name
	d = data
	print(n, len(d[n]))
	d[n] = avg(d[n])
	d['num'+n] = avg(d['num'+n])
	d['total_'+n] = avg(d['total_'+n])
	f.write (n +" tests: \n")
	f.write ("\t single operation time (min / avg / max):\n \t " + str(d[n+'_min']) +  "\t" + str(d[n]) + "\t" + str(d[n+'_max']) + "\t \n")
	f.write ("\t total time (min / avg / max ):\n \t " + str(d['total_'+n+'_min']) +  "\t" + str(d['total_'+n]) + "\t" + str(d['total_'+n+'_max']) + "\t"  + "%\n")
	f.write ("\t number of tests (min / avg / max):\n \t " + str(d['minnum'+n]) +  "\t" + str(d['num'+n]) + "\t" + str(d['maxnum'+n]) + "\n")
	
Steve Tonneau's avatar
Steve Tonneau committed
164
		
165
def analyzeData():
166
	f = open(log_dir+"/log_"+str(datetime.datetime.now())+".txt","w+")
Steve Tonneau's avatar
Steve Tonneau committed
167
		
168
	for scenario in scenarios:
Steve Tonneau's avatar
Steve Tonneau committed
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
		d = analysis[scenario]
		d['path_time'] = avg(d['path_time'])
		d['gen_time'] = avg(d['gen_time'])
		g_time = d['gen_time']
		tTime = d['gen_time'] + d['path_time']
		f.write (scenario +":\n")
		
		f.write ("total computation time (min / avg / max): " + str(d['mingen_time'] + d['min_path_time']) + "\t" + str(tTime) + "\t" + str(d['maxgen_time'] + d['max_path_time']) + "\n")
		
		f.write ("generation time (without RRT) (min / avg / max / % of total):\n \t " + str(d['mingen_time']) + "\t" + str(d['gen_time']) + "\t" + str(d['maxgen_time']) + "\t"+ str(float(d['gen_time']) / tTime * 100) + "%\n")
		
		f.write ("path computation: \n")
		f.write ("\t time (min / avg / max / % of total):\n \t " + str(d['min_path_time']) +  "\t" + str(d['path_time']) + "\t" + str(d['max_path_time']) + "\t" +  str(float(d['path_time']) / tTime * 100) + "%\n")
		
		for stat in stats:
			printOneStat(f, stat, d, g_time, tTime)
			
		f.write ("number of successes / failures / ratio: " + str(d['success']) + "\t" + str(d['failed']) + "\t" + str(float(d['success']) / float(d['success']) * 100)  + "%\n")
		
		nc = float(d['no contact'])
		c = float(d['contact'])
		uc = float(d['unstable contact'])
		tc = nc + c + uc 
		f.write ("% of failed contact generation (no candidates found): " + str(nc / tc * 100)  + "%\n")
		f.write ("% of unstable contact generation (no balanced candidates found): " + str(uc / tc * 100)  + "%\n")
194
195
		f.write ("\n")
		
196
197
198
199
200
201
202
203
204
205
		if(d["optim_num_trials"] > 0):
			f.write ("***********************%\n")
			f.write (" optimization related data %\n")
			f.write ("% succes rate of optimization: " + str(d["optim_num_success"] / d["optim_num_trials"] * 100)  + "%\n")
			f.write ("% succes rate of optimization disregarding collision: " + str(d["optim_num_success"] / (d["optim_num_trials"] - d["optim_error_com_proj"]) * 100)  + "%\n")
			if(d["num_errors"] > 0):
				f.write ("% errors due to problem infeasibility (% of errors, % over all trials): " + str(d["optim_error_optim_fail"] / d["num_errors"] * 100) + " " + str(d["optim_error_optim_fail"] / d["optim_num_trials"] * 100)  + "%\n")
				f.write ("% errors due to unknown reasons (% of errors, % over all trials): " + str(d["optim_error_unknown"] / d["num_errors"] * 100) + " " + str(d["optim_error_unknown"] / d["optim_num_trials"] * 100)  + "%\n")
			for stat in stats_optim:
				printOneStatOptim(f, stat, d)
Steve Tonneau's avatar
Steve Tonneau committed
206
207
		f.write ("\n \n \n")
	f.close()
208
209
210
211
	
for scenario in scenarios:
	for i in range(0,n_trials):
		var = scenario+python_script_extension;
Steve Tonneau's avatar
Steve Tonneau committed
212
		sp.check_call(["./profile.sh", str(var)]);
213
	parseData(scenario)
214
analyzeData()