-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod.ts
204 lines (184 loc) · 5.88 KB
/
mod.ts
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
// Copyright 2020 William E. Sorensen. All rights reserved. MIT license.
export type ExperimentFunction<TParams extends any[], TResult> = (
...args: TParams
) => TResult;
export type ExperimentAsyncFunction<
TParams extends any[],
TResult,
> = ExperimentFunction<TParams, Promise<TResult>>;
export interface Results<TParams extends any[], TResult> {
experimentName: string;
experimentArguments: TParams;
controlResult?: TResult;
candidateResult?: TResult;
controlError?: any;
candidateError?: any;
controlTimeMs?: number;
candidateTimeMs?: number;
}
export interface Options<TParams extends any[], TResult> {
publish?: (results: Results<TParams, TResult>) => void;
enabled?: (...args: TParams) => boolean;
}
function defaultPublish<TParams extends any[], TResult>(
results: Results<TParams, TResult>,
): void {
if (
results.candidateResult !== results.controlResult ||
(results.candidateError && !results.controlError) ||
(!results.candidateError && results.controlError)
) {
console.warn(`Experiment ${results.experimentName}: difference found`);
}
}
const defaultOptions = {
publish: defaultPublish,
};
/**
* A factory that creates an experiment function.
*
* @param name - The name of the experiment, typically for use in publish.
* @param control - The legacy function you are trying to replace.
* @param candidate - The new function intended to replace the control.
* @param [options] - Options for the experiment. You will usually want to specify a publish function.
* @returns A function that acts like the control while also running the candidate and publishing results.
*/
export function experiment<TParams extends any[], TResult>({
name,
control,
candidate,
options = defaultOptions,
}: {
name: string;
control: ExperimentFunction<TParams, TResult>;
candidate: ExperimentFunction<TParams, TResult>;
options?: Options<TParams, TResult>;
}): ExperimentFunction<TParams, TResult> {
const publish = options.publish || defaultPublish;
return (...args): TResult => {
let controlResult: TResult | undefined;
let candidateResult: TResult | undefined;
let controlError: any;
let candidateError: any;
let controlTimeMs: number;
let candidateTimeMs: number;
const isEnabled: boolean = !options.enabled || options.enabled(...args);
function publishResults(): void {
if (isEnabled) {
publish({
experimentName: name,
experimentArguments: args,
controlResult,
candidateResult,
controlError,
candidateError,
controlTimeMs,
candidateTimeMs,
});
}
}
if (isEnabled) {
try {
const candidateStartTime = performance.now();
candidateResult = candidate(...args);
const candidateEndTime = performance.now();
candidateTimeMs = candidateEndTime - candidateStartTime;
} catch (e) {
candidateError = e;
}
}
try {
const controlStartTime = performance.now();
controlResult = control(...args);
const controlEndTime = performance.now();
controlTimeMs = controlEndTime - controlStartTime;
} catch (e) {
controlError = e;
publishResults();
throw e;
}
publishResults();
return controlResult;
};
}
async function executeAndTime<TParams extends any[], TResult>(
controlOrCandidate: ExperimentAsyncFunction<TParams, TResult>,
args: TParams,
): Promise<[TResult, number]> {
const startTime = performance.now();
const result = await controlOrCandidate(...args);
const endTime = performance.now();
const timeMs = endTime - startTime;
return [result, timeMs];
}
/**
* A factory that creates an asynchronous experiment function.
*
* @param name - The name of the experiment, typically for use in publish.
* @param control - The legacy async function you are trying to replace.
* @param candidate - The new async function intended to replace the control.
* @param [options] - Options for the experiment. You will usually want to specify a publish function.
* @returns An async function that acts like the control while also running the candidate and publishing results.
*/
export function experimentAsync<TParams extends any[], TResult>({
name,
control,
candidate,
options = defaultOptions,
}: {
name: string;
control: ExperimentAsyncFunction<TParams, TResult>;
candidate: ExperimentAsyncFunction<TParams, TResult>;
options?: Options<TParams, TResult>;
}): ExperimentAsyncFunction<TParams, TResult> {
const publish = options.publish || defaultPublish;
return async (...args): Promise<TResult> => {
let controlResult: TResult | undefined;
let candidateResult: TResult | undefined;
let controlError: any;
let candidateError: any;
let controlTimeMs: number | undefined;
let candidateTimeMs: number | undefined;
const isEnabled: boolean = !options.enabled || options.enabled(...args);
function publishResults(): void {
if (isEnabled) {
publish({
experimentName: name,
experimentArguments: args,
controlResult,
candidateResult,
controlError,
candidateError,
controlTimeMs,
candidateTimeMs,
});
}
}
if (isEnabled) {
// Run in parallel
[
[candidateResult, candidateTimeMs],
[controlResult, controlTimeMs],
] = await Promise.all([
executeAndTime(candidate, args).catch((e) => {
candidateError = e;
return [undefined, undefined];
}),
executeAndTime(control, args).catch((e) => {
controlError = e;
return [undefined, undefined];
}),
]);
} else {
controlResult = await control(...args).catch((e) => {
controlError = e;
return undefined;
});
}
publishResults();
if (controlError) {
throw controlError;
}
return controlResult!;
};
}