Mercurial > hg > orthanc-tests
annotate PerfsDb/Test.py @ 510:fb900401be28
fix
author | Alain Mazy <am@osimis.io> |
---|---|
date | Mon, 20 Mar 2023 18:06:08 +0100 |
parents | f42c10dab862 |
children |
rev | line source |
---|---|
200 | 1 import os |
156 | 2 import time |
3 import statistics | |
4 from orthancRestApi import OrthancClient | |
5 | |
6 from TestResult import TestResult | |
7 | |
8 class Test: | |
9 | |
10 def __init__(self, name: str): | |
11 self.name = name | |
12 self._orthanc = None | |
164 | 13 self.repeatCount = 20 |
156 | 14 |
15 def setOrthancClient(self, orthanc: OrthancClient): | |
16 self._orthanc = orthanc | |
17 | |
18 def setRepeatCount(self, repeatCount: int): | |
19 self.repeatCount = repeatCount | |
20 | |
158
df1f9946571c
perfs db continued: tests working with tiny DBs on all setup but the sqliteplugin
am@osimis.io
parents:
156
diff
changeset
|
21 def beforeAll(self): |
df1f9946571c
perfs db continued: tests working with tiny DBs on all setup but the sqliteplugin
am@osimis.io
parents:
156
diff
changeset
|
22 """ |
df1f9946571c
perfs db continued: tests working with tiny DBs on all setup but the sqliteplugin
am@osimis.io
parents:
156
diff
changeset
|
23 Code to execute before the execution of all repetitions of a test; i.e: upload a file. |
df1f9946571c
perfs db continued: tests working with tiny DBs on all setup but the sqliteplugin
am@osimis.io
parents:
156
diff
changeset
|
24 This code is not included in timings |
156 | 25 """ |
158
df1f9946571c
perfs db continued: tests working with tiny DBs on all setup but the sqliteplugin
am@osimis.io
parents:
156
diff
changeset
|
26 pass |
df1f9946571c
perfs db continued: tests working with tiny DBs on all setup but the sqliteplugin
am@osimis.io
parents:
156
diff
changeset
|
27 |
df1f9946571c
perfs db continued: tests working with tiny DBs on all setup but the sqliteplugin
am@osimis.io
parents:
156
diff
changeset
|
28 def beforeEach(self): |
df1f9946571c
perfs db continued: tests working with tiny DBs on all setup but the sqliteplugin
am@osimis.io
parents:
156
diff
changeset
|
29 """ |
df1f9946571c
perfs db continued: tests working with tiny DBs on all setup but the sqliteplugin
am@osimis.io
parents:
156
diff
changeset
|
30 Code to execute before the execution of each repetition of a test. |
df1f9946571c
perfs db continued: tests working with tiny DBs on all setup but the sqliteplugin
am@osimis.io
parents:
156
diff
changeset
|
31 This code is not included in timings |
156 | 32 """ |
33 pass | |
34 | |
35 def test(self): | |
36 """ | |
37 Code whose execution time will be measured | |
38 """ | |
39 pass | |
40 | |
158
df1f9946571c
perfs db continued: tests working with tiny DBs on all setup but the sqliteplugin
am@osimis.io
parents:
156
diff
changeset
|
41 def afterEach(self): |
df1f9946571c
perfs db continued: tests working with tiny DBs on all setup but the sqliteplugin
am@osimis.io
parents:
156
diff
changeset
|
42 """ |
df1f9946571c
perfs db continued: tests working with tiny DBs on all setup but the sqliteplugin
am@osimis.io
parents:
156
diff
changeset
|
43 Code to execute after the execution of each repetition of a test. |
df1f9946571c
perfs db continued: tests working with tiny DBs on all setup but the sqliteplugin
am@osimis.io
parents:
156
diff
changeset
|
44 This code is not included in timings |
156 | 45 """ |
158
df1f9946571c
perfs db continued: tests working with tiny DBs on all setup but the sqliteplugin
am@osimis.io
parents:
156
diff
changeset
|
46 pass |
df1f9946571c
perfs db continued: tests working with tiny DBs on all setup but the sqliteplugin
am@osimis.io
parents:
156
diff
changeset
|
47 |
df1f9946571c
perfs db continued: tests working with tiny DBs on all setup but the sqliteplugin
am@osimis.io
parents:
156
diff
changeset
|
48 def afterAll(self): |
df1f9946571c
perfs db continued: tests working with tiny DBs on all setup but the sqliteplugin
am@osimis.io
parents:
156
diff
changeset
|
49 """ |
df1f9946571c
perfs db continued: tests working with tiny DBs on all setup but the sqliteplugin
am@osimis.io
parents:
156
diff
changeset
|
50 Code to execute after the execution of all repetitions of a test. |
df1f9946571c
perfs db continued: tests working with tiny DBs on all setup but the sqliteplugin
am@osimis.io
parents:
156
diff
changeset
|
51 This code is not included in timings |
156 | 52 """ |
53 pass | |
54 | |
55 def run(self) -> TestResult: | |
56 result = TestResult(self.name) | |
57 | |
158
df1f9946571c
perfs db continued: tests working with tiny DBs on all setup but the sqliteplugin
am@osimis.io
parents:
156
diff
changeset
|
58 self.beforeAll() |
df1f9946571c
perfs db continued: tests working with tiny DBs on all setup but the sqliteplugin
am@osimis.io
parents:
156
diff
changeset
|
59 |
156 | 60 for i in range(0, self.repeatCount): |
158
df1f9946571c
perfs db continued: tests working with tiny DBs on all setup but the sqliteplugin
am@osimis.io
parents:
156
diff
changeset
|
61 self.beforeEach() |
200 | 62 |
63 try: | |
64 with open('/proc/sys/vm/drop_caches', 'w') as f: | |
65 f.write('3') | |
66 except: | |
67 print('Please run this script as root to be able to flush the disk cache, and have reproducible runtimes') | |
68 | |
156 | 69 startTime = time.time() |
70 self.test() | |
71 endTime = time.time() | |
161 | 72 |
158
df1f9946571c
perfs db continued: tests working with tiny DBs on all setup but the sqliteplugin
am@osimis.io
parents:
156
diff
changeset
|
73 self.afterEach() |
156 | 74 |
75 result.add((endTime - startTime) * 1000) | |
76 | |
158
df1f9946571c
perfs db continued: tests working with tiny DBs on all setup but the sqliteplugin
am@osimis.io
parents:
156
diff
changeset
|
77 self.afterAll() |
156 | 78 result.compute() |
79 return result | |
80 | |
81 def __str__(self): | |
82 return "{name:<40}: {avg:>8.2f} ms {min:>8.2f} ms {max:>8.2f} ms".format( | |
83 name=self.name, | |
84 avg = self.averageTimeInMs, | |
85 min=self.minTimeInMs, | |
86 max=self.maxTimeInMs | |
200 | 87 ) |