annotate NewTests/Concurrency/test_concurrency.py @ 620:8ba9b20ae95f debug-pg-transactions

debug pg transactions tests
author Alain Mazy <am@osimis.io>
date Mon, 05 Feb 2024 22:32:39 +0100
parents 6ba2ff41ea52
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
592
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
1 import unittest
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
2 import time
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
3 import os
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
4 import threading
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
5 from helpers import OrthancTestCase, Helpers
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
6
598
b9ae7c59fee9 new concurrency test
Alain Mazy <am@osimis.io>
parents: 597
diff changeset
7 from orthanc_api_client import OrthancApiClient, ChangeType
b9ae7c59fee9 new concurrency test
Alain Mazy <am@osimis.io>
parents: 597
diff changeset
8 from orthanc_api_client import helpers as OrthancHelpers
b9ae7c59fee9 new concurrency test
Alain Mazy <am@osimis.io>
parents: 597
diff changeset
9
592
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
10 from orthanc_tools import OrthancTestDbPopulator
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
11
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
12 import pathlib
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
13 import subprocess
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
14 import glob
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
15 here = pathlib.Path(__file__).parent.resolve()
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
16
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
17
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
18 def worker_upload_folder(orthanc_root_url: str, folder: str, repeat: int, worker_id: int):
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
19 o = OrthancApiClient(orthanc_root_url)
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
20 for i in range(0, repeat):
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
21 o.upload_folder(folder, ignore_errors=True)
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
22
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
23 def worker_anonymize_study(orthanc_root_url: str, study_id: str, repeat: int, worker_id: int):
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
24 o = OrthancApiClient(orthanc_root_url)
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
25
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
26 for i in range(0, repeat):
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
27 o.studies.anonymize(orthanc_id=study_id, delete_original=False)
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
28
597
0cbefe7369ee concurrency: test changes
Alain Mazy <am@osimis.io>
parents: 596
diff changeset
29 def count_changes(changes, type: ChangeType):
0cbefe7369ee concurrency: test changes
Alain Mazy <am@osimis.io>
parents: 596
diff changeset
30 return len([c.change_type for c in changes if c.change_type==type])
0cbefe7369ee concurrency: test changes
Alain Mazy <am@osimis.io>
parents: 596
diff changeset
31
592
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
32 def worker_upload_delete_study_part(orthanc_root_url: str, folder: str, repeat: int, workers_count: int, worker_id: int):
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
33 o = OrthancApiClient(orthanc_root_url)
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
34
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
35 all_files = glob.glob(os.path.join(folder, '*.dcm'))
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
36
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
37 for i in range(0, repeat):
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
38 instances_ids = []
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
39
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
40 for i in range(0, len(all_files)):
598
b9ae7c59fee9 new concurrency test
Alain Mazy <am@osimis.io>
parents: 597
diff changeset
41 if i % workers_count == worker_id: # each thread takes a part
592
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
42 instances_ids.extend(o.upload_file(all_files[i]))
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
43
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
44 for instance_id in instances_ids:
594
27be80b4b1a9 fix test
Alain Mazy <am@osimis.io>
parents: 593
diff changeset
45 o.instances.delete(orthanc_id=instance_id, ignore_errors=True)
592
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
46
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
47
598
b9ae7c59fee9 new concurrency test
Alain Mazy <am@osimis.io>
parents: 597
diff changeset
48 def worker_upload_delete_test_dicoms(orthanc_root_url: str, files_count: int, worker_id: int):
b9ae7c59fee9 new concurrency test
Alain Mazy <am@osimis.io>
parents: 597
diff changeset
49 o = OrthancApiClient(orthanc_root_url)
b9ae7c59fee9 new concurrency test
Alain Mazy <am@osimis.io>
parents: 597
diff changeset
50
b9ae7c59fee9 new concurrency test
Alain Mazy <am@osimis.io>
parents: 597
diff changeset
51 instances_ids = []
b9ae7c59fee9 new concurrency test
Alain Mazy <am@osimis.io>
parents: 597
diff changeset
52 counter = 0
b9ae7c59fee9 new concurrency test
Alain Mazy <am@osimis.io>
parents: 597
diff changeset
53
b9ae7c59fee9 new concurrency test
Alain Mazy <am@osimis.io>
parents: 597
diff changeset
54 for i in range(0, files_count):
b9ae7c59fee9 new concurrency test
Alain Mazy <am@osimis.io>
parents: 597
diff changeset
55 counter += 1
b9ae7c59fee9 new concurrency test
Alain Mazy <am@osimis.io>
parents: 597
diff changeset
56 dicom_file = OrthancHelpers.generate_test_dicom_file(width=4, height=4,
b9ae7c59fee9 new concurrency test
Alain Mazy <am@osimis.io>
parents: 597
diff changeset
57 tags = {
b9ae7c59fee9 new concurrency test
Alain Mazy <am@osimis.io>
parents: 597
diff changeset
58 "PatientID" : f"{worker_id}",
b9ae7c59fee9 new concurrency test
Alain Mazy <am@osimis.io>
parents: 597
diff changeset
59 "StudyInstanceUID" : f"{worker_id}",
b9ae7c59fee9 new concurrency test
Alain Mazy <am@osimis.io>
parents: 597
diff changeset
60 "SeriesInstanceUID" : f"{worker_id}.{counter%10}"
b9ae7c59fee9 new concurrency test
Alain Mazy <am@osimis.io>
parents: 597
diff changeset
61 })
b9ae7c59fee9 new concurrency test
Alain Mazy <am@osimis.io>
parents: 597
diff changeset
62 instances_ids.extend(o.upload(dicom_file))
b9ae7c59fee9 new concurrency test
Alain Mazy <am@osimis.io>
parents: 597
diff changeset
63
b9ae7c59fee9 new concurrency test
Alain Mazy <am@osimis.io>
parents: 597
diff changeset
64 study_id = o.instances.get_parent_study_id(instances_ids[0])
b9ae7c59fee9 new concurrency test
Alain Mazy <am@osimis.io>
parents: 597
diff changeset
65 o.studies.delete(orthanc_id=study_id)
b9ae7c59fee9 new concurrency test
Alain Mazy <am@osimis.io>
parents: 597
diff changeset
66
b9ae7c59fee9 new concurrency test
Alain Mazy <am@osimis.io>
parents: 597
diff changeset
67
592
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
68 class TestConcurrency(OrthancTestCase):
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
69
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
70 @classmethod
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
71 def terminate(cls):
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
72
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
73 if Helpers.is_docker():
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
74 subprocess.run(["docker", "rm", "-f", "pg-server"])
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
75 else:
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
76 cls.pg_service_process.terminate()
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
77
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
78
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
79 @classmethod
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
80 def prepare(cls):
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
81 test_name = "Concurrency"
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
82 cls._storage_name = "concurrency"
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
83
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
84 print(f'-------------- preparing {test_name} tests')
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
85
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
86 cls.clear_storage(storage_name=cls._storage_name)
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
87
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
88 pg_hostname = "localhost"
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
89 if Helpers.is_docker():
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
90 pg_hostname = "pg-server"
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
91 cls.create_docker_network("concurrency")
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
92
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
93 config = {
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
94 "PostgreSQL" : {
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
95 "EnableStorage": False,
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
96 "EnableIndex": True,
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
97 "Host": pg_hostname,
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
98 "Port": 5432,
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
99 "Database": "postgres",
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
100 "Username": "postgres",
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
101 "Password": "postgres",
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
102 "IndexConnectionsCount": 10,
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
103 "MaximumConnectionRetries" : 2000,
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
104 "ConnectionRetryInterval" : 5,
620
8ba9b20ae95f debug pg transactions tests
Alain Mazy <am@osimis.io>
parents: 617
diff changeset
105 "TransactionMode": "READ COMMITTED"
617
Alain Mazy <am@osimis.io>
parents: 598
diff changeset
106 #"TransactionMode": "Serializable",
620
8ba9b20ae95f debug pg transactions tests
Alain Mazy <am@osimis.io>
parents: 617
diff changeset
107 # "EnableVerboseLogs": True
592
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
108 },
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
109 "AuthenticationEnabled": False,
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
110 "OverwriteInstances": True,
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
111 "JobsEngineThreadsCount" : {
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
112 "ResourceModification": 8
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
113 },
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
114 }
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
115
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
116 config_path = cls.generate_configuration(
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
117 config_name=f"{test_name}",
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
118 storage_name=cls._storage_name,
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
119 config=config,
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
120 plugins=Helpers.plugins
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
121 )
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
122
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
123 # launch the docker PG server
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
124 print('--------------- launching PostgreSQL server ------------------')
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
125
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
126 cls.pg_service_process = subprocess.Popen([
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
127 "docker", "run", "--rm",
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
128 "-p", "5432:5432",
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
129 "--network", "concurrency",
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
130 "--name", "pg-server",
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
131 "--env", "POSTGRES_HOST_AUTH_METHOD=trust",
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
132 "postgres:15"])
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
133 time.sleep(5)
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
134
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
135 if Helpers.break_before_preparation:
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
136 print(f"++++ It is now time to start your Orthanc under tests with configuration file '{config_path}' +++++")
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
137 input("Press Enter to continue")
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
138 else:
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
139 cls.launch_orthanc_under_tests(
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
140 config_name=f"{test_name}",
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
141 storage_name=cls._storage_name,
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
142 config=config,
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
143 plugins=Helpers.plugins,
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
144 docker_network="concurrency"
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
145 )
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
146
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
147 cls.o = OrthancApiClient(cls.o._root_url)
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
148 cls.o.wait_started()
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
149 cls.o.delete_all_content()
597
0cbefe7369ee concurrency: test changes
Alain Mazy <am@osimis.io>
parents: 596
diff changeset
150
0cbefe7369ee concurrency: test changes
Alain Mazy <am@osimis.io>
parents: 596
diff changeset
151 def check_is_empty(self):
620
8ba9b20ae95f debug pg transactions tests
Alain Mazy <am@osimis.io>
parents: 617
diff changeset
152 print("checking is empty")
8ba9b20ae95f debug pg transactions tests
Alain Mazy <am@osimis.io>
parents: 617
diff changeset
153
597
0cbefe7369ee concurrency: test changes
Alain Mazy <am@osimis.io>
parents: 596
diff changeset
154 self.assertEqual(0, len(self.o.studies.get_all_ids()))
0cbefe7369ee concurrency: test changes
Alain Mazy <am@osimis.io>
parents: 596
diff changeset
155 self.assertEqual(0, len(self.o.series.get_all_ids()))
0cbefe7369ee concurrency: test changes
Alain Mazy <am@osimis.io>
parents: 596
diff changeset
156 self.assertEqual(0, len(self.o.instances.get_all_ids()))
0cbefe7369ee concurrency: test changes
Alain Mazy <am@osimis.io>
parents: 596
diff changeset
157
620
8ba9b20ae95f debug pg transactions tests
Alain Mazy <am@osimis.io>
parents: 617
diff changeset
158 print("checking is empty (2)")
597
0cbefe7369ee concurrency: test changes
Alain Mazy <am@osimis.io>
parents: 596
diff changeset
159 stats = self.o.get_json("/statistics")
620
8ba9b20ae95f debug pg transactions tests
Alain Mazy <am@osimis.io>
parents: 617
diff changeset
160 print("checking is empty (3)")
597
0cbefe7369ee concurrency: test changes
Alain Mazy <am@osimis.io>
parents: 596
diff changeset
161 self.assertEqual(0, stats.get("CountPatients"))
0cbefe7369ee concurrency: test changes
Alain Mazy <am@osimis.io>
parents: 596
diff changeset
162 self.assertEqual(0, stats.get("CountStudies"))
0cbefe7369ee concurrency: test changes
Alain Mazy <am@osimis.io>
parents: 596
diff changeset
163 self.assertEqual(0, stats.get("CountSeries"))
0cbefe7369ee concurrency: test changes
Alain Mazy <am@osimis.io>
parents: 596
diff changeset
164 self.assertEqual(0, stats.get("CountInstances"))
0cbefe7369ee concurrency: test changes
Alain Mazy <am@osimis.io>
parents: 596
diff changeset
165 self.assertEqual(0, int(stats.get("TotalDiskSize")))
0cbefe7369ee concurrency: test changes
Alain Mazy <am@osimis.io>
parents: 596
diff changeset
166 # time.sleep(10000)
0cbefe7369ee concurrency: test changes
Alain Mazy <am@osimis.io>
parents: 596
diff changeset
167 self.assertTrue(self.is_storage_empty(self._storage_name))
0cbefe7369ee concurrency: test changes
Alain Mazy <am@osimis.io>
parents: 596
diff changeset
168
0cbefe7369ee concurrency: test changes
Alain Mazy <am@osimis.io>
parents: 596
diff changeset
169 # all changes shall have been deleted as well
0cbefe7369ee concurrency: test changes
Alain Mazy <am@osimis.io>
parents: 596
diff changeset
170 changes, last_change, done = self.o.get_changes(since=0, limit=100000)
0cbefe7369ee concurrency: test changes
Alain Mazy <am@osimis.io>
parents: 596
diff changeset
171 self.assertTrue(done)
0cbefe7369ee concurrency: test changes
Alain Mazy <am@osimis.io>
parents: 596
diff changeset
172 self.assertEqual(0, len(changes))
0cbefe7369ee concurrency: test changes
Alain Mazy <am@osimis.io>
parents: 596
diff changeset
173
592
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
174
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
175 def execute_workers(self, worker_func, worker_args, workers_count):
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
176 workers = []
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
177 for i in range(0, workers_count):
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
178 t = threading.Thread(target=worker_func, args=worker_args + (i, ))
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
179 workers.append(t)
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
180 t.start()
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
181
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
182 for t in workers:
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
183 t.join()
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
184
620
8ba9b20ae95f debug pg transactions tests
Alain Mazy <am@osimis.io>
parents: 617
diff changeset
185 # def test_concurrent_uploads_same_study(self):
8ba9b20ae95f debug pg transactions tests
Alain Mazy <am@osimis.io>
parents: 617
diff changeset
186 # self.o.delete_all_content()
8ba9b20ae95f debug pg transactions tests
Alain Mazy <am@osimis.io>
parents: 617
diff changeset
187 # self.clear_storage(storage_name=self._storage_name)
592
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
188
620
8ba9b20ae95f debug pg transactions tests
Alain Mazy <am@osimis.io>
parents: 617
diff changeset
189 # start_time = time.time()
8ba9b20ae95f debug pg transactions tests
Alain Mazy <am@osimis.io>
parents: 617
diff changeset
190 # workers_count = 20
8ba9b20ae95f debug pg transactions tests
Alain Mazy <am@osimis.io>
parents: 617
diff changeset
191 # repeat_count = 1
592
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
192
620
8ba9b20ae95f debug pg transactions tests
Alain Mazy <am@osimis.io>
parents: 617
diff changeset
193 # # massively reupload the same study multiple times with OverwriteInstances set to true
8ba9b20ae95f debug pg transactions tests
Alain Mazy <am@osimis.io>
parents: 617
diff changeset
194 # # Make sure the studies, series and instances are created only once
8ba9b20ae95f debug pg transactions tests
Alain Mazy <am@osimis.io>
parents: 617
diff changeset
195 # self.execute_workers(
8ba9b20ae95f debug pg transactions tests
Alain Mazy <am@osimis.io>
parents: 617
diff changeset
196 # worker_func=worker_upload_folder,
8ba9b20ae95f debug pg transactions tests
Alain Mazy <am@osimis.io>
parents: 617
diff changeset
197 # worker_args=(self.o._root_url, here / "../../Database/Knee", repeat_count,),
8ba9b20ae95f debug pg transactions tests
Alain Mazy <am@osimis.io>
parents: 617
diff changeset
198 # workers_count=workers_count)
592
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
199
620
8ba9b20ae95f debug pg transactions tests
Alain Mazy <am@osimis.io>
parents: 617
diff changeset
200 # elapsed = time.time() - start_time
8ba9b20ae95f debug pg transactions tests
Alain Mazy <am@osimis.io>
parents: 617
diff changeset
201 # print(f"TIMING test_concurrent_uploads_same_study with {workers_count} workers and {repeat_count}x repeat: {elapsed:.3f} s")
592
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
202
620
8ba9b20ae95f debug pg transactions tests
Alain Mazy <am@osimis.io>
parents: 617
diff changeset
203 # self.assertTrue(self.o.is_alive())
592
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
204
620
8ba9b20ae95f debug pg transactions tests
Alain Mazy <am@osimis.io>
parents: 617
diff changeset
205 # self.assertEqual(1, len(self.o.studies.get_all_ids()))
8ba9b20ae95f debug pg transactions tests
Alain Mazy <am@osimis.io>
parents: 617
diff changeset
206 # self.assertEqual(2, len(self.o.series.get_all_ids()))
8ba9b20ae95f debug pg transactions tests
Alain Mazy <am@osimis.io>
parents: 617
diff changeset
207 # self.assertEqual(50, len(self.o.instances.get_all_ids()))
592
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
208
620
8ba9b20ae95f debug pg transactions tests
Alain Mazy <am@osimis.io>
parents: 617
diff changeset
209 # stats = self.o.get_json("/statistics")
8ba9b20ae95f debug pg transactions tests
Alain Mazy <am@osimis.io>
parents: 617
diff changeset
210 # self.assertEqual(1, stats.get("CountPatients"))
8ba9b20ae95f debug pg transactions tests
Alain Mazy <am@osimis.io>
parents: 617
diff changeset
211 # self.assertEqual(1, stats.get("CountStudies"))
8ba9b20ae95f debug pg transactions tests
Alain Mazy <am@osimis.io>
parents: 617
diff changeset
212 # self.assertEqual(2, stats.get("CountSeries"))
8ba9b20ae95f debug pg transactions tests
Alain Mazy <am@osimis.io>
parents: 617
diff changeset
213 # self.assertEqual(50, stats.get("CountInstances"))
8ba9b20ae95f debug pg transactions tests
Alain Mazy <am@osimis.io>
parents: 617
diff changeset
214 # self.assertEqual(4118738, int(stats.get("TotalDiskSize")))
592
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
215
620
8ba9b20ae95f debug pg transactions tests
Alain Mazy <am@osimis.io>
parents: 617
diff changeset
216 # self.o.instances.delete(orthanc_ids=self.o.instances.get_all_ids())
592
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
217
620
8ba9b20ae95f debug pg transactions tests
Alain Mazy <am@osimis.io>
parents: 617
diff changeset
218 # self.check_is_empty()
592
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
219
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
220 def test_concurrent_anonymize_same_study(self):
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
221 self.o.delete_all_content()
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
222 self.clear_storage(storage_name=self._storage_name)
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
223
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
224 self.o.upload_folder(here / "../../Database/Knee")
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
225 study_id = self.o.studies.get_all_ids()[0]
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
226
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
227 start_time = time.time()
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
228 workers_count = 4
620
8ba9b20ae95f debug pg transactions tests
Alain Mazy <am@osimis.io>
parents: 617
diff changeset
229 repeat_count = 2
592
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
230
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
231 # massively anonymize the same study. This generates new studies and is a
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
232 # good way to simulate ingestion of new studies
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
233 self.execute_workers(
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
234 worker_func=worker_anonymize_study,
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
235 worker_args=(self.o._root_url, study_id, repeat_count,),
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
236 workers_count=workers_count)
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
237
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
238 elapsed = time.time() - start_time
593
f4f0e2d24a51 more pg-transactions tests
Alain Mazy <am@osimis.io>
parents: 592
diff changeset
239 print(f"TIMING test_concurrent_anonymize_same_study with {workers_count} workers and {repeat_count}x repeat: {elapsed:.3f} s")
592
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
240
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
241 self.assertTrue(self.o.is_alive())
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
242
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
243 self.assertEqual(1 + workers_count * repeat_count, len(self.o.studies.get_all_ids()))
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
244 self.assertEqual(2 * (1 + workers_count * repeat_count), len(self.o.series.get_all_ids()))
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
245 self.assertEqual(50 * (1 + workers_count * repeat_count), len(self.o.instances.get_all_ids()))
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
246
620
8ba9b20ae95f debug pg transactions tests
Alain Mazy <am@osimis.io>
parents: 617
diff changeset
247 print("get stats")
592
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
248 stats = self.o.get_json("/statistics")
620
8ba9b20ae95f debug pg transactions tests
Alain Mazy <am@osimis.io>
parents: 617
diff changeset
249 print("get stats (2)")
592
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
250 self.assertEqual(1 + workers_count * repeat_count, stats.get("CountPatients"))
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
251 self.assertEqual(1 + workers_count * repeat_count, stats.get("CountStudies"))
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
252 self.assertEqual(2 * (1 + workers_count * repeat_count), stats.get("CountSeries"))
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
253 self.assertEqual(50 * (1 + workers_count * repeat_count), stats.get("CountInstances"))
620
8ba9b20ae95f debug pg transactions tests
Alain Mazy <am@osimis.io>
parents: 617
diff changeset
254 print("get changes")
597
0cbefe7369ee concurrency: test changes
Alain Mazy <am@osimis.io>
parents: 596
diff changeset
255 changes, last_change, done = self.o.get_changes(since=0, limit=100000)
620
8ba9b20ae95f debug pg transactions tests
Alain Mazy <am@osimis.io>
parents: 617
diff changeset
256 print("get changes (2)")
597
0cbefe7369ee concurrency: test changes
Alain Mazy <am@osimis.io>
parents: 596
diff changeset
257 self.assertTrue(done)
0cbefe7369ee concurrency: test changes
Alain Mazy <am@osimis.io>
parents: 596
diff changeset
258
0cbefe7369ee concurrency: test changes
Alain Mazy <am@osimis.io>
parents: 596
diff changeset
259 self.assertEqual(1 + workers_count * repeat_count, count_changes(changes, ChangeType.NEW_PATIENT))
0cbefe7369ee concurrency: test changes
Alain Mazy <am@osimis.io>
parents: 596
diff changeset
260 self.assertEqual(1 + workers_count * repeat_count, count_changes(changes, ChangeType.NEW_STUDY))
0cbefe7369ee concurrency: test changes
Alain Mazy <am@osimis.io>
parents: 596
diff changeset
261 self.assertEqual(2 * (1 + workers_count * repeat_count), count_changes(changes, ChangeType.NEW_SERIES))
0cbefe7369ee concurrency: test changes
Alain Mazy <am@osimis.io>
parents: 596
diff changeset
262 self.assertEqual(50 * (1 + workers_count * repeat_count), count_changes(changes, ChangeType.NEW_INSTANCE))
592
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
263
593
f4f0e2d24a51 more pg-transactions tests
Alain Mazy <am@osimis.io>
parents: 592
diff changeset
264 start_time = time.time()
f4f0e2d24a51 more pg-transactions tests
Alain Mazy <am@osimis.io>
parents: 592
diff changeset
265
620
8ba9b20ae95f debug pg transactions tests
Alain Mazy <am@osimis.io>
parents: 617
diff changeset
266 print("deleting")
8ba9b20ae95f debug pg transactions tests
Alain Mazy <am@osimis.io>
parents: 617
diff changeset
267 all_instances_ids = self.o.instances.get_all_ids()
8ba9b20ae95f debug pg transactions tests
Alain Mazy <am@osimis.io>
parents: 617
diff changeset
268 print("deleting (2)")
8ba9b20ae95f debug pg transactions tests
Alain Mazy <am@osimis.io>
parents: 617
diff changeset
269 self.o.instances.delete(orthanc_ids=all_instances_ids)
8ba9b20ae95f debug pg transactions tests
Alain Mazy <am@osimis.io>
parents: 617
diff changeset
270 print("deleted")
592
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
271
593
f4f0e2d24a51 more pg-transactions tests
Alain Mazy <am@osimis.io>
parents: 592
diff changeset
272 elapsed = time.time() - start_time
f4f0e2d24a51 more pg-transactions tests
Alain Mazy <am@osimis.io>
parents: 592
diff changeset
273 print(f"TIMING test_concurrent_anonymize_same_study deletion took: {elapsed:.3f} s")
f4f0e2d24a51 more pg-transactions tests
Alain Mazy <am@osimis.io>
parents: 592
diff changeset
274
597
0cbefe7369ee concurrency: test changes
Alain Mazy <am@osimis.io>
parents: 596
diff changeset
275 self.check_is_empty()
592
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
276
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
277
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
278 def test_upload_delete_same_study_from_multiple_threads(self):
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
279 self.o.delete_all_content()
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
280 self.clear_storage(storage_name=self._storage_name)
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
281
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
282 start_time = time.time()
596
b1e1c7149a37 new PG upgrades tests
Alain Mazy <am@osimis.io>
parents: 594
diff changeset
283 overall_repeat = 10
594
27be80b4b1a9 fix test
Alain Mazy <am@osimis.io>
parents: 593
diff changeset
284
598
b9ae7c59fee9 new concurrency test
Alain Mazy <am@osimis.io>
parents: 597
diff changeset
285 for i in range(0, overall_repeat):
594
27be80b4b1a9 fix test
Alain Mazy <am@osimis.io>
parents: 593
diff changeset
286 workers_count = 5
596
b1e1c7149a37 new PG upgrades tests
Alain Mazy <am@osimis.io>
parents: 594
diff changeset
287 repeat_count = 3
594
27be80b4b1a9 fix test
Alain Mazy <am@osimis.io>
parents: 593
diff changeset
288
27be80b4b1a9 fix test
Alain Mazy <am@osimis.io>
parents: 593
diff changeset
289 # massively upload and delete the same study. Each worker is writing a part of the instances and deleting them.
27be80b4b1a9 fix test
Alain Mazy <am@osimis.io>
parents: 593
diff changeset
290 # We are trying to have multiple workers deleting the last instance of a study at the same time.
27be80b4b1a9 fix test
Alain Mazy <am@osimis.io>
parents: 593
diff changeset
291 self.execute_workers(
27be80b4b1a9 fix test
Alain Mazy <am@osimis.io>
parents: 593
diff changeset
292 worker_func=worker_upload_delete_study_part,
27be80b4b1a9 fix test
Alain Mazy <am@osimis.io>
parents: 593
diff changeset
293 worker_args=(self.o._root_url, here / "../../Database/Knee/T1", repeat_count, workers_count, ),
27be80b4b1a9 fix test
Alain Mazy <am@osimis.io>
parents: 593
diff changeset
294 workers_count=workers_count)
592
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
295
597
0cbefe7369ee concurrency: test changes
Alain Mazy <am@osimis.io>
parents: 596
diff changeset
296 self.check_is_empty()
592
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
297
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
298 elapsed = time.time() - start_time
596
b1e1c7149a37 new PG upgrades tests
Alain Mazy <am@osimis.io>
parents: 594
diff changeset
299 print(f"TIMING test_upload_delete_same_study_from_multiple_threads with {workers_count} workers and {repeat_count}x repeat ({overall_repeat}x): {elapsed:.3f} s")
592
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
300
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
301
598
b9ae7c59fee9 new concurrency test
Alain Mazy <am@osimis.io>
parents: 597
diff changeset
302 def test_upload_multiple_studies_from_multiple_threads(self):
b9ae7c59fee9 new concurrency test
Alain Mazy <am@osimis.io>
parents: 597
diff changeset
303 self.o.delete_all_content()
b9ae7c59fee9 new concurrency test
Alain Mazy <am@osimis.io>
parents: 597
diff changeset
304 self.clear_storage(storage_name=self._storage_name)
b9ae7c59fee9 new concurrency test
Alain Mazy <am@osimis.io>
parents: 597
diff changeset
305
b9ae7c59fee9 new concurrency test
Alain Mazy <am@osimis.io>
parents: 597
diff changeset
306 start_time = time.time()
b9ae7c59fee9 new concurrency test
Alain Mazy <am@osimis.io>
parents: 597
diff changeset
307 overall_repeat = 3
b9ae7c59fee9 new concurrency test
Alain Mazy <am@osimis.io>
parents: 597
diff changeset
308
b9ae7c59fee9 new concurrency test
Alain Mazy <am@osimis.io>
parents: 597
diff changeset
309 for i in range(0, overall_repeat):
b9ae7c59fee9 new concurrency test
Alain Mazy <am@osimis.io>
parents: 597
diff changeset
310 files_count = 25
b9ae7c59fee9 new concurrency test
Alain Mazy <am@osimis.io>
parents: 597
diff changeset
311 workers_count = 10
b9ae7c59fee9 new concurrency test
Alain Mazy <am@osimis.io>
parents: 597
diff changeset
312
b9ae7c59fee9 new concurrency test
Alain Mazy <am@osimis.io>
parents: 597
diff changeset
313 # massively upload and delete all studies from the test detabase. Each worker is writing all instances from a folder and then deletes them.
b9ae7c59fee9 new concurrency test
Alain Mazy <am@osimis.io>
parents: 597
diff changeset
314 # This test is only measuring performances.
b9ae7c59fee9 new concurrency test
Alain Mazy <am@osimis.io>
parents: 597
diff changeset
315 self.execute_workers(
b9ae7c59fee9 new concurrency test
Alain Mazy <am@osimis.io>
parents: 597
diff changeset
316 worker_func=worker_upload_delete_test_dicoms,
b9ae7c59fee9 new concurrency test
Alain Mazy <am@osimis.io>
parents: 597
diff changeset
317 worker_args=(self.o._root_url, files_count, ),
b9ae7c59fee9 new concurrency test
Alain Mazy <am@osimis.io>
parents: 597
diff changeset
318 workers_count=workers_count)
b9ae7c59fee9 new concurrency test
Alain Mazy <am@osimis.io>
parents: 597
diff changeset
319
b9ae7c59fee9 new concurrency test
Alain Mazy <am@osimis.io>
parents: 597
diff changeset
320 self.check_is_empty()
b9ae7c59fee9 new concurrency test
Alain Mazy <am@osimis.io>
parents: 597
diff changeset
321
b9ae7c59fee9 new concurrency test
Alain Mazy <am@osimis.io>
parents: 597
diff changeset
322 elapsed = time.time() - start_time
b9ae7c59fee9 new concurrency test
Alain Mazy <am@osimis.io>
parents: 597
diff changeset
323 print(f"TIMING test_upload_multiple_studies_from_multiple_threads with {workers_count} workers and {files_count} files and repeat {overall_repeat}x: {elapsed:.3f} s")
b9ae7c59fee9 new concurrency test
Alain Mazy <am@osimis.io>
parents: 597
diff changeset
324
592
6753d96dd71f new concurrency tests
Alain Mazy <am@osimis.io>
parents:
diff changeset
325 # transfers + dicomweb