Coverage for test_benchmarks.py: 43%

21 statements  

« prev     ^ index     » next       coverage.py v7.6.4, created at 2024-12-24 08:16 +0100

1# (c) 2024 Martin Wendt; see https://github.com/mar10/benchman 

2# Licensed under the MIT license: https://www.opensource.org/licenses/mit-license.php 

3""" """ 

4# ruff: noqa: T201, T203 `print` found 

5# ruff: noqa: E501 Line too long 

6 

7import sys 

8 

9import pytest 

10from benchman import BenchmarkManager 

11 

12from . import fixtures # noqa: F401 

13 

14benchmark = pytest.mark.skipif( 

15 "--benchmarks" not in sys.argv, 

16 reason="`--benchmarks` not set", 

17) 

18 

19 

20@benchmark 

21class TestBenchmarks: 

22 # @pytest.mark.xfail(reason="just testing") 

23 

24 def _test_sort_suite(self, *, bm: BenchmarkManager, data: list): 

25 """ """ 

26 bmr = bm.make_runner(name="sort", sample_size=len(data)) 

27 bmr.run( 

28 variant="quick_sort", 

29 stmt="""\ 

30 _ = fix.quick_sort(arr) 

31 """, 

32 setup="arr = data.copy()", 

33 globals={"data": data, "fix": fixtures}, 

34 ) 

35 bmr.run( 

36 variant="bubble_sort", 

37 stmt="""\ 

38 _ = fix.bubble_sort(arr) 

39 """, 

40 setup="arr = data.copy()", 

41 globals={"data": data, "fix": fixtures}, 

42 ) 

43 bmr.run( 

44 variant="insertion_sort", 

45 stmt="""\ 

46 _ = fix.insertion_sort(arr) 

47 """, 

48 setup="arr = data.copy()", 

49 globals={"data": data, "fix": fixtures}, 

50 ) 

51 bmr.run( 

52 variant="builtin", 

53 stmt="""\ 

54 _ = fix.native_sort(arr) 

55 """, 

56 setup="arr = data.copy()", 

57 globals={"data": data, "fix": fixtures}, 

58 ) 

59 return bmr 

60 

61 def test_bench_sort(self, capsys, benchman): 

62 self._test_sort_suite(bm=benchman, data=fixtures.SMALL_RANDOM_ARRAY) 

63 self._test_sort_suite(bm=benchman, data=fixtures.MEDIUM_RANDOM_ARRAY) 

64 self._test_sort_suite(bm=benchman, data=fixtures.LARGE_RANDOM_ARRAY) 

65 

66 with capsys.disabled(): 

67 print(f"\n{benchman}") 

68 benchman.print_results()