1#!/usr/bin/env python
2
3"""
4  chewperf.py: Chew an http perf log
5  bucketize
6
7"""
8
9import sys, time
10
11def resets():
12    f = open(sys.argv[1]).read()
13    rawLines = f.split('\n')
14
15    times = []
16    for x in range(len(rawLines)):
17        line = rawLines[x].split()
18        try:
19            if line[-1] == "SIGNAL_STRENGTH":
20                ts = int(rawLines[x - 1].split()[-1])
21                times.append(ts)
22        except:
23            pass
24
25    return times
26
27def augment():
28    f = open(sys.argv[1]).read()
29    rawLines = f.split('\r\n')
30
31    out = []
32    t0 = None
33    last = 0
34    for line in rawLines:
35        if "Pulled" in line:
36            chewed = [int(line.split()[5]), int(line.split()[7])]
37            if not t0: t0 = chewed[1]
38            tm = chewed[1] - t0
39            out.append("%s %d" % (line, (tm - last)))
40            last = tm
41        else:
42            out.append(line)
43    print "\n".join(out)
44
45def chew():
46    f = open(sys.argv[1]).read()
47    rawLines = f.split('\n')
48    lines = [x for x in rawLines if "Pulled" in x]
49
50    sidx = lines[0].split().index("Pulled")
51    print "sidx", sidx
52    chewed = [[int(x.split()[sidx + 2]), int(x.split()[sidx + 4])] for x in lines]
53
54    t0 = chewed[0][1]
55    tLast = chewed[-1][1]
56    chewed = [[x[1] - t0, x[0]] for x in chewed]
57
58    totalTime = tLast - t0
59    bytes = sum(x[1] for x in chewed)
60    print "total time", totalTime, "bytes", bytes, "rate", bytes * 1000 / totalTime
61
62    buckets = {}
63    for x in chewed:
64        bucket = x[0] / 1000
65        bytes = x[1]
66        if bucket in buckets:
67            buckets[bucket] += bytes
68        else:
69            buckets[bucket] = bytes
70
71    top = max(buckets.keys())
72    for x in range(top):
73        if x not in buckets.keys():
74            buckets[x] = 0
75
76    # smooth
77    window = [0 for x in range(5)]
78
79    for x in range(len(buckets.items())):
80        window[x % len(window)] = buckets.items()[x][1]
81        print "%s\t%s" % (buckets.items()[x][0], sum(window) / len(window))
82
83def main():
84    chew()
85
86if __name__ == '__main__':
87    main()
88