-
Notifications
You must be signed in to change notification settings - Fork 4
/
mmhistogram
99 lines (86 loc) · 2.28 KB
/
mmhistogram
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
#!/usr/bin/env python2
import argparse
import itertools
import math
import sys
parser = argparse.ArgumentParser(description='Print log-2 histogram, like systemtap')
parser.add_argument('-t', '--title', default="Values",
help='title to print')
parser.add_argument('-c', '--columns', type=int, default="50",
help='number of colums for')
parser.add_argument('-b', '--base', type=int, default="2",
help='log base')
parser.add_argument('-l', '--linear', action='store_true',
help='do linear, not log')
parser.add_argument('-j', '--justval', action='store_true',
help='ignore bounds, use values from input')
parser.add_argument('-p', '--percentage', action='store_true',
help='Print percentage instead of counts')
args = parser.parse_args()
M = []
for line in sys.stdin:
line = line.strip()
v = float(line)
M.append( v )
M.sort(reverse=True)
totalcount = len(M)
minval = M[-1]
maxval = M[0]
avgval = sum(M) / float(len(M))
devval = math.sqrt(sum([(m - avgval)**2 for m in M]) / float(len(M)))
medval = M[len(M)/2]
KV = []
for i in itertools.count():
if args.justval:
bound=M[-1]
boundb=M[-1]
elif not args.linear:
if i > 0:
boundb = args.base**(i-1)
else:
boundb = 0
bound = args.base**i
else:
boundb = args.base*(i)
bound = args.base*(i+1)
c = 0
while len(M) > 0 and bound >= M[-1] :
c += 1
M.pop()
KV.append( (boundb, bound, c) )
if not M:
break
maxcount = float(max(c for _, _, c in KV))
maxbound = KV[-1][0]
boundl = max(len(str(maxbound)), len('value'))
print "%s min:%.2f avg:%.2f med=%.2f max:%.2f dev:%.2f count:%d" % (
args.title,
minval,
avgval,
medval,
maxval,
devval,
totalcount,
)
print "%s:" % (
args.title,
)
print "%*s |%*s %s" % (
boundl+1,
"value",
args.columns,
"-" * args.columns,
"count"
)
for boundb, bound, c in KV:
if args.percentage:
cp = "%5.2f%%" % ((c / float(totalcount))*100.0,)
else:
cp = "%d" % (c,)
print "%*d |%*s %s" % (
boundl + 1,
boundb,
args.columns,
"*" * int(args.columns * (c/maxcount)),
cp
)