Pavlin Radoslavov | 1a39b48 | 2013-05-21 19:20:10 +0000 | [diff] [blame] | 1 | #! /usr/bin/env python |
| 2 | # -*- Mode: python; py-indent-offset: 4; tab-width: 8; indent-tabs-mode: t; -*- |
| 3 | |
| 4 | import functools |
| 5 | import math |
| 6 | import sys |
| 7 | |
| 8 | ## {{{ http://code.activestate.com/recipes/511478/ (r1) |
| 9 | |
| 10 | def percentile(N, percent, key=lambda x:x): |
| 11 | """ |
| 12 | Find the percentile of a list of values. |
| 13 | |
| 14 | @parameter N - is a list of values. Note N MUST BE already sorted. |
| 15 | @parameter percent - a float value from 0.0 to 1.0. |
| 16 | @parameter key - optional key function to compute value from each element of N. |
| 17 | |
| 18 | @return - the percentile of the values |
| 19 | """ |
| 20 | if not N: |
| 21 | return None |
| 22 | k = (len(N)-1) * percent |
| 23 | f = math.floor(k) |
| 24 | c = math.ceil(k) |
| 25 | if f == c: |
| 26 | return key(N[int(k)]) |
| 27 | d0 = key(N[int(f)]) * (c-k) |
| 28 | d1 = key(N[int(c)]) * (k-f) |
| 29 | return d0+d1 |
| 30 | |
| 31 | # median is 50th percentile. |
| 32 | # median = functools.partial(percentile, percent=0.5) |
| 33 | ## end of http://code.activestate.com/recipes/511478/ }}} |
| 34 | |
| 35 | if __name__ == "__main__": |
| 36 | |
| 37 | dict = {} |
| 38 | |
| 39 | # |
| 40 | # Read the data from the stdin, and store it in a dictionary. |
| 41 | # The dictionary uses lists as values. |
| 42 | # |
| 43 | data = sys.stdin.readlines() |
| 44 | for line in data: |
| 45 | words = line.split() |
| 46 | thread_n = int(words[0]) |
| 47 | msec = float(words[1]) |
| 48 | dict.setdefault(thread_n, []).append(msec) |
| 49 | |
| 50 | # |
| 51 | # Compute and print the values: median (50-th), 10-th, and 90-th |
| 52 | # percentile: |
| 53 | # <key> <median> <10-percentile> <90-percentile> |
| 54 | # |
| 55 | for key, val_list in sorted(dict.items()): |
| 56 | val_10 = percentile(sorted(val_list), 0.1) |
| 57 | val_50 = percentile(sorted(val_list), 0.5) |
| 58 | val_90 = percentile(sorted(val_list), 0.9) |
| 59 | print "%s %s %s %s" % (str(key), str(val_50), str(val_10), str(val_90)) |