Commits

Joe LaFata committed c1aa647

change the way output is handled
allow parameters to be tracked or not

Comments (0)

Files changed (2)

src/tests/test_unitbench.py

 
 class TestFunctionProfiler(TestCase):
         
-    def test_args(self):
-        fp = FunctionProfiler()
+    def test_track_parameters(self):
+        fp = FunctionProfiler(track_parameters=True)
         
         @fp
         def sum23():
         eq_(sum23(), 5)
         eq_(adder(7, 9), 16)
         eq_(adder(1, 1), 2)
-        print fp.results
         eq_(len(fp.results), 5)
         
     def test_args_to_str(self):
            "None")
         eq_(FunctionProfiler._args_to_str(1),
            "[1]")
+        eq_(FunctionProfiler._args_to_str(1, '1'),
+           "[1, \"1\"]")
         eq_(FunctionProfiler._args_to_str(1, 2, 3, 4),
            "[1, 2, 3, 4]")
         eq_(FunctionProfiler._args_to_str(1, 2, 3, 4, x=5),
-           "[1, 2, 3, 4] {\"x\":5}")
+           "[1, 2, 3, 4] {x:5}")
         eq_(FunctionProfiler._args_to_str(x=5),
-           "{\"x\":5}")
+           "{x:5}")
         eq_(FunctionProfiler._args_to_str(x=5, y=True),
-           "{\"x\":5, \"y\":True}")
+           "{x:5, y:True}")
         eq_(FunctionProfiler._args_to_str(2, x=5, y=True),
-           "[2] {\"x\":5, \"y\":True}")
+           "[2] {x:5, y:True}")
         eq_(FunctionProfiler._args_to_str('', x=5, y=True),
-           "[\"\"] {\"x\":5, \"y\":True}")
+           "[\"\"] {x:5, y:True}")
     
 class TestBenchmark(TestCase):
     def test_warmup(self):
         eq_(lines[3].find("Sample2"), 0)
         
 
-class TestCsvReporter(TestCase):
-    def test_write_titles(self):
+class TestCsvReporter(TestCase):        
+    def test_write_results(self):
         class sample(OneRun):
+            def input(self):
+                i = 10
+                while i < 10000:
+                    yield i
+                    i *= 10
+                    
             def warmup(self):
                 return 0
             
             def repeats(self):
-                return 0
+                return 3
             
             def bench_sample1(self):
                 self.no_param = True
         output = stream.getvalue()
         stream.close()
         
-        eq_("Values,Sample1,Sample2\n", output)
-        
+        lines = output.split("\n")
+        eq_("Values,Sample1,Sample2", lines[0])
+        eq_("10", lines[1].split(',')[0])
+        eq_("100", lines[2].split(',')[0])
+        eq_("1000", lines[3].split(',')[0])
         
         
 
     Combine one of the times and one of the statistics to get
     the appropriate attribute.
-
+    
+    The call_count is also included in the BenchResult.
+    
     Examples:
      * wall_mean - Mean wall clock time
      * user_std_dev - Standard deviation of user time
         if self.times == None:
             return self
         
+        self.call_count = len(self.times) 
+        
         time_types = ["wall", "user", "system"]
         for type in time_types:
             minimum = maxint
                        end_user_system[1] - start_user_system[1])
 
 class FunctionProfiler(BenchBase):
-    __slots__ = ["repeats", "warmup", "results"]
+    __slots__ = ["repeats", "warmup", "results", "track_parameters"]
     
     @staticmethod
     def _to_str(x):
                 value += ' '
             sorted_kwargs = sorted(kwargs.iteritems(), key=itemgetter(0))
             value += '{' + ', '.join(
-                 map(lambda x: to_str(x[0]) + ':' + to_str(x[1]), sorted_kwargs)) + '}'
+                 map(lambda x: x[0] + ':' + to_str(x[1]), sorted_kwargs)) + '}'
         return value
     
     @staticmethod
             return x
         return wrapper
     
-    def __init__(self, repeats=1, warmup=0):
+    def __init__(self, repeats=1, warmup=0, track_parameters=False):
         self.repeats = FunctionProfiler._wrap_int(repeats)
         self.warmup = FunctionProfiler._wrap_int(warmup)
+        self.track_parameters = track_parameters
         
         self.results = {}
         
             
             r = self._run_benchmark(func_call)
             
-            key = r.name + r.value
+            key = r.name
+            if self.track_parameters:
+                key += r.value
+                
             if key in self.results:
                 self.results[key].add_time(r.times)
             else:
         if not reporter:
             reporter = ConsoleReporter()
         benchmarks = sorted(self._find_benchmarks())
-        titles = map(Benchmark._function_name_to_title, benchmarks)
-        reporter.write_titles(titles)
 
         results = []
         for value in self.input():
     you'd like to write a custom importer this is the class to extend.
     """
     
-    def write_titles(self, titles):
-        """
-        Override this method if you'd like to write out the titles
-        at the beginning of your reporter.  The CsvReporter uses
-        this function to output the titles at the top of each column.
-        Alternatively, the ConsoleReporter doesn't override this
-        function because it doesn't need to output the titles of
-        each benchmark.
-        """
-        pass
-    
     def write_results(self, results):
         """
         Override this method to output the results of the benchmark
     def __init__(self, output_stream=sys.stdout, time_type="wall"):
         self.stream = output_stream
         self.time_type = time_type
-        
-    def write_titles(self, titles):
-        self.stream.write("Values," + ",".join(titles))
-        self.stream.write("\n")
     
     def write_results(self, results):
         
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.