(samples, metric, options = {})
| 185 | } |
| 186 | |
| 187 | export function compare(samples, metric, options = {}) { |
| 188 | if (!samples.length) { |
| 189 | throw new Error('Comparison requires samples.'); |
| 190 | } |
| 191 | let blocks = [...new Set(samples.map(sample => sample.block))]; |
| 192 | let pairs = blocks.map(block => { |
| 193 | let values = arm => |
| 194 | samples |
| 195 | .filter(sample => sample.block === block && sample.arm === arm) |
| 196 | .map(sample => sample.metrics[metric]); |
| 197 | return {base: values('base'), candidate: values('candidate')}; |
| 198 | }); |
| 199 | if ( |
| 200 | pairs.some( |
| 201 | pair => |
| 202 | !pair.base.length || |
| 203 | pair.base.length !== pair.candidate.length || |
| 204 | [...pair.base, ...pair.candidate].some(value => !Number.isFinite(value)) |
| 205 | ) |
| 206 | ) { |
| 207 | throw new Error('Comparison requires complete, finite paired samples for every block.'); |
| 208 | } |
| 209 | let median = values => percentile(values, 0.5); |
| 210 | let base = median(pairs.flatMap(pair => pair.base)); |
| 211 | let candidate = median(pairs.flatMap(pair => pair.candidate)); |
| 212 | // Each browser launch is compared with itself, so drift between launches cancels; |
| 213 | // the blocks, not the trials within them, are the independent observations. |
| 214 | let deltas = pairs.map(pair => median(pair.candidate) - median(pair.base)); |
| 215 | let { |
| 216 | estimate: delta, |
| 217 | interval, |
| 218 | confidence |
| 219 | } = signedRankInterval(deltas, options.confidence ?? 0.95); |
| 220 | let threshold = Math.max(options.absolute ?? 8, base * (options.relative ?? 0.05)); |
| 221 | let verdict = 'inconclusive'; |
| 222 | if (interval) { |
| 223 | if (interval[1] < -threshold) { |
| 224 | verdict = 'improvement detected'; |
| 225 | } else if (interval[0] > threshold) { |
| 226 | verdict = 'regression detected'; |
| 227 | } else if (interval[0] >= -threshold && interval[1] <= threshold) { |
| 228 | verdict = 'within practical threshold'; |
| 229 | } |
| 230 | } |
| 231 | return { |
| 232 | base, |
| 233 | candidate, |
| 234 | delta, |
| 235 | percent: base ? (delta / base) * 100 : null, |
| 236 | deltas, |
| 237 | interval, |
| 238 | confidence, |
| 239 | threshold, |
| 240 | verdict, |
| 241 | blocks: blocks.length |
| 242 | }; |
| 243 | } |
| 244 |
no test coverage detected