Skip to content

[CI] Add unittesting for metrics collection script #150360

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

boomanaiden154
Copy link
Contributor

This patch adds some initial unittests for the metrics collection
script. This initial patch focuses on getting the files setup and adding
unittests for uploading metrics. A subsequent patch will add tests for
the workflow collection which is significantly more complicated.

Created using spr 1.3.6
boomanaiden154 added a commit to boomanaiden154/llvm-project that referenced this pull request Jul 24, 2025
This patch adds some initial unittests for the metrics collection
script. This initial patch focuses on getting the files setup and adding
unittests for uploading metrics. A subsequent patch will add tests for
the workflow collection which is significantly more complicated.

Pull Request: llvm#150360
@boomanaiden154 boomanaiden154 requested a review from cmtice July 24, 2025 17:49
Created using spr 1.3.6
Created using spr 1.3.6
@llvmbot
Copy link
Member

llvmbot commented Jul 24, 2025

@llvm/pr-subscribers-github-workflow

Author: Aiden Grossman (boomanaiden154)

Changes

This patch adds some initial unittests for the metrics collection
script. This initial patch focuses on getting the files setup and adding
unittests for uploading metrics. A subsequent patch will add tests for
the workflow collection which is significantly more complicated.


Full diff: https://github.com/llvm/llvm-project/pull/150360.diff

2 Files Affected:

  • (added) .ci/metrics/metrics_test.py (+75)
  • (modified) .github/workflows/check-ci.yml (+1)
diff --git a/.ci/metrics/metrics_test.py b/.ci/metrics/metrics_test.py
new file mode 100644
index 0000000000000..259e55f817939
--- /dev/null
+++ b/.ci/metrics/metrics_test.py
@@ -0,0 +1,75 @@
+# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+# See https://llvm.org/LICENSE.txt for license information.
+# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+"""Tests for metrics.py"""
+
+from dataclasses import dataclass
+import requests
+import unittest
+import unittest.mock
+
+import metrics
+
+
+class TestMetrics(unittest.TestCase):
+    def test_upload_gauge_metric(self):
+        """Test that we can upload a gauge metric correctly.
+
+        Also verify that we pass around parameters like API keys and user IDs
+        correctly to the HTTP POST request.
+        """
+        test_metrics = [metrics.GaugeMetric("gauge_test", 5, 1000)]
+        return_value = requests.Response()
+        return_value.status_code = 204
+        with unittest.mock.patch(
+            "requests.post", return_value=return_value
+        ) as post_mock:
+            metrics.upload_metrics(test_metrics, "test_userid", "test_api_key")
+            self.assertSequenceEqual(post_mock.call_args.args, [metrics.GRAFANA_URL])
+            self.assertEqual(
+                post_mock.call_args.kwargs["data"], "gauge_test value=5 1000"
+            )
+            self.assertEqual(
+                post_mock.call_args.kwargs["auth"], ("test_userid", "test_api_key")
+            )
+
+    def test_upload_job_metric(self):
+        """Test that we can upload a job metric correctly."""
+        test_metrics = [
+            metrics.JobMetrics("test_job", 5, 10, 1, 1000, 7, "test_workflow")
+        ]
+        return_value = requests.Response()
+        return_value.status_code = 204
+        with unittest.mock.patch(
+            "requests.post", return_value=return_value
+        ) as post_mock:
+            metrics.upload_metrics(test_metrics, "test_userid", "test_aoi_key")
+            self.assertEqual(
+                post_mock.call_args.kwargs["data"],
+                "test_job queue_time=5,run_time=10,status=1 1000",
+            )
+
+    def test_upload_unknown_metric(self):
+        """Test we report an error if we encounter an unknown metric type."""
+
+        @dataclass
+        class FakeMetric:
+            fake_data: str
+
+        test_metrics = [FakeMetric("test")]
+
+        with self.assertRaises(ValueError):
+            metrics.upload_metrics(test_metrics, "test_userid", "test_api_key")
+
+    def test_bad_response_code(self):
+        """Test that we gracefully handle HTTP response errors."""
+        test_metrics = [metrics.GaugeMetric("gauge_test", 5, 1000)]
+        return_value = requests.Response()
+        return_value.status_code = 403
+        # Just assert that we continue running here and do not raise anything.
+        with unittest.mock.patch("requests.post", return_value=return_value) as _:
+            metrics.upload_metrics(test_metrics, "test_userid", "test_api_key")
+
+
+if __name__ == "__main__":
+    unittest.main()
diff --git a/.github/workflows/check-ci.yml b/.github/workflows/check-ci.yml
index d6212c8a84737..befea2093f908 100644
--- a/.github/workflows/check-ci.yml
+++ b/.github/workflows/check-ci.yml
@@ -31,6 +31,7 @@ jobs:
       - name: Install Python Dependencies
         run: |
           pip3 install -r .ci/all_requirements.txt
+          pip3 install -r .ci/metrics/requirements.lock.txt
           pip3 install pytest==8.4.1
       - name: Run Tests
         working-directory: .ci

boomanaiden154 added a commit to boomanaiden154/llvm-project that referenced this pull request Jul 24, 2025
This patch adds some initial unittests for the metrics collection
script. This initial patch focuses on getting the files setup and adding
unittests for uploading metrics. A subsequent patch will add tests for
the workflow collection which is significantly more complicated.

Pull Request: llvm#150360
@boomanaiden154 boomanaiden154 merged commit c12dfd5 into main Jul 24, 2025
14 checks passed
@boomanaiden154 boomanaiden154 deleted the users/boomanaiden154/ci-add-unittesting-for-metrics-collection-script branch July 24, 2025 19:25
llvm-sync bot pushed a commit to arm/arm-toolchain that referenced this pull request Jul 24, 2025
This patch adds some initial unittests for the metrics collection
script. This initial patch focuses on getting the files setup and adding
unittests for uploading metrics. A subsequent patch will add tests for
the workflow collection which is significantly more complicated.

Reviewers: Keenuts, gburgessiv, dschuff, cmtice, lnihlen

Reviewed By: cmtice

Pull Request: llvm/llvm-project#150360
mahesh-attarde pushed a commit to mahesh-attarde/llvm-project that referenced this pull request Jul 28, 2025
This patch adds some initial unittests for the metrics collection
script. This initial patch focuses on getting the files setup and adding
unittests for uploading metrics. A subsequent patch will add tests for
the workflow collection which is significantly more complicated.

Reviewers: Keenuts, gburgessiv, dschuff, cmtice, lnihlen

Reviewed By: cmtice

Pull Request: llvm#150360
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants