Coverage for MC6809/core/bechmark.py: 98%

41 statements  

« prev     ^ index     » next       coverage.py v7.2.1, created at 2023-03-06 19:50 +0100

1#!/usr/bin/env python 

2 

3""" 

4 MC6809 - 6809 CPU emulator in Python 

5 ======================================= 

6 

7 :created: 2014 by Jens Diemer - www.jensdiemer.de 

8 :copyleft: 2014-2015 by the MC6809 team, see AUTHORS for more details. 

9 :license: GNU GPL v3 or above, see LICENSE for more details. 

10""" 

11 

12 

13import logging 

14import string 

15import time 

16 

17from MC6809.tests.test_6809_program import Test6809_Program 

18from MC6809.utils.humanize import locale_format_number 

19 

20 

21log = logging.getLogger("MC6809") 

22 

23 

24class Test6809_Program2(Test6809_Program): 

25 def runTest(self): 

26 pass 

27 

28 def bench(self, loops, multiply, func, msg): 

29 print(f"\n{msg} benchmark") 

30 

31 self.setUp() 

32 self.cpu.cycles = 0 

33 

34 txt = string.printable 

35 txt = bytes(txt, encoding="UTF-8") 

36 txt = txt * multiply 

37 

38 print(f"\nStart {loops:d} {msg} loops with {len(txt):d} Bytes test string...") 

39 

40 start_time = time.time() 

41 for __ in range(loops): 

42 self._crc32(txt) 

43 duration = time.time() - start_time 

44 

45 print(f"{msg} benchmark runs {locale_format_number(self.cpu.cycles)} CPU cycles in {duration:.2f} sec") 

46 

47 return duration, self.cpu.cycles 

48 

49 def crc32_benchmark(self, loops, multiply): 

50 return self.bench(loops, multiply, self._crc32, "CRC32") 

51 

52 def crc16_benchmark(self, loops, multiply): 

53 return self.bench(loops, multiply, self._crc16, "CRC16") 

54 

55 

56def run_benchmark(loops, multiply): 

57 total_duration = 0 

58 total_cycles = 0 

59 bench_class = Test6809_Program2() 

60 

61 # -------------------------------------------------------------------------- 

62 

63 duration, cycles = bench_class.crc16_benchmark(loops, multiply) 

64 total_duration += duration 

65 total_cycles += cycles 

66 

67 # -------------------------------------------------------------------------- 

68 

69 duration, cycles = bench_class.crc32_benchmark(loops, multiply) 

70 total_duration += duration 

71 total_cycles += cycles 

72 

73 # -------------------------------------------------------------------------- 

74 print("-" * 79) 

75 print( 

76 f"\nTotal of {loops:d} benchmak loops run in {total_duration:.2f} sec" 

77 f" {locale_format_number(total_cycles)} CPU cycles." 

78 ) 

79 print(f"\tavg.: {locale_format_number(total_cycles / total_duration)} CPU cycles/sec")