annotate PerfsDb/DbPopulator.py @ 164:ff939d07989f

saving results
author am@osimis.io
date Mon, 20 Aug 2018 15:50:47 +0200
parents 53575aa2614b
children 1ff0d830bb87
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
161
27b3b0df5f90 2 upload tests
am@osimis.io
parents: 158
diff changeset
2 import time
163
53575aa2614b added stats during db creation
am@osimis.io
parents: 162
diff changeset
3 import csv
53575aa2614b added stats during db creation
am@osimis.io
parents: 162
diff changeset
4 import os
156
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
5 from orthancRestApi import OrthancClient
161
27b3b0df5f90 2 upload tests
am@osimis.io
parents: 158
diff changeset
6 from TestResult import TestResult
156
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
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
9 class DbPopulator:
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
10
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
11 def __init__(self, orthanc: OrthancClient, dbSize: DbSize):
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
12 self._orthanc = orthanc
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
13 self._dbSize = dbSize
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
14 self._sourceInstanceId = None
161
27b3b0df5f90 2 upload tests
am@osimis.io
parents: 158
diff changeset
15 self._fileCounter = 0
27b3b0df5f90 2 upload tests
am@osimis.io
parents: 158
diff changeset
16
163
53575aa2614b added stats during db creation
am@osimis.io
parents: 162
diff changeset
17 def populate(self, label: str):
156
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
18 self._sourceInstanceId = self._orthanc.uploadDicomFile("../Database/DummyCT.dcm")
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
19
158
df1f9946571c perfs db continued: tests working with tiny DBs on all setup but the sqliteplugin
am@osimis.io
parents: 156
diff changeset
20 if self._dbSize == DbSize.Tiny:
df1f9946571c perfs db continued: tests working with tiny DBs on all setup but the sqliteplugin
am@osimis.io
parents: 156
diff changeset
21 patientCount = 1
df1f9946571c perfs db continued: tests working with tiny DBs on all setup but the sqliteplugin
am@osimis.io
parents: 156
diff changeset
22 smallStudiesPerPatient = 2
df1f9946571c perfs db continued: tests working with tiny DBs on all setup but the sqliteplugin
am@osimis.io
parents: 156
diff changeset
23 largeStudiesPerPatient = 1
161
27b3b0df5f90 2 upload tests
am@osimis.io
parents: 158
diff changeset
24 seriesPerSmallStudy = 1
27b3b0df5f90 2 upload tests
am@osimis.io
parents: 158
diff changeset
25 seriesPerLargeStudy = 2
27b3b0df5f90 2 upload tests
am@osimis.io
parents: 158
diff changeset
26 instancesPerSmallSeries = 1
158
df1f9946571c perfs db continued: tests working with tiny DBs on all setup but the sqliteplugin
am@osimis.io
parents: 156
diff changeset
27 instancesPerLargeSeries = 5
df1f9946571c perfs db continued: tests working with tiny DBs on all setup but the sqliteplugin
am@osimis.io
parents: 156
diff changeset
28 elif self._dbSize == DbSize.Small:
161
27b3b0df5f90 2 upload tests
am@osimis.io
parents: 158
diff changeset
29 patientCount = 100
156
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
30 smallStudiesPerPatient = 2
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
31 largeStudiesPerPatient = 1
161
27b3b0df5f90 2 upload tests
am@osimis.io
parents: 158
diff changeset
32 seriesPerSmallStudy = 1
27b3b0df5f90 2 upload tests
am@osimis.io
parents: 158
diff changeset
33 seriesPerLargeStudy = 2
27b3b0df5f90 2 upload tests
am@osimis.io
parents: 158
diff changeset
34 instancesPerSmallSeries = 1
27b3b0df5f90 2 upload tests
am@osimis.io
parents: 158
diff changeset
35 instancesPerLargeSeries = 30
158
df1f9946571c perfs db continued: tests working with tiny DBs on all setup but the sqliteplugin
am@osimis.io
parents: 156
diff changeset
36 elif self._dbSize == DbSize.Medium:
df1f9946571c perfs db continued: tests working with tiny DBs on all setup but the sqliteplugin
am@osimis.io
parents: 156
diff changeset
37 patientCount = 1000
df1f9946571c perfs db continued: tests working with tiny DBs on all setup but the sqliteplugin
am@osimis.io
parents: 156
diff changeset
38 smallStudiesPerPatient = 2
161
27b3b0df5f90 2 upload tests
am@osimis.io
parents: 158
diff changeset
39 largeStudiesPerPatient = 2
27b3b0df5f90 2 upload tests
am@osimis.io
parents: 158
diff changeset
40 seriesPerSmallStudy = 1
27b3b0df5f90 2 upload tests
am@osimis.io
parents: 158
diff changeset
41 seriesPerLargeStudy = 2
27b3b0df5f90 2 upload tests
am@osimis.io
parents: 158
diff changeset
42 instancesPerSmallSeries = 1
27b3b0df5f90 2 upload tests
am@osimis.io
parents: 158
diff changeset
43 instancesPerLargeSeries = 300
158
df1f9946571c perfs db continued: tests working with tiny DBs on all setup but the sqliteplugin
am@osimis.io
parents: 156
diff changeset
44 elif self._dbSize == DbSize.Large:
161
27b3b0df5f90 2 upload tests
am@osimis.io
parents: 158
diff changeset
45 patientCount = 10000
27b3b0df5f90 2 upload tests
am@osimis.io
parents: 158
diff changeset
46 smallStudiesPerPatient = 2
27b3b0df5f90 2 upload tests
am@osimis.io
parents: 158
diff changeset
47 largeStudiesPerPatient = 2
27b3b0df5f90 2 upload tests
am@osimis.io
parents: 158
diff changeset
48 seriesPerSmallStudy = 1
27b3b0df5f90 2 upload tests
am@osimis.io
parents: 158
diff changeset
49 seriesPerLargeStudy = 2
27b3b0df5f90 2 upload tests
am@osimis.io
parents: 158
diff changeset
50 instancesPerSmallSeries = 1
27b3b0df5f90 2 upload tests
am@osimis.io
parents: 158
diff changeset
51 instancesPerLargeSeries = 300
158
df1f9946571c perfs db continued: tests working with tiny DBs on all setup but the sqliteplugin
am@osimis.io
parents: 156
diff changeset
52 else:
df1f9946571c perfs db continued: tests working with tiny DBs on all setup but the sqliteplugin
am@osimis.io
parents: 156
diff changeset
53 raise NotImplementedError
156
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
54
161
27b3b0df5f90 2 upload tests
am@osimis.io
parents: 158
diff changeset
55 print("Will generate data for (approximately):")
27b3b0df5f90 2 upload tests
am@osimis.io
parents: 158
diff changeset
56 print("{n:>12} patients".format(n=patientCount))
27b3b0df5f90 2 upload tests
am@osimis.io
parents: 158
diff changeset
57 print("{n:>12} studies".format(n=patientCount * (smallStudiesPerPatient + largeStudiesPerPatient)))
27b3b0df5f90 2 upload tests
am@osimis.io
parents: 158
diff changeset
58 print("{n:>12} instances".format(n=patientCount * (smallStudiesPerPatient * seriesPerSmallStudy * instancesPerSmallSeries + largeStudiesPerPatient * seriesPerLargeStudy * instancesPerLargeSeries)))
27b3b0df5f90 2 upload tests
am@osimis.io
parents: 158
diff changeset
59
27b3b0df5f90 2 upload tests
am@osimis.io
parents: 158
diff changeset
60 startTime = time.time()
158
df1f9946571c perfs db continued: tests working with tiny DBs on all setup but the sqliteplugin
am@osimis.io
parents: 156
diff changeset
61 # first add data that are the same in small and large DBs (and that can be used in tests for comparing the same things !!)
156
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 # used in TestFindStudyByPatientId5Results
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
64 self.createStudy(studyIndex=99994, patientIndex=99998, seriesCount=1, instancesPerSeries=1)
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
65 self.createStudy(studyIndex=99995, patientIndex=99998, seriesCount=1, instancesPerSeries=1)
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
66 self.createStudy(studyIndex=99996, patientIndex=99998, seriesCount=1, instancesPerSeries=1)
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
67 self.createStudy(studyIndex=99997, patientIndex=99998, seriesCount=1, instancesPerSeries=1)
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
68 self.createStudy(studyIndex=99998, patientIndex=99998, seriesCount=1, instancesPerSeries=1)
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
69
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
70 # used in TestFindStudyByStudyDescription1Result
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
71 # used in TestFindStudyByPatientId1Result
162
dbc42a03ee75 more perfs db tests
am@osimis.io
parents: 161
diff changeset
72 # used in TestToolsFindStudyByStudyInstanceUID
156
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
73 self.createStudy(studyIndex=99999, patientIndex=99999, seriesCount=1, instancesPerSeries=1)
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
74
163
53575aa2614b added stats during db creation
am@osimis.io
parents: 162
diff changeset
75 with open(os.path.join(os.path.dirname(os.path.abspath(__file__)), "Results/db-init-" + label), 'w', newline='') as resultFile:
53575aa2614b added stats during db creation
am@osimis.io
parents: 162
diff changeset
76 resultWriter = csv.writer(resultFile)
53575aa2614b added stats during db creation
am@osimis.io
parents: 162
diff changeset
77 resultWriter.writerow(["patientCount", "filesCount", "duration", "files/sec"])
53575aa2614b added stats during db creation
am@osimis.io
parents: 162
diff changeset
78 # then, add data to make the DB "large" or "small"
53575aa2614b added stats during db creation
am@osimis.io
parents: 162
diff changeset
79 for patientIndex in range(0, patientCount):
53575aa2614b added stats during db creation
am@osimis.io
parents: 162
diff changeset
80 studyIndex=0
53575aa2614b added stats during db creation
am@osimis.io
parents: 162
diff changeset
81 print("Generating data for patient " + str(patientIndex))
53575aa2614b added stats during db creation
am@osimis.io
parents: 162
diff changeset
82 fileCounterAtPatientStart = self._fileCounter
53575aa2614b added stats during db creation
am@osimis.io
parents: 162
diff changeset
83 startTimePatient = time.time()
53575aa2614b added stats during db creation
am@osimis.io
parents: 162
diff changeset
84
53575aa2614b added stats during db creation
am@osimis.io
parents: 162
diff changeset
85 for i in range(0, smallStudiesPerPatient):
53575aa2614b added stats during db creation
am@osimis.io
parents: 162
diff changeset
86 print("Generating small study " + str(i))
53575aa2614b added stats during db creation
am@osimis.io
parents: 162
diff changeset
87 self.createStudy(studyIndex=studyIndex, patientIndex=patientIndex, seriesCount=seriesPerSmallStudy, instancesPerSeries=instancesPerSmallSeries)
53575aa2614b added stats during db creation
am@osimis.io
parents: 162
diff changeset
88 studyIndex+=1
53575aa2614b added stats during db creation
am@osimis.io
parents: 162
diff changeset
89 for i in range(0, largeStudiesPerPatient):
53575aa2614b added stats during db creation
am@osimis.io
parents: 162
diff changeset
90 print("Generating large study " + str(i))
53575aa2614b added stats during db creation
am@osimis.io
parents: 162
diff changeset
91 self.createStudy(studyIndex=studyIndex, patientIndex=patientIndex, seriesCount=seriesPerLargeStudy, instancesPerSeries=instancesPerLargeSeries)
53575aa2614b added stats during db creation
am@osimis.io
parents: 162
diff changeset
92 studyIndex+=1
53575aa2614b added stats during db creation
am@osimis.io
parents: 162
diff changeset
93
53575aa2614b added stats during db creation
am@osimis.io
parents: 162
diff changeset
94 endTimePatient = time.time()
53575aa2614b added stats during db creation
am@osimis.io
parents: 162
diff changeset
95 print("STATS: uploaded {n} files in {s:.2f} seconds; {x:.2f} files/sec".format(
53575aa2614b added stats during db creation
am@osimis.io
parents: 162
diff changeset
96 n=self._fileCounter - fileCounterAtPatientStart,
53575aa2614b added stats during db creation
am@osimis.io
parents: 162
diff changeset
97 s=endTimePatient - startTimePatient,
53575aa2614b added stats during db creation
am@osimis.io
parents: 162
diff changeset
98 x=(self._fileCounter - fileCounterAtPatientStart)/(endTimePatient - startTimePatient)
53575aa2614b added stats during db creation
am@osimis.io
parents: 162
diff changeset
99 ))
53575aa2614b added stats during db creation
am@osimis.io
parents: 162
diff changeset
100 resultWriter.writerow([
53575aa2614b added stats during db creation
am@osimis.io
parents: 162
diff changeset
101 patientIndex,
53575aa2614b added stats during db creation
am@osimis.io
parents: 162
diff changeset
102 self._fileCounter - fileCounterAtPatientStart,
53575aa2614b added stats during db creation
am@osimis.io
parents: 162
diff changeset
103 endTimePatient - startTimePatient,
53575aa2614b added stats during db creation
am@osimis.io
parents: 162
diff changeset
104 (self._fileCounter - fileCounterAtPatientStart)/(endTimePatient - startTimePatient)
53575aa2614b added stats during db creation
am@osimis.io
parents: 162
diff changeset
105 ])
53575aa2614b added stats during db creation
am@osimis.io
parents: 162
diff changeset
106 resultFile.flush()
156
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
107
161
27b3b0df5f90 2 upload tests
am@osimis.io
parents: 158
diff changeset
108 endTime = time.time()
27b3b0df5f90 2 upload tests
am@osimis.io
parents: 158
diff changeset
109 print("Generation completed. Elapsed time: {duration:.2f} sec".format(duration=endTime-startTime))
27b3b0df5f90 2 upload tests
am@osimis.io
parents: 158
diff changeset
110 print("Uploaded {n} files -> {p:.2f} files/sec".format(n=self._fileCounter, p=self._fileCounter/(endTime-startTime)))
156
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 def createStudy(self, studyIndex: int, patientIndex: int, seriesCount: int, instancesPerSeries: int):
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
113 for seriesIndex in range(0, seriesCount):
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
114 for instanceIndex in range(0, instancesPerSeries):
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
115 dicomFile = self.createDicomFile(patientIndex=patientIndex, studyIndex=studyIndex, seriesIndex=seriesIndex, instanceIndex=instanceIndex)
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
116 self._orthanc.uploadDicom(dicomFile)
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
117
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
118 def createDicomFile(self, patientIndex: int, studyIndex: int, seriesIndex: int, instanceIndex: int) -> object:
161
27b3b0df5f90 2 upload tests
am@osimis.io
parents: 158
diff changeset
119 self._fileCounter += 1
156
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
120 return self._orthanc.instances.modify(
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
121 instanceId=self._sourceInstanceId,
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
122 replaceTags={
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
123 "PatientName": "Patient-" + str(patientIndex),
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
124 "PatientID": str(patientIndex),
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
125 "StudyDescription": str(patientIndex) + "-" + str(studyIndex),
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
126 "SeriesDescription": str(patientIndex) + "-" + str(studyIndex) + "-" + str(seriesIndex),
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
127 "SOPInstanceUID": str(patientIndex) + "." + str(studyIndex) + "." + str(seriesIndex) + "." + str(instanceIndex),
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
128 "StudyInstanceUID": str(patientIndex) + "." + str(studyIndex),
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
129 "SeriesInstanceUID": str(patientIndex) + "." + str(studyIndex) + "." + str(seriesIndex),
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
130 "SeriesNumber": str(seriesIndex),
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
131 "InstanceNumber": str(instanceIndex)
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
132 },
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
133 deleteOriginal=False
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
134 )