1. Joe LaFata
  2. unitbench

Commits

Joe LaFata  committed 8aded34

only format function names when using the bench prefix and extending Benchmark

  • Participants
  • Parent commits 941b517
  • Branches default

Comments (0)

Files changed (2)

File src/tests/test_unitbench.py

View file
  • Ignore whitespace
 '''
 import sys
 from nose.tools import assert_almost_equals, assert_raises, eq_
-from unitbench import _function_name_to_title
 from unitbench import TimeSet, FunctionProfiler, Benchmark
 from unitbench import BenchResult, Reporter, ConsoleReporter, CsvReporter
 from unittest import TestCase
         assert "bench_sample4" in bms
         
     def test_function_name_to_title(self):
-        eq_(_function_name_to_title("bench_sample1_sample2"), "Sample1 Sample2")
-        eq_(_function_name_to_title("benchSample1Sample2"), "Sample1 Sample2")
-        eq_(_function_name_to_title("sample1_sample2"), "Sample1 Sample2")
-        eq_(_function_name_to_title("Sample1Sample2"), "Sample1 Sample2")
-        eq_(_function_name_to_title("_sample1_sample2_"), "Sample1 Sample2")
-        eq_(_function_name_to_title("XMLBenchmark"), "Xml Benchmark")
+        eq_(Benchmark._function_name_to_title("bench_sample1_sample2"), "Sample1 Sample2")
+        eq_(Benchmark._function_name_to_title("benchSample1Sample2"), "Sample1 Sample2")
+        eq_(Benchmark._function_name_to_title("BENCHsample1_sample2"), "Benchsample1 Sample2")
+        eq_(Benchmark._function_name_to_title("Sample1Sample2"), "Sample1 Sample2")
+        eq_(Benchmark._function_name_to_title("_sample1_sample2_"), "Sample1 Sample2")
+        eq_(Benchmark._function_name_to_title("XMLBenchmark"), "Xml Benchmark")
 
 class TestConsoleReporter(TestCase):
     def test_write_results(self):
         bm = sample()
         
         stream = StringIO()
-        bm.run(ConsoleReporter(stream)) 
+        bm.run(ConsoleReporter(stream, ConsoleReporter.GroupType.VALUE)) 
         
         output = stream.getvalue()
         stream.close()

File src/unitbench.py

View file
  • Ignore whitespace
         if not reporter:
             reporter = ConsoleReporter()
         benchmarks = sorted(self._find_benchmarks())
-        reporter.write_titles(benchmarks)
+        titles = map(Benchmark._function_name_to_title, benchmarks)
+        reporter.write_titles(titles)
 
         results = []
         for value in self.input():
                 func_call.setup = self.setup
                 func_call.teardown = self.teardown
                 
-                results.append(self._run_benchmark(func_call))
+                r = self._run_benchmark(func_call)
+                r.name = Benchmark._function_name_to_title(r.name)
+                results.append(r)
         reporter.write_results(results)
+    
+    @staticmethod
+    def _function_name_to_title(name):
+        output = name
+        if output.startswith("bench"):
+            output = output[5:]
+        if output.find("_") != -1:
+            return string.capwords(output.replace("_", " ").strip())
+        else:
+            s1 = re.sub('(.)([A-Z][a-z]+)', r'\1 \2', output)
+            return string.capwords(
+                        re.sub('([a-z0-9])([A-Z])', r'\1 \2', s1).strip())
             
     def _find_benchmarks(self):
         """Return a suite of all tests cases contained in testCaseClass"""
         """
         pass
 
-def _function_name_to_title(name):
-    output = name
-    if output.startswith("bench"):
-        output = output[5:]
-    if output.find("_") != -1:
-        return string.capwords(output.replace("_", " ").strip())
-    else:
-        s1 = re.sub('(.)([A-Z][a-z]+)', r'\1 \2', output)
-        return string.capwords(
-                    re.sub('([a-z0-9])([A-Z])', r'\1 \2', s1).strip())
-
 class ConsoleReporter(Reporter):
     class GroupType(object):
         FUNCTION = "name"
                     hasattr(r, "system_mean") and hasattr(r, "wall_mean")):
                     
                     o = other(r)
-                    if self.group_by == ConsoleReporter.GroupType.VALUE:
-                        o = _function_name_to_title(o)
-                        
                     if len(o) >= 40:
                         key.append(o)
                         digits = ConsoleReporter.__count_digits(len(key))
         self.time_type = time_type
         
     def write_titles(self, titles):
-        titles = map(_function_name_to_title, titles)
         self.stream.write("Values," + ",".join(titles))
         self.stream.write("\n")