Coverage for pyfields/tests/_test_benchmarks.py: 0%

55 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2023-11-06 16:35 +0000

1# Authors: Sylvain Marie <sylvain.marie@se.com> 

2# 

3# Copyright (c) Schneider Electric Industries, 2019. All right reserved. 

4import pytest 

5import dataclasses as dc 

6from attr import attrs, attrib 

7 

8from pyfields import field, inject_fields 

9 

10 

11def _create_class_creator(type): 

12 if type == "pyfields": 

13 def _call_me(): 

14 class Wall(object): 

15 height = field(doc="Height of the wall in mm.") # type: int 

16 color = field(default='white', doc="Color of the wall.") # type: str 

17 

18 @inject_fields 

19 def __init__(self, fields): 

20 fields.init(self) 

21 return Wall 

22 

23 elif type == "python": 

24 def _call_me(): 

25 class Wall(object): 

26 def __init__(self, 

27 height, # type: int 

28 color='white' # type: str 

29 ): 

30 """ 

31 

32 :param height: Height of the wall in mm. 

33 :param color: Color of the wall. 

34 """ 

35 self.height = height 

36 self.color = color 

37 return Wall 

38 elif type == "attrs": 

39 def _call_me(): 

40 @attrs 

41 class Wall(object): 

42 height: int = attrib(repr=False, cmp=False, hash=False) 

43 color: str = attrib(default='white', repr=False, cmp=False, hash=False) 

44 

45 return Wall 

46 

47 elif type == "dataclass": 

48 def _call_me(): 

49 @dc.dataclass 

50 class Wall(object): 

51 height: int = dc.field(init=True) 

52 color: str = dc.field(default='white', init=True) 

53 

54 return Wall 

55 else: 

56 raise ValueError() 

57 

58 return _call_me 

59 

60 

61@pytest.mark.parametrize("type", ["python", "pyfields", "attrs", "dataclass"]) 

62def test_timers_class(benchmark, type): 

63 # benchmark it 

64 benchmark(_create_class_creator(type)) 

65 

66 

67def _instantiate(clazz): 

68 return lambda: clazz(color='hello', height=50) 

69 

70 

71@pytest.mark.parametrize("type", ["python", "pyfields", "attrs", "dataclass"]) 

72def test_timers_instance(benchmark, type): 

73 clazz = _create_class_creator(type)() 

74 

75 benchmark(_instantiate(clazz)) 

76 

77 

78def _read_field(obj): 

79 return lambda: obj.color 

80 

81 

82@pytest.mark.parametrize("type", ["python", "pyfields", "attrs", "dataclass"]) 

83def test_timers_instance_read(benchmark, type): 

84 clazz = _create_class_creator(type)() 

85 obj = clazz(color='hello', height=50) 

86 

87 benchmark(_read_field(obj)) 

88 

89 

90def _write_field(obj): 

91 obj.color = 'sky_blue' 

92 

93 

94@pytest.mark.parametrize("type", ["python", "pyfields", "attrs", "dataclass"]) 

95def test_timers_instance_write(benchmark, type): 

96 clazz = _create_class_creator(type)() 

97 obj = clazz(color='hello', height=50) 

98 

99 benchmark(_write_field, obj)