-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathtest_perf.py
66 lines (51 loc) · 1.81 KB
/
test_perf.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
import json
import time
import pytest
from pyscitt import crypto
DEFAULT_ITERATIONS_NUM = 10
CLIENT_WAIT_TIME = 0.01
def measure_latency(fn, arg_fn=None, n=DEFAULT_ITERATIONS_NUM):
if arg_fn is None:
def arg_fn():
return None
times = []
for _ in range(n):
arg = arg_fn()
t_start = time.perf_counter()
fn(arg)
t_end = time.perf_counter()
times.append(t_end - t_start)
return {
"avg": sum(times) / len(times),
"p99": sorted(times)[int(len(times) * 0.99)],
"min": min(times),
"max": max(times),
}
@pytest.mark.perf
@pytest.mark.disable_proxy
class TestPerf:
def test_latency(self, client, trusted_ca):
payload = {"foo": "bar"}
client = client.replace(wait_time=CLIENT_WAIT_TIME)
# Test x5c performance.
identity = trusted_ca.create_identity(
length=1, alg="ES256", kty="ec", ec_curve="P-256"
)
signed_statemtent = crypto.sign_json_statement(identity, payload)
latency_x5c_submit_s = measure_latency(
lambda _: client.register_signed_statement(signed_statemtent)
)
latency_x5c_submit_and_receipt_s = measure_latency(
lambda _: client.register_signed_statement(signed_statemtent)
)
# Time-to-receipt depends on the configured signature interval of
# the CCF network and is hence just a rough estimate.
stats = {
"latency_x5c_submit_s": latency_x5c_submit_s,
"latency_x5c_submit_and_receipt_s": latency_x5c_submit_and_receipt_s,
}
# Write stats to file for further analysis.
with open("perf.json", "w", encoding="utf-8") as fp:
json.dump(stats, fp, indent=2)