@@ -93,7 +93,7 @@ def test_help_compare(testdir, args):
9393 ' [--columns LABELS] [--name FORMAT]' ,
9494 ' [--time-unit COLUMN]' ,
9595 ' [--histogram [FILENAME-PREFIX]]' ,
96- ' [--csv [FILENAME]]' ,
96+ ' [--csv [FILENAME]] [-k EXPR] ' ,
9797 ' [[]glob_or_file *[]]' ,
9898 '' ,
9999 'Compare saved runs.' ,
@@ -125,6 +125,9 @@ def test_help_compare(testdir, args):
125125 " --csv [FILENAME] Save a csv report. If FILENAME contains slashes ('/')" ,
126126 ' then directories will be created. Default:' ,
127127 " 'benchmark_*'" ,
128+ ' -k EXPR Only show benchmarks matching the given expression.' ,
129+ " Uses the same syntax as pytest's -k option (e.g. 'foo" ,
130+ " and not bar')." ,
128131 '' ,
129132 'examples:' ,
130133 '' ,
@@ -358,3 +361,35 @@ def func():
358361 )
359362 result = testdir .run ('py.test-benchmark' , 'compare' , '--csv' )
360363 result .stderr .fnmatch_lines (['Generated csv: *.csv' ])
364+
365+
366+ def test_compare_filter (testdir ):
367+ test = testdir .makepyfile ("""
368+ import pytest
369+ @pytest.mark.parametrize("arg", ["foo", "bar"])
370+ def test_alpha(benchmark, arg):
371+ benchmark(lambda: None)
372+ def test_beta(benchmark):
373+ benchmark(lambda: None)
374+ """ )
375+ result = testdir .runpytest_subprocess ('--benchmark-autosave' , test )
376+ result .stderr .fnmatch_lines (['Saved benchmark data in: *' ])
377+
378+ # Filter to only test_alpha benchmarks
379+ result = testdir .run ('py.test-benchmark' , 'compare' , '-k' , 'alpha' , '--columns' , 'min,max' )
380+ result .stdout .fnmatch_lines (['*benchmark: 2 tests*' ])
381+ assert 'test_beta' not in result .stdout .str ()
382+
383+ # Filter with "not" expression
384+ result = testdir .run ('py.test-benchmark' , 'compare' , '-k' , 'not alpha' , '--columns' , 'min,max' )
385+ result .stdout .fnmatch_lines (['*benchmark: 1 tests*' ])
386+ assert 'test_alpha' not in result .stdout .str ()
387+
388+ # Filter with "and" expression
389+ result = testdir .run ('py.test-benchmark' , 'compare' , '-k' , 'alpha and foo' , '--columns' , 'min,max' )
390+ result .stdout .fnmatch_lines (['*benchmark: 1 tests*' ])
391+ assert 'bar' not in result .stdout .str ()
392+
393+ # No filter shows all
394+ result = testdir .run ('py.test-benchmark' , 'compare' , '--columns' , 'min,max' )
395+ result .stdout .fnmatch_lines (['*benchmark: 3 tests*' ])
0 commit comments