annotate PerfsDb/TestConfig.py @ 158:df1f9946571c

perfs db continued: tests working with tiny DBs on all setup but the sqliteplugin
author am@osimis.io
date Fri, 17 Aug 2018 11:58:26 +0200
parents f1a75985caa8
children 6995d5d12d88
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
156
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
5 from orthancRestApi import OrthancClient
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 from DbSize import DbSize
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
8 from DbType import DbType
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
9 from ConfigFileBuilder import ConfigFileBuilder
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
10 from DbServer import DbServer
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
11 from DbPopulator import DbPopulator
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
12 from Tests import *
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
13 from TestResult import TestResult
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
14
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
15 class TestConfig:
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 __init__(self,
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
18 label: str,
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
19 dbSize: DbSize,
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
20 dbType: DbType=None,
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
21 dbServer: DbServer=None
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
22 ):
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 self._dbSize = dbSize
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
25 self._dbServer = dbServer
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
26 self._label = label
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
27 self._port = None
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
28 self._name = "unknown"
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
29 self._orthancProcess = None
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
30 self._repeatCount = 10
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 if dbServer is not None:
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
33 self._dbType = dbServer.dbType
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
34 self._dbServer.setLabel(self._label)
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
35 self._port = dbServer.port
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
36 else:
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
37 self._dbType = dbType
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
38
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
39 def setName(self, name: str):
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
40 self._name = name
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
41
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
42 def setRepeatCount(self, repeatCount: int):
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
43 self._repeatCount = repeatCount
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
44
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
45 def launchDbServer(self):
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
46 if self._dbServer is not None:
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
47 self._dbServer.launch()
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
48
158
df1f9946571c perfs db continued: tests working with tiny DBs on all setup but the sqliteplugin
am@osimis.io
parents: 156
diff changeset
49 def launchOrthanc(self, orthancPath) -> bool:
156
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
50 orthanc = OrthancClient("http://127.0.0.1:8042")
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
51
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
52 print("Checking if Orthanc is already running")
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
53 if orthanc.isAlive():
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
54 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
55 return False
156
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
56
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
57 print("Launching Orthanc")
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
58 self._orthancProcess = subprocess.Popen([
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
59 os.path.join(orthancPath, "Orthanc"),
158
df1f9946571c perfs db continued: tests working with tiny DBs on all setup but the sqliteplugin
am@osimis.io
parents: 156
diff changeset
60 os.path.join(os.path.abspath(os.path.dirname(__file__)), "ConfigFiles", self._name + ".json"),
156
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
61 ])
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
62
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
63 print("Waiting for Orthanc to start")
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
64 orthanc.waitStarted(timeout=30)
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
65 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
66 return True
156
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
67
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
68 def stopOrthanc(self):
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
69 if self._orthancProcess is not None:
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
70 self._orthancProcess.terminate()
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
71 self._orthancProcess.wait()
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
72
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
73 def initializeDb(self):
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
74 dbPopulator = DbPopulator(orthanc=OrthancClient("http://127.0.0.1:8042"), dbSize=self._dbSize)
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
75 dbPopulator.populate()
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
76
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
77 def runTests(self) -> typing.List[TestResult]:
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
78 allTests = [
158
df1f9946571c perfs db continued: tests working with tiny DBs on all setup but the sqliteplugin
am@osimis.io
parents: 156
diff changeset
79 TestStatistics(),
156
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
80 TestFindStudyByStudyDescription1Result(),
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
81 TestFindStudyByPatientId1Result(),
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
82 TestFindStudyByStudyDescription0Results(),
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
83 TestFindStudyByPatientId0Results(),
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
84 TestFindStudyByPatientId5Results(),
158
df1f9946571c perfs db continued: tests working with tiny DBs on all setup but the sqliteplugin
am@osimis.io
parents: 156
diff changeset
85 TestUploadFile(),
156
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
86 ]
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 results = []
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
89 for test in allTests:
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
90 test.setOrthancClient(OrthancClient("http://127.0.0.1:8042"))
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
91 test.setRepeatCount(self._repeatCount)
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
92 result = test.run()
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
93 print(str(result))
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
94
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
95 results.append(result)
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
96 return results
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
97
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
98 def clearDb(self):
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
99 if self._dbServer is not None:
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
100 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
101
df1f9946571c perfs db continued: tests working with tiny DBs on all setup but the sqliteplugin
am@osimis.io
parents: 156
diff changeset
102 # clear storage (in case of Sqlite DB, it will also clear the DB)
df1f9946571c perfs db continued: tests working with tiny DBs on all setup but the sqliteplugin
am@osimis.io
parents: 156
diff changeset
103 shutil.rmtree(os.path.join(os.path.abspath(os.path.dirname(__file__)), "Storages/{name}".format(name=self._name)), ignore_errors=True)
156
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
104
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
105 def generateOrthancConfigurationFile(self, pluginsPath: str):
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
106
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
107 ConfigFileBuilder.generate(
158
df1f9946571c perfs db continued: tests working with tiny DBs on all setup but the sqliteplugin
am@osimis.io
parents: 156
diff changeset
108 outputPath=os.path.join(os.path.abspath(os.path.dirname(__file__)), "ConfigFiles/{name}.json".format(name=self._name)),
df1f9946571c perfs db continued: tests working with tiny DBs on all setup but the sqliteplugin
am@osimis.io
parents: 156
diff changeset
109 pluginsPath=pluginsPath,
df1f9946571c perfs db continued: tests working with tiny DBs on all setup but the sqliteplugin
am@osimis.io
parents: 156
diff changeset
110 storagePath=os.path.join(os.path.abspath(os.path.dirname(__file__)), "Storages/{name}".format(name=self._name)),
156
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
111 dbType=self._dbType,
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
112 dbSize=self._dbSize,
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
113 port=self._port
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
114 )