Skip to content

Commit

Permalink
Add script to post results to GitHub (#153)
Browse files Browse the repository at this point in the history
  • Loading branch information
petechd authored Jun 21, 2024
1 parent 828368a commit 54e056a
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 0 deletions.
53 changes: 53 additions & 0 deletions ci/output-results-to-github.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
platform: linux
image_resource:
type: registry-image
source:
repository: python
tag: 3.11-slim
inputs:
- name: eq-survey-runner-benchmark
- name: pull-request
params:
OUTPUT_BUCKET:
OUTPUT_DIR:
NUMBER_OF_DAYS:
SERVICE_ACCOUNT_JSON: ((gcp.service_account_json))
ACCESS_TOKEN: ((eq-github.access_token))
run:
path: bash
args:
- -exc
- |
export GOOGLE_APPLICATION_CREDENTIALS=/root/gcloud-service-key.json
cat >$GOOGLE_APPLICATION_CREDENTIALS <<EOL
$SERVICE_ACCOUNT_JSON
EOL
cd eq-survey-runner-benchmark
pip3 install pipenv
pipenv install --deploy
# Get benchmark outputs
OUTPUT_BUCKET="$OUTPUT_BUCKET" \
OUTPUT_DIR="outputs" \
NUMBER_OF_DAYS="$NUMBER_OF_DAYS" \
pipenv run python -m scripts.get_benchmark_results
# Date to get summary for
RUNTIME_DATE_STRING="$(date +'%Y-%m-%d')"
SUMMARY=$(OUTPUT_DIR="outputs/${OUTPUT_DIR}" \
OUTPUT_DATE="$RUNTIME_DATE_STRING" \
OUTPUT_TO_GITHUB="True" \
pipenv run python -m scripts.get_summary)
# Post summary to Github
cd ../
cd pull-request
PR_NUMBER=`cat .git/resource/pr`
cd ../
apt-get update && apt-get install -y curl
curl -s -H "Authorization: token ${ACCESS_TOKEN}" \
-X POST -d "${SUMMARY}" \
"https://api.github.com/repos/ONSdigital/eq-questionnaire-runner/issues/${PR_NUMBER}/comments"
15 changes: 15 additions & 0 deletions scripts/benchmark_stats.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
from collections import defaultdict
from csv import DictReader
from glob import glob
Expand All @@ -10,6 +11,7 @@ class BenchmarkStats:

def __init__(self, folder_paths: List[str]):
self._files: List = []
self.output_to_github = os.getenv("OUTPUT_TO_GITHUB", False)
for folder_path in folder_paths:
self._files.extend(glob(f"{folder_path}/*stats.csv"))

Expand All @@ -28,6 +30,19 @@ def __str__(self):
f"{percentile}th: {self.percentiles[percentile]}ms"
for percentile in self.PERCENTILES_TO_REPORT
)
if self.output_to_github:
formatted_percentiles = formatted_percentiles.replace(os.linesep, "<br />")
return (
f'{{"body": "'
f'**Benchmark Results**<br /><br />'
f'Percentile Averages:<br />'
f'{formatted_percentiles}<br />'
f'GETs (99th): {self.average_get}ms<br />'
f'POSTs (99th): {self.average_post}ms<br /><br />'
f'Total Requests: {self.total_requests:,}<br />'
f'Total Failures: {self._total_failures:,}<br />'
f'Error Percentage: {(round(self.error_percentage, 2))}%<br />"}}'
)
return (
f'---\n'
f'Percentile Averages:\n'
Expand Down
5 changes: 5 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,8 @@
@pytest.fixture
def get_results_single_file():
return get_results(folders=["./tests/mock_stats/2024-02-07T03:09:41"])


@pytest.fixture
def get_results_single_file_github():
return get_results(folders=["./tests/mock_stats/2024-02-07T03:09:41"])
23 changes: 23 additions & 0 deletions tests/test_get_summary.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,35 @@
'Error Percentage: 0.0%\n'
)

EXPECTED_OUTPUT_20240206_FOLDER_GITHUB = (
'{"body": "'
'**Benchmark Results**<br /><br />'
'Percentile Averages:<br />'
'50th: 58ms<br />'
'90th: 96ms<br />'
'95th: 173ms<br />'
'99th: 301ms<br />'
'99.9th: 477ms<br />'
'GETs (99th): 380ms<br />'
'POSTs (99th): 211ms<br /><br />'
'Total Requests: 70,640<br />'
'Total Failures: 1<br />'
'Error Percentage: 0.0%<br />'
'"}\n'
)


def test_get_results(get_results_single_file):
results = list(get_results_single_file)
assert str(results[0]) == f"2024-02-07\n{EXPECTED_OUTPUT_SINGLE_FOLDER}\n"


def test_get_results_github(get_results_single_file, monkeypatch):
monkeypatch.setenv("OUTPUT_TO_GITHUB", "True")
results = list(get_results(folders=["./tests/mock_stats/2024-02-07T03:09:41"]))
assert str(results[0]) == f"2024-02-07\n{EXPECTED_OUTPUT_20240206_FOLDER_GITHUB}"


def test_get_results_multiple_folders():
generated_results = get_results(
folders=[
Expand Down

0 comments on commit 54e056a

Please sign in to comment.