Mercurial > hg > orthanc-tests
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 |
rev | line source |
---|---|
156 | 1 import typing |
161 | 2 import time |
163 | 3 import csv |
4 import os | |
156 | 5 from orthancRestApi import OrthancClient |
161 | 6 from TestResult import TestResult |
156 | 7 from DbSize import DbSize |
8 | |
9 class DbPopulator: | |
10 | |
11 def __init__(self, orthanc: OrthancClient, dbSize: DbSize): | |
12 self._orthanc = orthanc | |
13 self._dbSize = dbSize | |
14 self._sourceInstanceId = None | |
161 | 15 self._fileCounter = 0 |
16 | |
163 | 17 def populate(self, label: str): |
156 | 18 self._sourceInstanceId = self._orthanc.uploadDicomFile("../Database/DummyCT.dcm") |
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 | 24 seriesPerSmallStudy = 1 |
25 seriesPerLargeStudy = 2 | |
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 | 29 patientCount = 100 |
156 | 30 smallStudiesPerPatient = 2 |
31 largeStudiesPerPatient = 1 | |
161 | 32 seriesPerSmallStudy = 1 |
33 seriesPerLargeStudy = 2 | |
34 instancesPerSmallSeries = 1 | |
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 | 39 largeStudiesPerPatient = 2 |
40 seriesPerSmallStudy = 1 | |
41 seriesPerLargeStudy = 2 | |
42 instancesPerSmallSeries = 1 | |
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 | 45 patientCount = 10000 |
46 smallStudiesPerPatient = 2 | |
47 largeStudiesPerPatient = 2 | |
48 seriesPerSmallStudy = 1 | |
49 seriesPerLargeStudy = 2 | |
50 instancesPerSmallSeries = 1 | |
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 | 54 |
161 | 55 print("Will generate data for (approximately):") |
56 print("{n:>12} patients".format(n=patientCount)) | |
57 print("{n:>12} studies".format(n=patientCount * (smallStudiesPerPatient + largeStudiesPerPatient))) | |
58 print("{n:>12} instances".format(n=patientCount * (smallStudiesPerPatient * seriesPerSmallStudy * instancesPerSmallSeries + largeStudiesPerPatient * seriesPerLargeStudy * instancesPerLargeSeries))) | |
59 | |
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 | 62 |
165 | 63 # used in TestFindStudyByPatientId100Results |
64 for i in range(100): | |
65 self.createStudy(studyIndex=199000+i, patientIndex=99997, seriesCount=1, instancesPerSeries=1) | |
66 | |
156 | 67 # used in TestFindStudyByPatientId5Results |
68 self.createStudy(studyIndex=99994, patientIndex=99998, seriesCount=1, instancesPerSeries=1) | |
69 self.createStudy(studyIndex=99995, patientIndex=99998, seriesCount=1, instancesPerSeries=1) | |
70 self.createStudy(studyIndex=99996, patientIndex=99998, seriesCount=1, instancesPerSeries=1) | |
71 self.createStudy(studyIndex=99997, patientIndex=99998, seriesCount=1, instancesPerSeries=1) | |
72 self.createStudy(studyIndex=99998, patientIndex=99998, seriesCount=1, instancesPerSeries=1) | |
73 | |
74 # used in TestFindStudyByStudyDescription1Result | |
75 # used in TestFindStudyByPatientId1Result | |
162 | 76 # used in TestToolsFindStudyByStudyInstanceUID |
156 | 77 self.createStudy(studyIndex=99999, patientIndex=99999, seriesCount=1, instancesPerSeries=1) |
78 | |
165 | 79 with open(os.path.join(os.path.dirname(os.path.abspath(__file__)), "Results/db-init-" + label + ".csv"), 'w', newline='') as resultFile: |
163 | 80 resultWriter = csv.writer(resultFile) |
165 | 81 resultWriter.writerow(["#patientCount", "filesCount", "files/sec"]) |
163 | 82 # then, add data to make the DB "large" or "small" |
83 for patientIndex in range(0, patientCount): | |
84 studyIndex=0 | |
85 print("Generating data for patient " + str(patientIndex)) | |
86 fileCounterAtPatientStart = self._fileCounter | |
87 startTimePatient = time.time() | |
88 | |
89 for i in range(0, smallStudiesPerPatient): | |
90 print("Generating small study " + str(i)) | |
91 self.createStudy(studyIndex=studyIndex, patientIndex=patientIndex, seriesCount=seriesPerSmallStudy, instancesPerSeries=instancesPerSmallSeries) | |
92 studyIndex+=1 | |
93 for i in range(0, largeStudiesPerPatient): | |
94 print("Generating large study " + str(i)) | |
95 self.createStudy(studyIndex=studyIndex, patientIndex=patientIndex, seriesCount=seriesPerLargeStudy, instancesPerSeries=instancesPerLargeSeries) | |
96 studyIndex+=1 | |
97 | |
98 endTimePatient = time.time() | |
99 print("STATS: uploaded {n} files in {s:.2f} seconds; {x:.2f} files/sec".format( | |
100 n=self._fileCounter - fileCounterAtPatientStart, | |
101 s=endTimePatient - startTimePatient, | |
102 x=(self._fileCounter - fileCounterAtPatientStart)/(endTimePatient - startTimePatient) | |
103 )) | |
104 resultWriter.writerow([ | |
105 patientIndex, | |
165 | 106 self._fileCounter, |
163 | 107 (self._fileCounter - fileCounterAtPatientStart)/(endTimePatient - startTimePatient) |
108 ]) | |
109 resultFile.flush() | |
156 | 110 |
161 | 111 endTime = time.time() |
112 print("Generation completed. Elapsed time: {duration:.2f} sec".format(duration=endTime-startTime)) | |
113 print("Uploaded {n} files -> {p:.2f} files/sec".format(n=self._fileCounter, p=self._fileCounter/(endTime-startTime))) | |
156 | 114 |
115 def createStudy(self, studyIndex: int, patientIndex: int, seriesCount: int, instancesPerSeries: int): | |
116 for seriesIndex in range(0, seriesCount): | |
117 for instanceIndex in range(0, instancesPerSeries): | |
118 dicomFile = self.createDicomFile(patientIndex=patientIndex, studyIndex=studyIndex, seriesIndex=seriesIndex, instanceIndex=instanceIndex) | |
119 self._orthanc.uploadDicom(dicomFile) | |
120 | |
121 def createDicomFile(self, patientIndex: int, studyIndex: int, seriesIndex: int, instanceIndex: int) -> object: | |
161 | 122 self._fileCounter += 1 |
156 | 123 return self._orthanc.instances.modify( |
124 instanceId=self._sourceInstanceId, | |
125 replaceTags={ | |
126 "PatientName": "Patient-" + str(patientIndex), | |
127 "PatientID": str(patientIndex), | |
128 "StudyDescription": str(patientIndex) + "-" + str(studyIndex), | |
129 "SeriesDescription": str(patientIndex) + "-" + str(studyIndex) + "-" + str(seriesIndex), | |
130 "SOPInstanceUID": str(patientIndex) + "." + str(studyIndex) + "." + str(seriesIndex) + "." + str(instanceIndex), | |
131 "StudyInstanceUID": str(patientIndex) + "." + str(studyIndex), | |
132 "SeriesInstanceUID": str(patientIndex) + "." + str(studyIndex) + "." + str(seriesIndex), | |
133 "SeriesNumber": str(seriesIndex), | |
134 "InstanceNumber": str(instanceIndex) | |
135 }, | |
136 deleteOriginal=False | |
137 ) |