annotate PerfsDb/TestConfig.py @ 178:c896ac762b9f

Added tag Orthanc-1.4.2 for changeset ef6b791c6d62
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 20 Sep 2018 14:22:38 +0200
parents a38c3cbc74dd
children c92e7191c912
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 typing
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
2 import subprocess
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
3 import os
158
df1f9946571c perfs db continued: tests working with tiny DBs on all setup but the sqliteplugin
am@osimis.io
parents: 156
diff changeset
4 import shutil
169
85a2074f8794 measuring orthanc startup time
am@osimis.io
parents: 164
diff changeset
5 import time
85a2074f8794 measuring orthanc startup time
am@osimis.io
parents: 164
diff changeset
6 from osimis_timer import TimeOut
156
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
7 from orthancRestApi import OrthancClient
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 from DbSize import DbSize
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
10 from DbType import DbType
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
11 from ConfigFileBuilder import ConfigFileBuilder
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
12 from DbServer import DbServer
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
13 from DbPopulator import DbPopulator
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
14 from Tests import *
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
15 from TestResult import TestResult
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 class TestConfig:
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
18
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
19 def __init__(self,
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
20 label: str,
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
21 dbSize: DbSize,
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
22 dbType: DbType=None,
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
23 dbServer: DbServer=None
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
24 ):
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 self._dbSize = dbSize
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
27 self._dbServer = dbServer
160
6995d5d12d88 filtering tests
am@osimis.io
parents: 158
diff changeset
28 self.label = label
156
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
29 self._port = None
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
30 self._orthancProcess = None
164
ff939d07989f saving results
am@osimis.io
parents: 163
diff changeset
31 self._repeatCount = 20
169
85a2074f8794 measuring orthanc startup time
am@osimis.io
parents: 164
diff changeset
32 self._results = []
156
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 if dbServer is not None:
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
35 self._dbType = dbServer.dbType
160
6995d5d12d88 filtering tests
am@osimis.io
parents: 158
diff changeset
36 self._dbServer.setLabel(self.label)
156
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
37 self._port = dbServer.port
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
38 else:
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
39 self._dbType = dbType
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 def setRepeatCount(self, repeatCount: int):
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
42 self._repeatCount = repeatCount
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
43
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
44 def launchDbServer(self):
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
45 if self._dbServer is not None:
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
46 self._dbServer.launch()
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
47
161
27b3b0df5f90 2 upload tests
am@osimis.io
parents: 160
diff changeset
48 def launchOrthanc(self, orthancPath, verboseEnabled: bool=False, traceEnabled: bool=False) -> bool:
156
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
49 orthanc = OrthancClient("http://127.0.0.1:8042")
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
50
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
51 print("Checking if Orthanc is already running")
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
52 if orthanc.isAlive():
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
53 print("Orthanc is already running")
158
df1f9946571c perfs db continued: tests working with tiny DBs on all setup but the sqliteplugin
am@osimis.io
parents: 156
diff changeset
54 return False
156
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
55
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
56 print("Launching Orthanc")
161
27b3b0df5f90 2 upload tests
am@osimis.io
parents: 160
diff changeset
57 runOrthancCommand = [
156
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
58 os.path.join(orthancPath, "Orthanc"),
160
6995d5d12d88 filtering tests
am@osimis.io
parents: 158
diff changeset
59 os.path.join(os.path.abspath(os.path.dirname(__file__)), "ConfigFiles", self.label + ".json"),
161
27b3b0df5f90 2 upload tests
am@osimis.io
parents: 160
diff changeset
60 ]
27b3b0df5f90 2 upload tests
am@osimis.io
parents: 160
diff changeset
61 if traceEnabled:
27b3b0df5f90 2 upload tests
am@osimis.io
parents: 160
diff changeset
62 runOrthancCommand.append("--trace")
27b3b0df5f90 2 upload tests
am@osimis.io
parents: 160
diff changeset
63 elif verboseEnabled:
27b3b0df5f90 2 upload tests
am@osimis.io
parents: 160
diff changeset
64 runOrthancCommand.append("--verbose")
27b3b0df5f90 2 upload tests
am@osimis.io
parents: 160
diff changeset
65
169
85a2074f8794 measuring orthanc startup time
am@osimis.io
parents: 164
diff changeset
66 startupTimeResult = TestResult("Startup time")
85a2074f8794 measuring orthanc startup time
am@osimis.io
parents: 164
diff changeset
67 startTime = time.time()
85a2074f8794 measuring orthanc startup time
am@osimis.io
parents: 164
diff changeset
68
161
27b3b0df5f90 2 upload tests
am@osimis.io
parents: 160
diff changeset
69 self._orthancProcess = subprocess.Popen(runOrthancCommand)
156
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
70
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
71 print("Waiting for Orthanc to start")
170
a38c3cbc74dd increasedd startup timeout
am@osimis.io
parents: 169
diff changeset
72 if not TimeOut.waitUntilCondition(lambda: orthanc.isAlive(), 5000, evaluateInterval = 0.1):
169
85a2074f8794 measuring orthanc startup time
am@osimis.io
parents: 164
diff changeset
73 print("Orthanc failed to start")
85a2074f8794 measuring orthanc startup time
am@osimis.io
parents: 164
diff changeset
74 exit(-2)
85a2074f8794 measuring orthanc startup time
am@osimis.io
parents: 164
diff changeset
75 endTime = time.time()
85a2074f8794 measuring orthanc startup time
am@osimis.io
parents: 164
diff changeset
76
85a2074f8794 measuring orthanc startup time
am@osimis.io
parents: 164
diff changeset
77 startupTimeResult.add((endTime - startTime) * 1000)
85a2074f8794 measuring orthanc startup time
am@osimis.io
parents: 164
diff changeset
78 startupTimeResult.compute()
85a2074f8794 measuring orthanc startup time
am@osimis.io
parents: 164
diff changeset
79 self._results.append(startupTimeResult)
156
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
80 print("Orthanc has started")
158
df1f9946571c perfs db continued: tests working with tiny DBs on all setup but the sqliteplugin
am@osimis.io
parents: 156
diff changeset
81 return True
156
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
82
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
83 def stopOrthanc(self):
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
84 if self._orthancProcess is not None:
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
85 self._orthancProcess.terminate()
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
86 self._orthancProcess.wait()
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
87
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
88 def initializeDb(self):
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
89 dbPopulator = DbPopulator(orthanc=OrthancClient("http://127.0.0.1:8042"), dbSize=self._dbSize)
163
53575aa2614b added stats during db creation
am@osimis.io
parents: 161
diff changeset
90 dbPopulator.populate(self.label)
156
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
91
160
6995d5d12d88 filtering tests
am@osimis.io
parents: 158
diff changeset
92 def runTests(self, tests: typing.List[Test]) -> typing.List[TestResult]:
156
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
93
160
6995d5d12d88 filtering tests
am@osimis.io
parents: 158
diff changeset
94 for test in tests:
156
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
95 test.setOrthancClient(OrthancClient("http://127.0.0.1:8042"))
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
96 test.setRepeatCount(self._repeatCount)
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
97 result = test.run()
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
98 print(str(result))
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
99
169
85a2074f8794 measuring orthanc startup time
am@osimis.io
parents: 164
diff changeset
100 self._results.append(result)
85a2074f8794 measuring orthanc startup time
am@osimis.io
parents: 164
diff changeset
101 return self._results
156
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
102
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
103 def clearDb(self):
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
104 if self._dbServer is not None:
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
105 self._dbServer.clear()
158
df1f9946571c perfs db continued: tests working with tiny DBs on all setup but the sqliteplugin
am@osimis.io
parents: 156
diff changeset
106
df1f9946571c perfs db continued: tests working with tiny DBs on all setup but the sqliteplugin
am@osimis.io
parents: 156
diff changeset
107 # clear storage (in case of Sqlite DB, it will also clear the DB)
160
6995d5d12d88 filtering tests
am@osimis.io
parents: 158
diff changeset
108 shutil.rmtree(os.path.join(os.path.abspath(os.path.dirname(__file__)), "Storages/{name}".format(name=self.label)), ignore_errors=True)
156
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
109
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
110 def generateOrthancConfigurationFile(self, pluginsPath: str):
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
111
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
112 ConfigFileBuilder.generate(
160
6995d5d12d88 filtering tests
am@osimis.io
parents: 158
diff changeset
113 outputPath=os.path.join(os.path.abspath(os.path.dirname(__file__)), "ConfigFiles/{name}.json".format(name=self.label)),
158
df1f9946571c perfs db continued: tests working with tiny DBs on all setup but the sqliteplugin
am@osimis.io
parents: 156
diff changeset
114 pluginsPath=pluginsPath,
160
6995d5d12d88 filtering tests
am@osimis.io
parents: 158
diff changeset
115 storagePath=os.path.join(os.path.abspath(os.path.dirname(__file__)), "Storages/{name}".format(name=self.label)),
156
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
116 dbType=self._dbType,
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
117 dbSize=self._dbSize,
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
118 port=self._port
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
119 )