forked from ttsiodras/utils
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstats.py
More file actions
executable file
·95 lines (83 loc) · 2.8 KB
/
Copy pathstats.py
File metadata and controls
executable file
·95 lines (83 loc) · 2.8 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
#!/usr/bin/env python2
'''
If you are able to generate (or filter - grep, sed, awk, etc) a list
of numbers in your stdout, then just pipe it to this utility, and
you will get nice, colored statistics:
$ for i in {1..100} ; do echo $i ; done | stats.py
Statistics :
Average value: 50.50000
Std deviation: 28.86607
Sample stddev: 29.01149
Median: 50.50000
Min: 1.00000
Max: 100.00000
Overall: 50.5 +/- 57.4%
Very useful if, like me, you benchmark short-lived runs a lot - e.g.
$ for i in {1..10} ; do \
bash -c "time /bin/ls /usr/bin/" 2>&1 >/dev/null | \
grep ^real | \
sed 's,0m,,;s,s,,;' | \
awk '{print $2}' ;
done | stats.py
Naturally, I benchmark my own stuff, not ls :-)
'''
import math
from sys import stdout
# Colored message ANSI constants
g_green = chr(27) + "[32m" if stdout.isatty() else ""
g_yellow = chr(27) + "[33m" if stdout.isatty() else ""
g_normal = chr(27) + "[0m" if stdout.isatty() else ""
def printStatsOfList(results, label='Statistics', summaryOnly=False):
total = totalSq = n = 0
allOfThem = []
for a in results:
total += a
totalSq += a*a
n += 1
allOfThem.append(a)
if n == 0:
return
varianceFull = (totalSq - total*total/n)/n
if varianceFull < 0.:
varianceFull = 0.
if n > 1:
variance = (totalSq - total*total/n)/(n-1)
if variance < 0.:
variance = 0.
else:
variance = 0.
srted = sorted(allOfThem)
if summaryOnly:
s = g_green + ("%6.2f" % (total/n)) + " +/- " + "%6.2f%%" + g_normal
print s % ((100*math.sqrt(variance)*n/total) if total > 0 else 0.),
else:
print "\n", g_yellow+label+g_normal, ":"
samplesNo = len(allOfThem)
measurements = [
("Average value", total/n),
("Std deviation", math.sqrt(varianceFull)),
("Sample stddev", math.sqrt(variance)),
("Median",
srted[samplesNo/2]
if samplesNo % 2
else 0.5*(srted[samplesNo/2 - 1] + srted[samplesNo/2])),
("Min", srted[0]),
("Max", srted[-1]),
(g_green+"Overall", (str(total/n)+" +/- "+"%2.1f%%"+g_normal) %
((100*math.sqrt(variance)*n/total) if total > 0 else 0.))
]
for label, value in measurements:
print "%*s:" % (15, label),
if isinstance(value, str):
print value
else:
print "%.5f" % value
def readListOfIntegersOrFloatsFromStdin():
while True:
try:
a = float(raw_input())
yield a
except:
break
if __name__ == "__main__":
printStatsOfList(readListOfIntegersOrFloatsFromStdin())