|
2 | 2 |
|
3 | 3 | import inspect
|
4 | 4 | import time
|
5 |
| -import unittest |
| 5 | +from concurrent.futures import ThreadPoolExecutor |
| 6 | + |
| 7 | +try: |
| 8 | + import unittest2 as unittest |
| 9 | +except ImportError: |
| 10 | + import unittest |
| 11 | + |
6 | 12 |
|
7 | 13 | from prometheus_client.core import (
|
8 | 14 | CollectorRegistry,
|
@@ -124,6 +130,26 @@ def f():
|
124 | 130 | f()
|
125 | 131 | self.assertNotEqual(0, self.registry.get_sample_value('g'))
|
126 | 132 |
|
| 133 | + def test_function_decorator_multithread(self): |
| 134 | + self.assertEqual(0, self.registry.get_sample_value('g')) |
| 135 | + workers = 2 |
| 136 | + pool = ThreadPoolExecutor(max_workers=workers) |
| 137 | + |
| 138 | + @self.gauge.time() |
| 139 | + def f(duration): |
| 140 | + time.sleep(duration) |
| 141 | + |
| 142 | + expected_duration = 1 |
| 143 | + pool.submit(f, expected_duration) |
| 144 | + time.sleep(0.7 * expected_duration) |
| 145 | + pool.submit(f, expected_duration * 2) |
| 146 | + time.sleep(expected_duration) |
| 147 | + |
| 148 | + rounding_coefficient = 0.9 |
| 149 | + adjusted_expected_duration = expected_duration * rounding_coefficient |
| 150 | + self.assertLess(adjusted_expected_duration, self.registry.get_sample_value('g')) |
| 151 | + pool.shutdown(wait=True) |
| 152 | + |
127 | 153 | def test_time_block_decorator(self):
|
128 | 154 | self.assertEqual(0, self.registry.get_sample_value('g'))
|
129 | 155 | with self.gauge.time():
|
@@ -155,6 +181,55 @@ def f():
|
155 | 181 | f()
|
156 | 182 | self.assertEqual(1, self.registry.get_sample_value('s_count'))
|
157 | 183 |
|
| 184 | + def test_function_decorator_multithread(self): |
| 185 | + self.assertEqual(0, self.registry.get_sample_value('s_count')) |
| 186 | + summary2 = Summary('s2', 'help', registry=self.registry) |
| 187 | + |
| 188 | + workers = 3 |
| 189 | + duration = 0.1 |
| 190 | + pool = ThreadPoolExecutor(max_workers=workers) |
| 191 | + |
| 192 | + @self.summary.time() |
| 193 | + def f(): |
| 194 | + time.sleep(duration / 2) |
| 195 | + # Testing that different instances of timer do not interfere |
| 196 | + summary2.time()(lambda : time.sleep(duration / 2))() |
| 197 | + |
| 198 | + jobs = workers * 3 |
| 199 | + for i in range(jobs): |
| 200 | + pool.submit(f) |
| 201 | + pool.shutdown(wait=True) |
| 202 | + |
| 203 | + self.assertEqual(jobs, self.registry.get_sample_value('s_count')) |
| 204 | + |
| 205 | + rounding_coefficient = 0.9 |
| 206 | + total_expected_duration = jobs * duration * rounding_coefficient |
| 207 | + self.assertLess(total_expected_duration, self.registry.get_sample_value('s_sum')) |
| 208 | + self.assertLess(total_expected_duration / 2 , self.registry.get_sample_value('s2_sum')) |
| 209 | + |
| 210 | + def test_function_decorator_reentrancy(self): |
| 211 | + self.assertEqual(0, self.registry.get_sample_value('s_count')) |
| 212 | + |
| 213 | + iterations = 2 |
| 214 | + sleep = 0.1 |
| 215 | + |
| 216 | + @self.summary.time() |
| 217 | + def f(i=1): |
| 218 | + time.sleep(sleep) |
| 219 | + if i == iterations: |
| 220 | + return |
| 221 | + f(i+1) |
| 222 | + |
| 223 | + f() |
| 224 | + |
| 225 | + self.assertEqual(iterations, self.registry.get_sample_value('s_count')) |
| 226 | + |
| 227 | + # Arithmetic series with d == a_1 |
| 228 | + total_expected_duration = sleep * (iterations**2 + iterations) / 2 |
| 229 | + rounding_coefficient = 0.9 |
| 230 | + total_expected_duration *= rounding_coefficient |
| 231 | + self.assertLess(total_expected_duration, self.registry.get_sample_value('s_sum')) |
| 232 | + |
158 | 233 | def test_block_decorator(self):
|
159 | 234 | self.assertEqual(0, self.registry.get_sample_value('s_count'))
|
160 | 235 | with self.summary.time():
|
@@ -234,6 +309,27 @@ def f():
|
234 | 309 | self.assertEqual(1, self.registry.get_sample_value('h_count'))
|
235 | 310 | self.assertEqual(1, self.registry.get_sample_value('h_bucket', {'le': '+Inf'}))
|
236 | 311 |
|
| 312 | + def test_function_decorator_multithread(self): |
| 313 | + self.assertEqual(0, self.registry.get_sample_value('h_count')) |
| 314 | + workers = 3 |
| 315 | + duration = 0.1 |
| 316 | + pool = ThreadPoolExecutor(max_workers=workers) |
| 317 | + |
| 318 | + @self.histogram.time() |
| 319 | + def f(): |
| 320 | + time.sleep(duration) |
| 321 | + |
| 322 | + jobs = workers * 3 |
| 323 | + for i in range(jobs): |
| 324 | + pool.submit(f) |
| 325 | + pool.shutdown(wait=True) |
| 326 | + |
| 327 | + self.assertEqual(jobs, self.registry.get_sample_value('h_count')) |
| 328 | + |
| 329 | + rounding_coefficient = 0.9 |
| 330 | + total_expected_duration = jobs * duration * rounding_coefficient |
| 331 | + self.assertLess(total_expected_duration, self.registry.get_sample_value('h_sum')) |
| 332 | + |
237 | 333 | def test_block_decorator(self):
|
238 | 334 | self.assertEqual(0, self.registry.get_sample_value('h_count'))
|
239 | 335 | self.assertEqual(0, self.registry.get_sample_value('h_bucket', {'le': '+Inf'}))
|
|
0 commit comments