Commits

Anonymous committed a90e3da

issue 20: add a csv report option to performance plugin

Comments (0)

Files changed (2)

functional_tests/test_performance_plugin.py

 import os
 import sys
 import unittest
+import tempfile
+import csv
+
 from nose.plugins.performance import Performance
 from nose.plugins import PluginTester
 
         assert 'test_perf.perf_function' in self.output
         assert 'Ran 10 tests in' in self.output
 
+
+class TestPerformancePluginCsvReport(PluginTester, unittest.TestCase):
+    
+    csv_report = tempfile.mktemp()
+    activate = "--performance"
+    args = ['--performance-csv', csv_report]
+    plugins = [Performance()]
+    suitepath = os.path.join(support, 'perf')
+
+    def runTest(self):
+        output = file(self.csv_report).read()
+        print '+' * 70
+        print output
+        print '+' * 70
+        
+        # check this parses as csv
+        parser = csv.reader(file(self.csv_report))
+        for line in parser:
+            pass
+        
+        # check contents
+        assert 'perf_method (test_perf.PerformanceTest),' in output
+        assert 'test_perf.perf_function,' in output

nose/plugins/performance.py

 import logging
 import os
 import time
+import csv
 from collections import defaultdict
 
 from nose.plugins import Plugin
 
 class Performance(Plugin):
     
-    iters = 'iters'
+    iters = None
+    csv = None
     
     def __init__(self):
         Plugin.__init__(self)
                           default=env.get('NOSE_PERFORMANCE_ITERS', 10),
                           help="Number of times to run a performance test (default 10)"
                           "specified by ITERS [NOSE_PERFORMANCE_ITERS]")
+        parser.add_option("--performance-csv",
+                        dest='csv', 
+                        action="store",
+                        type='string',
+                        default=env.get('NOSE_PERFORMANCE_CSV', None),
+                        help="Output performance statistics to csv file "
+                        "specified by CSV [NOSE_PERFORMANCE_CSV]")
 
     def configure(self, options, config):
         """Configure plugin
         """
         Plugin.configure(self, options, config)
         self.iters = options.iters
+        self.csv = options.csv
 
     def wantFunction(self, function):
         """This plugin wants all functions that start with 'perf'
         """
         self.loader = loader
 
+    def _report_data(self):
+        """Generate performance report data
+        """
+        for test in sorted(self._startTime):
+            timeTaken = [(stop-start) for (stop, start) in 
+                            zip(self._stopTime[test], self._startTime[test])]
+            avgTimeTaken = sum(timeTaken)/len(timeTaken)
+            yield (test, min(timeTaken), avgTimeTaken, max(timeTaken))
+        
+
     def report(self, stream):
         """Write the performance report to a stream
         """
         stream.write('-' * 70 + '\n')
         stream.write('Test Name\tMin time\tAvg time\tMax time\n')
         
-        for test in sorted(self._startTime):
-            timeTaken = [(stop-start) for (stop, start) in 
-                            zip(self._stopTime[test], self._startTime[test])]
-            avgTimeTaken = sum(timeTaken)/len(timeTaken)
-            stream.write("%s\t%.3fs\t%.3fs\t%.3fs\n" % (test, 
-                        min(timeTaken), avgTimeTaken, max(timeTaken)))
-        
-        
-        # prevent other output
-        return 1
-    
+        for data in self._report_data():
+            stream.write("%s\t%.3fs\t%.3fs\t%.3fs\n" % (data))
+
+    def finalize(self, stream):
+        """Write a csv report if requested
+        """
+        if not self.csv:
+            return
+            
+        writer = csv.writer(file(self.csv, 'w'))
+        writer.writerow('Test Name,Min Time,Avg Time,Max Time'.split(','))
+        [writer.writerow(data) for data in self._report_data()]
+