|  | 
| 5 | 5 | import unittest | 
| 6 | 6 | import warnings | 
| 7 | 7 | 
 | 
|  | 8 | +import pytest | 
|  | 9 | + | 
| 8 | 10 | from prometheus_client import mmap_dict, values | 
| 9 | 11 | from prometheus_client.core import ( | 
| 10 | 12 |     CollectorRegistry, Counter, Gauge, Histogram, Sample, Summary, | 
| 11 | 13 | ) | 
|  | 14 | +import prometheus_client.multiprocess as multiprocess | 
| 12 | 15 | from prometheus_client.multiprocess import ( | 
| 13 | 16 |     mark_process_dead, MultiProcessCollector, | 
| 14 | 17 | ) | 
| @@ -398,6 +401,61 @@ def test_remove_clear_warning(self): | 
| 398 | 401 |             assert "Clearing labels has not been implemented" in str(w[-1].message) | 
| 399 | 402 | 
 | 
| 400 | 403 | 
 | 
|  | 404 | +@pytest.fixture | 
|  | 405 | +def tempdir(): | 
|  | 406 | +    tempdir = tempfile.mkdtemp() | 
|  | 407 | +    os.environ['PROMETHEUS_MULTIPROC_DIR'] = tempdir | 
|  | 408 | +    values.ValueClass = MultiProcessValue(lambda: 123) | 
|  | 409 | +    yield tempdir | 
|  | 410 | +    del os.environ['PROMETHEUS_MULTIPROC_DIR'] | 
|  | 411 | +    shutil.rmtree(tempdir) | 
|  | 412 | +    values.ValueClass = MutexValue | 
|  | 413 | + | 
|  | 414 | + | 
|  | 415 | +@pytest.fixture | 
|  | 416 | +def registry() -> CollectorRegistry: | 
|  | 417 | +    return CollectorRegistry() | 
|  | 418 | + | 
|  | 419 | + | 
|  | 420 | +@pytest.fixture | 
|  | 421 | +def collector(tempdir, registry) -> MultiProcessCollector: | 
|  | 422 | +    return MultiProcessCollector(registry) | 
|  | 423 | + | 
|  | 424 | + | 
|  | 425 | +@pytest.fixture | 
|  | 426 | +def no_speedup(): | 
|  | 427 | +    tmp = multiprocess._speedups | 
|  | 428 | +    multiprocess._speedups = False | 
|  | 429 | +    yield tmp | 
|  | 430 | +    multiprocess._speedups = tmp | 
|  | 431 | + | 
|  | 432 | + | 
|  | 433 | +def setup_benchmark(): | 
|  | 434 | +    labels = {i: i for i in 'abcd'} | 
|  | 435 | +    for pid in range(1000): | 
|  | 436 | +        values.ValueClass = MultiProcessValue(lambda: pid) | 
|  | 437 | + | 
|  | 438 | +        c = Counter('c', 'help', labelnames=labels.keys(), registry=None) | 
|  | 439 | +        g = Gauge('g', 'help', labelnames=labels.keys(), registry=None) | 
|  | 440 | +        h = Histogram('h', 'help', labelnames=labels.keys(), registry=None) | 
|  | 441 | + | 
|  | 442 | +        c.labels(**labels).inc(1) | 
|  | 443 | +        g.labels(**labels).set(1) | 
|  | 444 | +        h.labels(**labels).observe(1) | 
|  | 445 | + | 
|  | 446 | + | 
|  | 447 | +def test_native_collect_performance(benchmark, collector, no_speedup): | 
|  | 448 | +    setup_benchmark() | 
|  | 449 | +    benchmark(collector.collect) | 
|  | 450 | + | 
|  | 451 | + | 
|  | 452 | +def test_speedup_collect_performance(benchmark, collector): | 
|  | 453 | +    if not multiprocess._speedups: | 
|  | 454 | +        pytest.skip("prometheus_client_python_speedups not installed") | 
|  | 455 | +    setup_benchmark() | 
|  | 456 | +    benchmark(collector.collect) | 
|  | 457 | + | 
|  | 458 | + | 
| 401 | 459 | class TestMmapedDict(unittest.TestCase): | 
| 402 | 460 |     def setUp(self): | 
| 403 | 461 |         fd, self.tempfile = tempfile.mkstemp() | 
|  | 
0 commit comments