| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
import argparse |
| 16 |
import json |
| 17 |
from typing import Tuple |
| 18 |
from collections.abc import Generator |
| 19 |
import boto3 |
| 20 |
|
| 21 |
|
| 22 |
class CriterionResult: |
| 23 |
def __init__(self, json_obj: dict): |
| 24 |
self._json_obj: dict = json_obj |
| 25 |
|
| 26 |
@property |
| 27 |
def _group_and_category(self) -> Tuple[str, str]: |
| 28 |
id_ = self._json_obj["id"] |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
id_split = id_.split("/") |
| 33 |
assert len(id_split) == 2 |
| 34 |
|
| 35 |
group = id_split[0] |
| 36 |
category = id_split[1] |
| 37 |
|
| 38 |
return group, category |
| 39 |
|
| 40 |
@property |
| 41 |
def group(self) -> str: |
| 42 |
group, category = self._group_and_category |
| 43 |
return group |
| 44 |
|
| 45 |
@property |
| 46 |
def category(self) -> str: |
| 47 |
group, category = self._group_and_category |
| 48 |
return category |
| 49 |
|
| 50 |
@property |
| 51 |
def mean_us(self) -> float: |
| 52 |
mean = self._json_obj["mean"] |
| 53 |
|
| 54 |
unit = mean["unit"] |
| 55 |
assert unit == "ns" |
| 56 |
|
| 57 |
|
| 58 |
estimate_ns = mean["estimate"] |
| 59 |
estimate_us = estimate_ns / 1_000 |
| 60 |
|
| 61 |
return estimate_us |
| 62 |
|
| 63 |
|
| 64 |
class CriterionReader: |
| 65 |
def __init__(self, criterion_output_path: str): |
| 66 |
self.criterion_output_path: str = criterion_output_path |
| 67 |
|
| 68 |
def read_bench_results(self) -> Generator[CriterionResult, None, None]: |
| 69 |
with open(self.criterion_output_path, 'r') as output: |
| 70 |
for line in output.readlines(): |
| 71 |
obj = json.loads(line) |
| 72 |
|
| 73 |
|
| 74 |
if obj.get("reason") != "benchmark-complete": |
| 75 |
continue |
| 76 |
|
| 77 |
yield CriterionResult(obj) |
| 78 |
|
| 79 |
|
| 80 |
if __name__ == '__main__': |
| 81 |
parser = argparse.ArgumentParser( |
| 82 |
prog="criterion_to_cloudwatch", |
| 83 |
description="Emits CloudWatch metrics from criterion output.", |
| 84 |
) |
| 85 |
parser.add_argument( |
| 86 |
"--criterion_output_path", |
| 87 |
required=True, |
| 88 |
help="File containing criterion json output. Generated with cargo-criterion by specifying the " |
| 89 |
"`--message-format json` option.", |
| 90 |
) |
| 91 |
parser.add_argument( |
| 92 |
"--namespace", |
| 93 |
required=True, |
| 94 |
help="CloudWatch namespace to emit metrics to (e.g. s2n-tls-bench).", |
| 95 |
) |
| 96 |
parser.add_argument( |
| 97 |
"--platform", |
| 98 |
required=True, |
| 99 |
help="Specifies the platform dimension of the CloudWatch metrics (e.g. Ubuntu24-x86).", |
| 100 |
) |
| 101 |
args = parser.parse_args() |
| 102 |
|
| 103 |
cloudwatch = boto3.client("cloudwatch") |
| 104 |
|
| 105 |
reader = CriterionReader(args.criterion_output_path) |
| 106 |
for result in reader.read_bench_results(): |
| 107 |
print(f"Emitting {args.namespace} - {result.group}/{result.category} for {args.platform}: {result.mean_us}") |
| 108 |
|
| 109 |
cloudwatch.put_metric_data( |
| 110 |
Namespace=args.namespace, |
| 111 |
MetricData=[{ |
| 112 |
"MetricName": result.group, |
| 113 |
"Dimensions": [ |
| 114 |
{ |
| 115 |
"Name": "Category", |
| 116 |
"Value": result.category, |
| 117 |
}, |
| 118 |
{ |
| 119 |
"Name": "Platform", |
| 120 |
"Value": args.platform, |
| 121 |
}, |
| 122 |
], |
| 123 |
"Value": result.mean_us, |
| 124 |
"Unit": "Microseconds", |
| 125 |
}], |
| 126 |
) |