]>
Commit | Line | Data |
---|---|---|
1d09f67e TL |
1 | # Licensed to the Apache Software Foundation (ASF) under one |
2 | # or more contributor license agreements. See the NOTICE file | |
3 | # distributed with this work for additional information | |
4 | # regarding copyright ownership. The ASF licenses this file | |
5 | # to you under the Apache License, Version 2.0 (the | |
6 | # "License"); you may not use this file except in compliance | |
7 | # with the License. You may obtain a copy of the License at | |
8 | # | |
9 | # http://www.apache.org/licenses/LICENSE-2.0 | |
10 | # | |
11 | # Unless required by applicable law or agreed to in writing, | |
12 | # software distributed under the License is distributed on an | |
13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | |
14 | # KIND, either express or implied. See the License for the | |
15 | # specific language governing permissions and limitations | |
16 | # under the License. | |
17 | ||
18 | ||
19 | import json | |
20 | ||
21 | from ..benchmark.core import Benchmark, BenchmarkSuite | |
22 | from ..benchmark.runner import BenchmarkRunner, StaticBenchmarkRunner | |
23 | from ..benchmark.compare import BenchmarkComparator | |
24 | ||
25 | ||
26 | class JsonEncoder(json.JSONEncoder): | |
27 | def default(self, o): | |
28 | if isinstance(o, Benchmark): | |
29 | return BenchmarkCodec.encode(o) | |
30 | ||
31 | if isinstance(o, BenchmarkSuite): | |
32 | return BenchmarkSuiteCodec.encode(o) | |
33 | ||
34 | if isinstance(o, BenchmarkRunner): | |
35 | return BenchmarkRunnerCodec.encode(o) | |
36 | ||
37 | if isinstance(o, BenchmarkComparator): | |
38 | return BenchmarkComparatorCodec.encode(o) | |
39 | ||
40 | return json.JSONEncoder.default(self, o) | |
41 | ||
42 | ||
43 | class BenchmarkCodec: | |
44 | @staticmethod | |
45 | def encode(b): | |
46 | return { | |
47 | "name": b.name, | |
48 | "unit": b.unit, | |
49 | "less_is_better": b.less_is_better, | |
50 | "values": b.values, | |
51 | "time_unit": b.time_unit, | |
52 | "times": b.times, | |
53 | "counters": b.counters, | |
54 | } | |
55 | ||
56 | @staticmethod | |
57 | def decode(dct, **kwargs): | |
58 | return Benchmark(**dct, **kwargs) | |
59 | ||
60 | ||
61 | class BenchmarkSuiteCodec: | |
62 | @staticmethod | |
63 | def encode(bs): | |
64 | return { | |
65 | "name": bs.name, | |
66 | "benchmarks": [BenchmarkCodec.encode(b) for b in bs.benchmarks] | |
67 | } | |
68 | ||
69 | @staticmethod | |
70 | def decode(dct, **kwargs): | |
71 | benchmarks = [BenchmarkCodec.decode(b) | |
72 | for b in dct.pop("benchmarks", [])] | |
73 | return BenchmarkSuite(benchmarks=benchmarks, **dct, **kwargs) | |
74 | ||
75 | ||
76 | class BenchmarkRunnerCodec: | |
77 | @staticmethod | |
78 | def encode(br): | |
79 | return {"suites": [BenchmarkSuiteCodec.encode(s) for s in br.suites]} | |
80 | ||
81 | @staticmethod | |
82 | def decode(dct, **kwargs): | |
83 | suites = [BenchmarkSuiteCodec.decode(s) | |
84 | for s in dct.pop("suites", [])] | |
85 | return StaticBenchmarkRunner(suites=suites, **dct, **kwargs) | |
86 | ||
87 | ||
88 | class BenchmarkComparatorCodec: | |
89 | @staticmethod | |
90 | def encode(bc): | |
91 | comparator = bc.formatted | |
92 | ||
93 | suite_name = bc.suite_name | |
94 | if suite_name: | |
95 | comparator["suite"] = suite_name | |
96 | ||
97 | return comparator |