-
Notifications
You must be signed in to change notification settings - Fork 0
/
eval_success
96 lines (81 loc) · 2.47 KB
/
eval_success
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
#!/usr/bin/env python
# ------------------------------------------------------------------------------
#
# Evaluate the success rate of a randomized command
#
# Author: Stefan Heule <[email protected]>
#
# ------------------------------------------------------------------------------
import sys
import subprocess
import threading
import time
# ------------------------------------------
# main entry point
# ------------------------------------------
def main():
if len(sys.argv)-1 == 0:
print "usage: "+sys.argv[0]+" [-n max] <command> [args]"
print ""
print "Executes <command> repeatably, and checks how often the command succeeds (based on the return value)."
sys.exit(1)
if sys.argv[1] == "-n":
max_iterations = int(sys.argv[2])
index = 3
else:
max_iterations = 0
index = 1
n = 0
succ = 0
succtime = 0
unsucctime = 0
totaltime = 0
while max_iterations == 0 or n < max_iterations:
start = time.time()
val, output = execute(" ".join(sys.argv[index:]))
end = time.time()
n += 1
if val == 0:
succ += 1
succtime += end - start
else:
unsucctime += end - start
totaltime += end - start
print "Success: %d/%d (%.3f%%)" % (succ, n, float(succ*100)/float(n))
if succ == 0:
succtimeavg = 0
else:
succtimeavg = succtime / float(succ)
if succ == n:
unsucctimeavg = 0
else:
unsucctimeavg = unsucctime / float(n-succ)
print " Average time for success: %.2f seconds" % (succtimeavg)
print " Average time for non-success: %.2f seconds" % (unsucctimeavg)
print " Total time: %.2f seconds" % totaltime
print ""
def execute(cmd):
return Command(cmd).run(None)
# taken from http://stackoverflow.com/questions/1191374/subprocess-with-timeout
class Command(object):
def __init__(self, cmd):
self.cmd = cmd
self.process = None
self.output = ""
self.error = ""
def run(self, timeout):
def target():
self.process = subprocess.Popen(self.cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
self.output = self.process.stdout.read()
self.error = self.process.stderr.read()
self.child = self.process.communicate()[0]
thread = threading.Thread(target=target)
thread.start()
thread.join(timeout)
if thread.is_alive():
self.process.terminate()
thread.join()
retval = self.process.returncode
return (retval, self.output + "\n" + self.error)
if __name__ == '__main__':
main()