Debian Codegraph @

crt/s2n/.github/bin/criterion_to_cloudwatch.py

raw symbols SCIP
1 # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License").
4 # You may not use this file except in compliance with the License.
5 # A copy of the License is located at
6 #
7 # http://aws.amazon.com/apache2.0
8 #
9 # or in the "license" file accompanying this file. This file is distributed
10 # on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
11 # express or implied. See the License for the specific language governing
12 # permissions and limitations under the License.
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 # The id is made up of the bench group and the category, separated by a /.
31 # For example: handshake-ecdsa384/s2n-tls
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 # CloudWatch doesn't support nanoseconds, so the units are converted to microseconds.
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 # Skip criterion output that isn't describing a benchmarking result.
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 )