|
| 1 | +"""Tests for the benchmarks API endpoints.""" |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | + |
| 6 | +@pytest.mark.asyncio |
| 7 | +async def test_all_benchmark_names_empty(client): |
| 8 | + response = await client.get("/api/benchmarks") |
| 9 | + assert response.status_code == 200 |
| 10 | + assert response.json() == [] |
| 11 | + |
| 12 | + |
| 13 | +@pytest.mark.asyncio |
| 14 | +async def test_all_benchmark_names(client, sample_benchmark_result): |
| 15 | + response = await client.get("/api/benchmarks") |
| 16 | + assert response.status_code == 200 |
| 17 | + data = response.json() |
| 18 | + assert "json_dumps" in data |
| 19 | + |
| 20 | + |
| 21 | +@pytest.mark.asyncio |
| 22 | +async def test_filtered_benchmark_names(client, sample_benchmark_result): |
| 23 | + response = await client.get( |
| 24 | + "/api/benchmark-names", |
| 25 | + params={ |
| 26 | + "environment_id": "linux-x86_64", |
| 27 | + "binary_id": "default", |
| 28 | + "python_major": 3, |
| 29 | + "python_minor": 14, |
| 30 | + }, |
| 31 | + ) |
| 32 | + assert response.status_code == 200 |
| 33 | + data = response.json() |
| 34 | + assert "json_dumps" in data |
| 35 | + |
| 36 | + |
| 37 | +@pytest.mark.asyncio |
| 38 | +async def test_filtered_benchmark_names_no_match(client, sample_benchmark_result): |
| 39 | + response = await client.get( |
| 40 | + "/api/benchmark-names", |
| 41 | + params={ |
| 42 | + "environment_id": "linux-x86_64", |
| 43 | + "binary_id": "default", |
| 44 | + "python_major": 3, |
| 45 | + "python_minor": 99, |
| 46 | + }, |
| 47 | + ) |
| 48 | + assert response.status_code == 200 |
| 49 | + assert response.json() == [] |
| 50 | + |
| 51 | + |
| 52 | +@pytest.mark.asyncio |
| 53 | +async def test_diff_table(client, sample_benchmark_result): |
| 54 | + response = await client.get( |
| 55 | + "/api/diff", |
| 56 | + params={ |
| 57 | + "commit_sha": "a" * 40, |
| 58 | + "binary_id": "default", |
| 59 | + "environment_id": "linux-x86_64", |
| 60 | + }, |
| 61 | + ) |
| 62 | + assert response.status_code == 200 |
| 63 | + data = response.json() |
| 64 | + assert len(data) == 1 |
| 65 | + row = data[0] |
| 66 | + assert row["benchmark_name"] == "json_dumps" |
| 67 | + assert row["curr_metric_value"] == 1_000_000 |
| 68 | + assert row["metric_key"] == "high_watermark_bytes" |
| 69 | + assert row["has_flamegraph"] is True |
| 70 | + |
| 71 | + |
| 72 | +@pytest.mark.asyncio |
| 73 | +async def test_diff_table_commit_not_found(client): |
| 74 | + response = await client.get( |
| 75 | + "/api/diff", |
| 76 | + params={ |
| 77 | + "commit_sha": "f" * 40, |
| 78 | + "binary_id": "default", |
| 79 | + "environment_id": "linux-x86_64", |
| 80 | + }, |
| 81 | + ) |
| 82 | + assert response.status_code == 404 |
| 83 | + |
| 84 | + |
| 85 | +@pytest.mark.asyncio |
| 86 | +async def test_trends(client, sample_benchmark_result): |
| 87 | + response = await client.get( |
| 88 | + "/api/trends", |
| 89 | + params={ |
| 90 | + "benchmark_name": "json_dumps", |
| 91 | + "binary_id": "default", |
| 92 | + "environment_id": "linux-x86_64", |
| 93 | + "python_major": 3, |
| 94 | + "python_minor": 14, |
| 95 | + }, |
| 96 | + ) |
| 97 | + assert response.status_code == 200 |
| 98 | + data = response.json() |
| 99 | + assert len(data) == 1 |
| 100 | + assert data[0]["sha"] == "a" * 40 |
| 101 | + assert data[0]["high_watermark_bytes"] == 1_000_000 |
| 102 | + |
| 103 | + |
| 104 | +@pytest.mark.asyncio |
| 105 | +async def test_trends_batch(client, sample_benchmark_result): |
| 106 | + response = await client.post( |
| 107 | + "/api/trends-batch", |
| 108 | + json={ |
| 109 | + "trend_queries": [ |
| 110 | + { |
| 111 | + "benchmark_name": "json_dumps", |
| 112 | + "binary_id": "default", |
| 113 | + "environment_id": "linux-x86_64", |
| 114 | + "python_major": 3, |
| 115 | + "python_minor": 14, |
| 116 | + "limit": 50, |
| 117 | + } |
| 118 | + ] |
| 119 | + }, |
| 120 | + ) |
| 121 | + assert response.status_code == 200 |
| 122 | + data = response.json() |
| 123 | + assert "results" in data |
| 124 | + assert len(data["results"]) == 1 |
| 125 | + |
| 126 | + |
| 127 | +@pytest.mark.asyncio |
| 128 | +async def test_flamegraph(client, sample_benchmark_result): |
| 129 | + result_id = sample_benchmark_result.id |
| 130 | + response = await client.get(f"/api/flamegraph/{result_id}") |
| 131 | + assert response.status_code == 200 |
| 132 | + data = response.json() |
| 133 | + assert data["flamegraph_html"] == "<html>flamegraph</html>" |
| 134 | + assert data["benchmark_name"] == "json_dumps" |
| 135 | + |
| 136 | + |
| 137 | +@pytest.mark.asyncio |
| 138 | +async def test_flamegraph_not_found(client): |
| 139 | + response = await client.get("/api/flamegraph/nonexistent") |
| 140 | + assert response.status_code == 404 |
0 commit comments