1
1
#!/usr/bin/python
2
2
# -*- coding: utf-8 -*-
3
3
4
- # ===--- benchmark_utils .py -------- --------------------------------------===//
4
+ # ===--- test_compare_perf_tests .py --------------------------------------===//
5
5
#
6
6
# This source file is part of the Swift.org open source project
7
7
#
15
15
16
16
import os
17
17
import shutil
18
+ import sys
18
19
import tempfile
19
20
import unittest
20
21
22
+ from StringIO import StringIO
23
+ from contextlib import contextmanager
24
+
21
25
from compare_perf_tests import PerformanceTestResult
22
26
from compare_perf_tests import ReportFormatter
23
27
from compare_perf_tests import ResultComparison
24
28
from compare_perf_tests import TestComparator
29
+ from compare_perf_tests import main
25
30
from compare_perf_tests import parse_args
26
31
27
32
33
+ @contextmanager
34
+ def captured_output ():
35
+ new_out , new_err = StringIO (), StringIO ()
36
+ old_out , old_err = sys .stdout , sys .stderr
37
+ try :
38
+ sys .stdout , sys .stderr = new_out , new_err
39
+ yield sys .stdout , sys .stderr
40
+ finally :
41
+ sys .stdout , sys .stderr = old_out , old_err
42
+
43
+
28
44
class TestPerformanceTestResult (unittest .TestCase ):
29
45
30
46
def test_init (self ):
@@ -195,6 +211,11 @@ def write_temp_file(self, file_name, data):
195
211
f .write (data )
196
212
return temp_file_name
197
213
214
+ def assert_report_contains (self , texts , report ):
215
+ assert not isinstance (texts , str )
216
+ for text in texts :
217
+ self .assertIn (text , report )
218
+
198
219
199
220
class TestTestComparator (OldAndNewLog ):
200
221
def test_init (self ):
@@ -236,14 +257,6 @@ def setUp(self):
236
257
self .git = self .rf .git ()
237
258
self .html = self .rf .html ()
238
259
239
- def assert_report_contains (self , texts , report ):
240
- # assert not isinstance(texts, str)
241
- if isinstance (texts , str ):
242
- self .assertIn (texts , report )
243
- else :
244
- for text in texts :
245
- self .assertIn (text , report )
246
-
247
260
def assert_markdown_contains (self , texts ):
248
261
self .assert_report_contains (texts , self .markdown )
249
262
@@ -257,7 +270,6 @@ def test_justified_columns(self):
257
270
"""Table columns are all formated with same width, defined by the
258
271
longest value.
259
272
"""
260
- print self .markdown
261
273
self .assert_markdown_contains ([
262
274
'AnyHashableWithAClass | 247027 | 319065 | 259056 | 10250445' ,
263
275
'Array2D | 335831 | 335831 | +0.0% | 1.00x' ])
@@ -269,7 +281,6 @@ def test_column_headers(self):
269
281
"""Report contains table headers for ResultComparisons and changed
270
282
PerformanceTestResults.
271
283
"""
272
- print self .git
273
284
self .assert_markdown_contains ([
274
285
'TEST | OLD | NEW | DELTA | SPEEDUP' ,
275
286
'--- | --- | --- | --- | --- ' ,
@@ -304,7 +315,6 @@ def test_emphasize_speedup(self):
304
315
'AngryPhonebook 10458 10458 +0.0% 1.00x' ,
305
316
'ArrayAppend 23641 20000 -15.4% **1.18x (?)**'
306
317
])
307
- print self .html
308
318
self .assert_html_contains ([
309
319
"""
310
320
<tr>
@@ -374,21 +384,30 @@ class Test_parse_args(unittest.TestCase):
374
384
required = ['--old-file' , 'old.log' , '--new-file' , 'new.log' ]
375
385
376
386
def test_required_input_arguments (self ):
387
+ with captured_output () as (_ , err ):
388
+ self .assertRaises (SystemExit , parse_args , [])
389
+ self .assertIn ('usage: compare_perf_tests.py' , err .getvalue ())
390
+
377
391
args = parse_args (self .required )
378
392
self .assertEquals (args .old_file , 'old.log' )
379
393
self .assertEquals (args .new_file , 'new.log' )
380
- self .assertRaises (SystemExit , parse_args , [])
381
394
382
395
def test_format_argument (self ):
396
+ self .assertEquals (parse_args (self .required ).format , 'markdown' )
383
397
self .assertEquals (
384
398
parse_args (self .required + ['--format' , 'markdown' ]).format ,
385
399
'markdown' )
386
400
self .assertEquals (
387
401
parse_args (self .required + ['--format' , 'git' ]).format , 'git' )
388
402
self .assertEquals (
389
403
parse_args (self .required + ['--format' , 'html' ]).format , 'html' )
390
- self .assertRaises (SystemExit , parse_args ,
391
- self .required + ['--format' , 'bogus' ])
404
+
405
+ with captured_output () as (_ , err ):
406
+ self .assertRaises (SystemExit , parse_args ,
407
+ self .required + ['--format' , 'bogus' ])
408
+ self .assertIn ("error: argument --format: invalid choice: 'bogus' "
409
+ "(choose from 'markdown', 'git', 'html')" ,
410
+ err .getvalue ())
392
411
393
412
def test_delta_threshold_argument (self ):
394
413
# default value
@@ -401,8 +420,13 @@ def test_delta_threshold_argument(self):
401
420
self .assertEquals (args .delta_threshold , 1.0 )
402
421
args = parse_args (self .required + ['--delta-threshold' , '.2' ])
403
422
self .assertEquals (args .delta_threshold , 0.2 )
404
- self .assertRaises (SystemExit , parse_args ,
405
- self .required + ['--delta-threshold' , '2,2' ])
423
+
424
+ with captured_output () as (_ , err ):
425
+ self .assertRaises (SystemExit , parse_args ,
426
+ self .required + ['--delta-threshold' , '2,2' ])
427
+ self .assertIn (" error: argument --delta-threshold: invalid float "
428
+ "value: '2,2'" ,
429
+ err .getvalue ())
406
430
407
431
def test_output_argument (self ):
408
432
self .assertEquals (parse_args (self .required ).output , None )
@@ -428,5 +452,80 @@ def test_branch_arguments(self):
428
452
self .assertEquals (args .new_branch , 'amazing-optimization' )
429
453
430
454
455
+ class Test_compare_perf_tests_main (OldAndNewLog ):
456
+ """Integration test that invokes the whole comparison script."""
457
+ markdown = [
458
+ '<summary>Regression (1)</summary>' ,
459
+ 'TEST | OLD | NEW | DELTA | SPEEDUP' ,
460
+ 'BitCount | 3 | 9 | +199.9% | **0.33x**' ,
461
+ ]
462
+ git = [
463
+ 'Regression (1):' ,
464
+ 'TEST OLD NEW DELTA SPEEDUP' ,
465
+ 'BitCount 3 9 +199.9% **0.33x**' ,
466
+ ]
467
+ html = ['<html>' , "<td align='left'>BitCount</td>" ]
468
+
469
+ def execute_main_with_format (self , report_format , test_output = False ):
470
+ report_file = self .test_dir + 'report.log'
471
+ args = ['compare_perf_tests.py' ,
472
+ '--old-file' , self .old_log ,
473
+ '--new-file' , self .new_log ,
474
+ '--format' , report_format ]
475
+
476
+ sys .argv = (args if not test_output else
477
+ args + ['--output' , report_file ])
478
+
479
+ with captured_output () as (out , _ ):
480
+ main ()
481
+ report_out = out .getvalue ()
482
+
483
+ if test_output :
484
+ with open (report_file , 'r' ) as f :
485
+ report = f .read ()
486
+ # because print adds newline, add one here, too:
487
+ report_file = str (report + '\n ' )
488
+ else :
489
+ report_file = None
490
+
491
+ return report_out , report_file
492
+
493
+ def test_markdown (self ):
494
+ """Writes Markdown formatted report to stdout"""
495
+ report_out , _ = self .execute_main_with_format ('markdown' )
496
+ self .assert_report_contains (self .markdown , report_out )
497
+
498
+ def test_markdown_output (self ):
499
+ """Writes Markdown formatted report to stdout and `--output` file."""
500
+ report_out , report_file = (
501
+ self .execute_main_with_format ('markdown' , test_output = True ))
502
+ self .assertEquals (report_out , report_file )
503
+ self .assert_report_contains (self .markdown , report_file )
504
+
505
+ def test_git (self ):
506
+ """Writes Git formatted report to stdout."""
507
+ report_out , _ = self .execute_main_with_format ('git' )
508
+ self .assert_report_contains (self .git , report_out )
509
+
510
+ def test_git_output (self ):
511
+ """Writes Git formatted report to stdout and `--output` file."""
512
+ report_out , report_file = (
513
+ self .execute_main_with_format ('git' , test_output = True ))
514
+ self .assertEquals (report_out , report_file )
515
+ self .assert_report_contains (self .git , report_file )
516
+
517
+ def test_html (self ):
518
+ """Writes HTML formatted report to stdout."""
519
+ report_out , _ = self .execute_main_with_format ('html' )
520
+ self .assert_report_contains (self .html , report_out )
521
+
522
+ def test_html_output (self ):
523
+ """Writes HTML formatted report to stdout and `--output` file."""
524
+ report_out , report_file = (
525
+ self .execute_main_with_format ('html' , test_output = True ))
526
+ self .assertEquals (report_out , report_file )
527
+ self .assert_report_contains (self .html , report_file )
528
+
529
+
431
530
if __name__ == '__main__' :
432
531
unittest .main ()
0 commit comments