adds flag for choosing bench representative algorithm.

git-svn-id: http://skia.googlecode.com/svn/trunk@5113 2bbb7eff-a529-9590-31e7-b0007b416f81
diff --git a/bench/bench_graph_svg.py b/bench/bench_graph_svg.py
index 8165c98..04c7bef 100644
--- a/bench/bench_graph_svg.py
+++ b/bench/bench_graph_svg.py
@@ -28,6 +28,8 @@
     print '-r <revision>[:<revision>] the revisions to show.'
     print '   Negative <revision> is taken as offset from most recent revision.'
     print '-s <setting>[=<value>] a setting to show (alpha, scalar, etc).'
+    print '-m <representative> use "avg", "min", or "med" for bench value.'
+    print '   They correspond to average, minimum and median of bench iters.'
     print '-t <time> the time to show (w, c, g, etc).'
     print '-x <int> the desired width of the svg.'
     print '-y <int> the desired height of the svg.'
@@ -88,7 +90,8 @@
     else:
         return latest_revision_found
 
-def parse_dir(directory, default_settings, oldest_revision, newest_revision):
+def parse_dir(directory, default_settings, oldest_revision, newest_revision,
+              rep):
     """Parses bench data from files like bench_r<revision>_<scalar>.
     
     (str, {str, str}, Number, Number) -> {int:[BenchDataPoints]}"""
@@ -110,7 +113,7 @@
             revision_data_points[revision] = []
         default_settings['scalar'] = scalar_type
         revision_data_points[revision].extend(
-                        bench_util.parse(default_settings, file_handle))
+                        bench_util.parse(default_settings, file_handle, rep))
         file_handle.close()
     return revision_data_points
 
@@ -267,7 +270,7 @@
     
     try:
         opts, _ = getopt.getopt(sys.argv[1:]
-                                 , "b:c:d:f:l:o:r:s:t:x:y:"
+                                 , "b:c:d:f:l:m:o:r:s:t:x:y:"
                                  , "default-setting=")
     except getopt.GetoptError, err:
         print str(err) 
@@ -278,6 +281,7 @@
     config_of_interest = None
     bench_of_interest = None
     time_of_interest = None
+    rep = "avg"  # bench representative calculation algorithm
     revision_range = '0:'
     regression_range = '0:'
     latest_revision = None
@@ -323,6 +327,10 @@
                 regression_range = value
             elif option == "-l":
                 title = value
+            elif option == "-m":
+                rep = value
+                if rep not in ["avg", "min", "med"]:
+                    raise Exception("Invalid -m representative: %s" % rep)
             elif option == "-o":
                 redirect_stdout(value)
             elif option == "-r":
@@ -355,7 +363,8 @@
     unfiltered_revision_data_points = parse_dir(directory
                                    , default_settings
                                    , oldest_revision
-                                   , newest_revision)
+                                   , newest_revision
+                                   , rep)
 
     # Filter out any data points that are utterly bogus... make sure to report
     # that we did so later!
diff --git a/bench/bench_util.py b/bench/bench_util.py
index 22ded9d..62b1e29 100644
--- a/bench/bench_util.py
+++ b/bench/bench_util.py
@@ -45,10 +45,11 @@
 Max = _ExtremeType(1, "Max")
 Min = _ExtremeType(-1, "Min")
 
-def parse(settings, lines):
+def parse(settings, lines, representative='avg'):
     """Parses bench output into a useful data structure.
     
-    ({str:str}, __iter__ -> str) -> [BenchDataPoint]"""
+    ({str:str}, __iter__ -> str) -> [BenchDataPoint]
+    representative is one of 'avg', 'min', 'med' (average, mean, median)."""
     
     benches = []
     current_bench = None
@@ -84,12 +85,26 @@
                     current_time_type = new_time.group(1)
                     iters = [float(i) for i in
                              new_time.group(2).strip().split(',')]
-                    current_time = sum(iters) / len(iters)
+                    iters.sort()
+                    iter_len = len(iters)
+                    if representative == 'avg':
+                        rep = sum(iters) / iter_len
+                    if representative == 'min':
+                        rep = iters[0]
+                    elif representative == 'med':
+                        if iter_len % 2:
+                            rep = (iters[iter_len / 2] +
+                                   iters[iter_len / 2 - 1]) / 2
+                        else:
+                            rep = iters[iter_len / 2]
+                    else:
+                        raise Exception("invalid representative algorithm %s!" %
+                                        representative)
                     benches.append(BenchDataPoint(
                             current_bench
                             , current_config
                             , current_time_type
-                            , current_time
+                            , rep
                             , settings))
     
     return benches