-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathutils.ts
More file actions
156 lines (138 loc) · 5.13 KB
/
utils.ts
File metadata and controls
156 lines (138 loc) · 5.13 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import { browser } from "@wdio/globals";
import type { Bench } from "tinybench";
import type { local } from "webdriver";
import {
Ciphersuite,
CoreCrypto,
CoreCryptoLogLevel,
type CommitBundle,
type HistorySecret,
type MlsTransport,
type MlsTransportData,
} from "../src/browser/CoreCrypto";
type ccModuleType = typeof import("../src/browser/CoreCrypto");
declare global {
interface Window {
ccModule: ccModuleType;
deliveryService: DeliveryService;
_latestCommitBundle: CommitBundle;
tinybench: typeof import("tinybench");
benchRunning: boolean;
bench: Bench;
helpers: BenchmarkHelpers;
}
}
const logLevel = Number(process.env["CC_TEST_LOG_LEVEL"] || "0");
export async function setup() {
if ((await browser.getUrl()) === "about:blank") {
await browser.url("/html/index.html");
}
browser.on("log.entryAdded", logEvents);
await browser.execute(async (loglevel) => {
if (window.tinybench === undefined) {
window.tinybench =
// @ts-expect-error TS2307: Cannot find module ./corecrypto.js or its corresponding type declarations.
await import("/node_modules/tinybench/dist/index.js");
}
if (window.ccModule === undefined) {
// @ts-expect-error TS2307: Cannot find module ./corecrypto.js or its corresponding type declarations.
window.ccModule = await import("/corecrypto.js");
await window.ccModule.initWasmModule(
"/autogenerated/wasm-bindgen/"
);
if (loglevel >= 2) {
window.ccModule.setLogger({
log: (
_level: CoreCryptoLogLevel,
message: string,
context: string | undefined
) => {
console.log(message, context);
},
});
window.ccModule.setMaxLogLevel(
window.ccModule.CoreCryptoLogLevel.Debug
);
}
}
if (window.deliveryService === undefined) {
window.deliveryService = {
async sendCommitBundle(commitBundle: CommitBundle) {
window._latestCommitBundle = commitBundle;
return window.ccModule.MlsTransportResponse.Success.new();
},
async prepareForTransport(
secret: HistorySecret
): Promise<MlsTransportData> {
return Promise.resolve(secret.clientId.copyBytes());
},
async getLatestCommitBundle() {
return window._latestCommitBundle;
},
};
}
class BenchmarkHelpers {
static async setupCc(
cipherSuite: Ciphersuite
): Promise<CoreCrypto> {
const encoder = new TextEncoder();
const clientIdStr = window.crypto.randomUUID();
const id = new window.ccModule.ClientId(
encoder.encode(clientIdStr).buffer
);
const key = new Uint8Array(32);
crypto.getRandomValues(key);
const db = await window.ccModule.Database.open(
clientIdStr,
new window.ccModule.DatabaseKey(key.buffer)
);
const cc = window.ccModule.CoreCrypto.new(db);
await cc.newTransaction(async (ctx) => {
await ctx.mlsInit(id, window.deliveryService);
await ctx.addCredential(
window.ccModule.Credential.basic(cipherSuite, id)
);
});
return cc;
}
}
window.helpers = BenchmarkHelpers;
}, logLevel);
}
interface DeliveryService extends MlsTransport {
getLatestCommitBundle: () => Promise<CommitBundle>;
}
interface BenchmarkHelpers {
setupCc: (cipherSuite: Ciphersuite) => Promise<CoreCrypto>;
}
type ParameterSet = {
count: number;
size: number;
cipherSuite: number;
};
export async function benchmarkParameters(): Promise<ParameterSet[]> {
const messageCounts = [1, 10, 100];
const messageSizes = [16, 1024, 65536];
const cipherSuites = [
Ciphersuite.Mls128Dhkemx25519Aes128gcmSha256Ed25519,
Ciphersuite.Mls128Dhkemx25519Chacha20poly1305Sha256Ed25519,
Ciphersuite.Mls128Dhkemp256Aes128gcmSha256P256,
Ciphersuite.Mls256Dhkemp384Aes256gcmSha384P384,
Ciphersuite.Mls256Dhkemp521Aes256gcmSha512P521,
];
function* benchmarkCombinations() {
for (const count of messageCounts) {
for (const size of messageSizes) {
for (const cipherSuite of cipherSuites) {
yield { count, size, cipherSuite };
}
}
}
}
return Array.from(benchmarkCombinations()) as ParameterSet[]; // return as plain array
}
function logEvents(entry: local.LogEntry) {
if (logLevel >= 1) {
console.log(`[${entry.level}] ${entry.text}`);
}
}