annotate PerfsDb/DbPopulator.py @ 656:7bfc8992ab8f Orthanc-1.12.4

updated CITATION.cff
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 05 Jun 2024 17:53:34 +0200
parents 1ff0d830bb87
children
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
165
1ff0d830bb87 find 100 results
am@osimis.io
parents: 163
diff changeset
63 # used in TestFindStudyByPatientId100Results
1ff0d830bb87 find 100 results
am@osimis.io
parents: 163
diff changeset
64 for i in range(100):
1ff0d830bb87 find 100 results
am@osimis.io
parents: 163
diff changeset
65 self.createStudy(studyIndex=199000+i, patientIndex=99997, seriesCount=1, instancesPerSeries=1)
1ff0d830bb87 find 100 results
am@osimis.io
parents: 163
diff changeset
66
156
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
67 # used in TestFindStudyByPatientId5Results
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
68 self.createStudy(studyIndex=99994, patientIndex=99998, seriesCount=1, instancesPerSeries=1)
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
69 self.createStudy(studyIndex=99995, patientIndex=99998, seriesCount=1, instancesPerSeries=1)
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
70 self.createStudy(studyIndex=99996, patientIndex=99998, seriesCount=1, instancesPerSeries=1)
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
71 self.createStudy(studyIndex=99997, patientIndex=99998, seriesCount=1, instancesPerSeries=1)
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
72 self.createStudy(studyIndex=99998, patientIndex=99998, seriesCount=1, instancesPerSeries=1)
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
73
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
74 # used in TestFindStudyByStudyDescription1Result
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
75 # used in TestFindStudyByPatientId1Result
162
dbc42a03ee75 more perfs db tests
am@osimis.io
parents: 161
diff changeset
76 # used in TestToolsFindStudyByStudyInstanceUID
156
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
77 self.createStudy(studyIndex=99999, patientIndex=99999, seriesCount=1, instancesPerSeries=1)
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
78
165
1ff0d830bb87 find 100 results
am@osimis.io
parents: 163
diff changeset
79 with open(os.path.join(os.path.dirname(os.path.abspath(__file__)), "Results/db-init-" + label + ".csv"), 'w', newline='') as resultFile:
163
53575aa2614b added stats during db creation
am@osimis.io
parents: 162
diff changeset
80 resultWriter = csv.writer(resultFile)
165
1ff0d830bb87 find 100 results
am@osimis.io
parents: 163
diff changeset
81 resultWriter.writerow(["#patientCount", "filesCount", "files/sec"])
163
53575aa2614b added stats during db creation
am@osimis.io
parents: 162
diff changeset
82 # then, add data to make the DB "large" or "small"
53575aa2614b added stats during db creation
am@osimis.io
parents: 162
diff changeset
83 for patientIndex in range(0, patientCount):
53575aa2614b added stats during db creation
am@osimis.io
parents: 162
diff changeset
84 studyIndex=0
53575aa2614b added stats during db creation
am@osimis.io
parents: 162
diff changeset
85 print("Generating data for patient " + str(patientIndex))
53575aa2614b added stats during db creation
am@osimis.io
parents: 162
diff changeset
86 fileCounterAtPatientStart = self._fileCounter
53575aa2614b added stats during db creation
am@osimis.io
parents: 162
diff changeset
87 startTimePatient = time.time()
53575aa2614b added stats during db creation
am@osimis.io
parents: 162
diff changeset
88
53575aa2614b added stats during db creation
am@osimis.io
parents: 162
diff changeset
89 for i in range(0, smallStudiesPerPatient):
53575aa2614b added stats during db creation
am@osimis.io
parents: 162
diff changeset
90 print("Generating small study " + str(i))
53575aa2614b added stats during db creation
am@osimis.io
parents: 162
diff changeset
91 self.createStudy(studyIndex=studyIndex, patientIndex=patientIndex, seriesCount=seriesPerSmallStudy, instancesPerSeries=instancesPerSmallSeries)
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 for i in range(0, largeStudiesPerPatient):
53575aa2614b added stats during db creation
am@osimis.io
parents: 162
diff changeset
94 print("Generating large study " + str(i))
53575aa2614b added stats during db creation
am@osimis.io
parents: 162
diff changeset
95 self.createStudy(studyIndex=studyIndex, patientIndex=patientIndex, seriesCount=seriesPerLargeStudy, instancesPerSeries=instancesPerLargeSeries)
53575aa2614b added stats during db creation
am@osimis.io
parents: 162
diff changeset
96 studyIndex+=1
53575aa2614b added stats during db creation
am@osimis.io
parents: 162
diff changeset
97
53575aa2614b added stats during db creation
am@osimis.io
parents: 162
diff changeset
98 endTimePatient = time.time()
53575aa2614b added stats during db creation
am@osimis.io
parents: 162
diff changeset
99 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
100 n=self._fileCounter - fileCounterAtPatientStart,
53575aa2614b added stats during db creation
am@osimis.io
parents: 162
diff changeset
101 s=endTimePatient - startTimePatient,
53575aa2614b added stats during db creation
am@osimis.io
parents: 162
diff changeset
102 x=(self._fileCounter - fileCounterAtPatientStart)/(endTimePatient - startTimePatient)
53575aa2614b added stats during db creation
am@osimis.io
parents: 162
diff changeset
103 ))
53575aa2614b added stats during db creation
am@osimis.io
parents: 162
diff changeset
104 resultWriter.writerow([
53575aa2614b added stats during db creation
am@osimis.io
parents: 162
diff changeset
105 patientIndex,
165
1ff0d830bb87 find 100 results
am@osimis.io
parents: 163
diff changeset
106 self._fileCounter,
163
53575aa2614b added stats during db creation
am@osimis.io
parents: 162
diff changeset
107 (self._fileCounter - fileCounterAtPatientStart)/(endTimePatient - startTimePatient)
53575aa2614b added stats during db creation
am@osimis.io
parents: 162
diff changeset
108 ])
53575aa2614b added stats during db creation
am@osimis.io
parents: 162
diff changeset
109 resultFile.flush()
156
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
110
161
27b3b0df5f90 2 upload tests
am@osimis.io
parents: 158
diff changeset
111 endTime = time.time()
27b3b0df5f90 2 upload tests
am@osimis.io
parents: 158
diff changeset
112 print("Generation completed. Elapsed time: {duration:.2f} sec".format(duration=endTime-startTime))
27b3b0df5f90 2 upload tests
am@osimis.io
parents: 158
diff changeset
113 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
114
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
115 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
116 for seriesIndex in range(0, seriesCount):
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
117 for instanceIndex in range(0, instancesPerSeries):
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
118 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
119 self._orthanc.uploadDicom(dicomFile)
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
120
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
121 def createDicomFile(self, patientIndex: int, studyIndex: int, seriesIndex: int, instanceIndex: int) -> object:
161
27b3b0df5f90 2 upload tests
am@osimis.io
parents: 158
diff changeset
122 self._fileCounter += 1
156
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
123 return self._orthanc.instances.modify(
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
124 instanceId=self._sourceInstanceId,
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
125 replaceTags={
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
126 "PatientName": "Patient-" + str(patientIndex),
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
127 "PatientID": str(patientIndex),
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
128 "StudyDescription": str(patientIndex) + "-" + str(studyIndex),
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
129 "SeriesDescription": str(patientIndex) + "-" + str(studyIndex) + "-" + str(seriesIndex),
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
130 "SOPInstanceUID": str(patientIndex) + "." + str(studyIndex) + "." + str(seriesIndex) + "." + str(instanceIndex),
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
131 "StudyInstanceUID": str(patientIndex) + "." + str(studyIndex),
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
132 "SeriesInstanceUID": str(patientIndex) + "." + str(studyIndex) + "." + str(seriesIndex),
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
133 "SeriesNumber": str(seriesIndex),
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
134 "InstanceNumber": str(instanceIndex)
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
135 },
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
136 deleteOriginal=False
f1a75985caa8 first Db test framework - work in progress
am@osimis.io
parents:
diff changeset
137 )