|
1 | 1 | from iop._director import _Director |
2 | 2 | from iop._utils import _Utils |
3 | | - |
4 | 3 | import timeit |
5 | 4 | import os |
6 | 5 |
|
7 | 6 | class TestBenchIoP: |
| 7 | + TEST_CASES = [ |
| 8 | + { |
| 9 | + 'name': 'Python BP to Python BO with Iris Message', |
| 10 | + 'component': 'Python.BenchIoPProcess', |
| 11 | + 'message_type': 'iris.Ens.StringRequest', |
| 12 | + 'use_json': False |
| 13 | + }, |
| 14 | + { |
| 15 | + 'name': 'Python BP to ObjetScript BO with Iris Message', |
| 16 | + 'component': 'Python.BenchIoPProcess.To.Cls', |
| 17 | + 'message_type': 'iris.Ens.StringRequest', |
| 18 | + 'use_json': False |
| 19 | + }, |
| 20 | + { |
| 21 | + 'name': 'Python BP to Python BO with Python Message', |
| 22 | + 'component': 'Python.BenchIoPProcess', |
| 23 | + 'message_type': 'msg.MyMessage', |
| 24 | + 'use_json': True |
| 25 | + }, |
| 26 | + { |
| 27 | + 'name': 'Python BP to ObjetScript BO with Python Message', |
| 28 | + 'component': 'Python.BenchIoPProcess.To.Cls', |
| 29 | + 'message_type': 'msg.MyMessage', |
| 30 | + 'use_json': True |
| 31 | + }, |
| 32 | + { |
| 33 | + 'name': 'ObjetScript BP to Python BO with Iris Message', |
| 34 | + 'component': 'Bench.Process', |
| 35 | + 'message_type': 'iris.Ens.StringRequest', |
| 36 | + 'use_json': False |
| 37 | + }, |
| 38 | + { |
| 39 | + 'name': 'ObjetScript BP to ObjetScript BO with Iris Message', |
| 40 | + 'component': 'Bench.Process.To.Cls', |
| 41 | + 'message_type': 'iris.Ens.StringRequest', |
| 42 | + 'use_json': False |
| 43 | + }, |
| 44 | + { |
| 45 | + 'name': 'ObjetScript BP to Python BO with Python Message', |
| 46 | + 'component': 'Bench.Process', |
| 47 | + 'message_type': 'msg.MyMessage', |
| 48 | + 'use_json': True |
| 49 | + }, |
| 50 | + { |
| 51 | + 'name': 'ObjetScript BP to ObjetScript BO with Python Message', |
| 52 | + 'component': 'Bench.Process.To.Cls', |
| 53 | + 'message_type': 'msg.MyMessage', |
| 54 | + 'use_json': True |
| 55 | + } |
| 56 | + ] |
8 | 57 |
|
9 | | - #before all tests |
10 | 58 | @classmethod |
11 | 59 | def setup_class(cls): |
12 | | - # get abspath of 'src/tests/bench' |
13 | 60 | path = os.path.abspath('src/tests/bench/settings.py') |
14 | | - # migrate the production |
15 | 61 | _Utils.migrate(path) |
16 | | - # stop all productions |
17 | 62 | _Director.stop_production() |
18 | | - # set the default production |
19 | 63 | _Director.set_default_production('Bench.Production') |
20 | | - # start the production |
21 | 64 | _Director.start_production() |
22 | | - # create a list of results |
23 | 65 | cls.results = [] |
24 | 66 |
|
25 | 67 | def test_bench_iris(self): |
26 | | - # this test is made to preload the production |
27 | 68 | _Director.test_component('Python.BenchIoPProcess') |
28 | 69 |
|
29 | | - |
30 | | - def test_bench_iris_message(self): |
31 | | - body = "test" |
32 | | - result = timeit.timeit(lambda: _Director.test_component('Python.BenchIoPProcess','','iris.Ens.StringRequest',body), number=1) |
33 | | - # set the result in the list |
34 | | - name = 'Python BP to Python BO with Iris Message' |
35 | | - self.results.append((name,result)) |
36 | | - # assert the result |
37 | | - assert result > 0 |
38 | | - |
39 | | - def test_bench_iris_message_to_cls(self): |
40 | | - body = "test" |
41 | | - result = timeit.timeit(lambda: _Director.test_component('Python.BenchIoPProcess.To.Cls','','iris.Ens.StringRequest',body), number=1) |
42 | | - # set the result in the list |
43 | | - name = 'Python BP to ObjetScript BO with Iris Message' |
44 | | - self.results.append((name,result)) |
45 | | - # assert the result |
46 | | - assert result > 0 |
47 | | - |
48 | | - def test_bench_python_message(self): |
49 | | - body = "test" |
50 | | - result = timeit.timeit(lambda: _Director.test_component('Python.BenchIoPProcess','','msg.MyMessage',f'{{"message":"{body}"}}'), number=1) |
51 | | - # set the result in the list |
52 | | - name = 'Python BP to Python BO with Python Message' |
53 | | - self.results.append((name,result)) |
54 | | - # assert the result |
55 | | - assert result > 0 |
56 | | - |
57 | | - def test_bench_python_message_to_cls(self): |
| 70 | + def run_benchmark(self, test_case): |
58 | 71 | body = "test" |
59 | | - result = timeit.timeit(lambda: _Director.test_component('Python.BenchIoPProcess.To.Cls','','msg.MyMessage',f'{{"message":"{body}"}}'), number=1) |
60 | | - # set the result in the list |
61 | | - name = 'Python BP to ObjetScript BO with Python Message' |
62 | | - self.results.append((name,result)) |
63 | | - # assert the result |
| 72 | + message = f'{{"message":"{body}"}}' if test_case['use_json'] else body |
| 73 | + result = timeit.timeit( |
| 74 | + lambda: _Director.test_component( |
| 75 | + test_case['component'], |
| 76 | + '', |
| 77 | + test_case['message_type'], |
| 78 | + message |
| 79 | + ), |
| 80 | + number=1 |
| 81 | + ) |
| 82 | + self.results.append((test_case['name'], result)) |
64 | 83 | assert result > 0 |
65 | 84 |
|
66 | | - def test_bench_cls_iris_message(self): |
67 | | - body = "test" |
68 | | - result = timeit.timeit(lambda: _Director.test_component('Bench.Process','','iris.Ens.StringRequest',body), number=1) |
69 | | - # set the result in the list |
70 | | - name = 'ObjetScript BP to Python BO with Iris Message' |
71 | | - self.results.append((name,result)) |
72 | | - # assert the result |
73 | | - assert result > 0 |
74 | | - |
75 | | - def test_bench_cls_iris_message_to_cls(self): |
76 | | - body = "test" |
77 | | - result = timeit.timeit(lambda: _Director.test_component('Bench.Process.To.Cls','','iris.Ens.StringRequest',body), number=1) |
78 | | - # set the result in the list |
79 | | - name = 'ObjetScript BP to ObjetScript BO with Iris Message' |
80 | | - self.results.append((name,result)) |
81 | | - # assert the result |
82 | | - assert result > 0 |
83 | | - |
84 | | - def test_bench_cls_python_message(self): |
85 | | - body = "test" |
86 | | - result = timeit.timeit(lambda: _Director.test_component('Bench.Process','','msg.MyMessage',f'{{"message":"{body}"}}'), number=1) |
87 | | - # set the result in the list |
88 | | - name = 'ObjetScript BP to Python BO with Python Message' |
89 | | - self.results.append((name,result)) |
90 | | - # assert the result |
91 | | - assert result > 0 |
92 | | - |
93 | | - def test_bench_cls_python_message_to_cls(self): |
94 | | - body = "test" |
95 | | - result = timeit.timeit(lambda: _Director.test_component('Bench.Process.To.Cls','','msg.MyMessage',f'{{"message":"{body}"}}'), number=1) |
96 | | - # set the result in the list |
97 | | - name = 'ObjetScript BP to ObjetScript BO with Python Message' |
98 | | - self.results.append((name,result)) |
99 | | - # assert the result |
100 | | - assert result > 0 |
| 85 | + def test_all_benchmarks(self): |
| 86 | + for test_case in self.TEST_CASES: |
| 87 | + self.run_benchmark(test_case) |
101 | 88 |
|
102 | | - #after all tests |
103 | 89 | @classmethod |
104 | 90 | def teardown_class(cls): |
105 | | - # stop all productions |
106 | 91 | _Director.stop_production() |
107 | | - # set the default production |
108 | 92 | _Director.set_default_production('test') |
109 | | - # write the results in a file |
110 | 93 | current_dir = os.path.dirname(os.path.abspath(__file__)) |
111 | | - with open(os.path.join(current_dir,'bench','result.txt'),'w') as f: |
112 | | - for name,result in cls.results: |
| 94 | + with open(os.path.join(current_dir, 'bench', 'result.txt'), 'w') as f: |
| 95 | + for name, result in cls.results: |
113 | 96 | f.write(f'{name}: {result}\n') |
0 commit comments