annotate PerfsDb/Test.py @ 156:f1a75985caa8

first Db test framework - work in progress
author am@osimis.io
date Thu, 16 Aug 2018 17:13:32 +0200
parents
children df1f9946571c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
156
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
1 import time
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
2 import statistics
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
3 from orthancRestApi import OrthancClient
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
4
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
5 from TestResult import TestResult
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
6
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
7 class Test:
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
8
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
9 def __init__(self, name: str):
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
10 self.name = name
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
11 self._orthanc = None
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
12 self.repeatCount = 10
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
13
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
14 def setOrthancClient(self, orthanc: OrthancClient):
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
15 self._orthanc = orthanc
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
16
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
17 def setRepeatCount(self, repeatCount: int):
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
18 self.repeatCount = repeatCount
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
19
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
20 def prepare(self):
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
21 """
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
22 Code to execute before the execution of a test; i.e: upload a file (not including in timings)
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
23 """
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
24 pass
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
25
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
26 def test(self):
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
27 """
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
28 Code whose execution time will be measured
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
29 """
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
30 pass
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
31
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
32 def cleanup(self):
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
33 """
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
34 Code to execute after the execution of a test; i.e: remove an instance (not including in timings)
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
35 """
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
36 pass
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
37
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
38 def run(self) -> TestResult:
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
39 result = TestResult(self.name)
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
40
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
41 for i in range(0, self.repeatCount):
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
42 self.prepare()
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
43 startTime = time.time()
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
44 self.test()
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
45 endTime = time.time()
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
46 self.cleanup()
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
47
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
48 result.add((endTime - startTime) * 1000)
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
49
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
50 result.compute()
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
51 return result
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
52
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
53 def __str__(self):
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
54 return "{name:<40}: {avg:>8.2f} ms {min:>8.2f} ms {max:>8.2f} ms".format(
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
55 name=self.name,
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
56 avg = self.averageTimeInMs,
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
57 min=self.minTimeInMs,
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
58 max=self.maxTimeInMs
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
59 )