-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhistory_extensions.ts
156 lines (138 loc) · 4.87 KB
/
history_extensions.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
import { BenchmarkResult, colors } from "./deps.ts";
import { rtime } from "./utils.ts";
import { stripColor } from "./common.ts";
import type { prettyBenchmarkProgressOptions } from "./pretty_benchmark_progress.ts";
import type { prettyBenchmarkResultOptions } from "./pretty_benchmark_result.ts";
import type { ColumnDefinition } from "./pretty_benchmark_down.ts";
import type {
DeltaKey,
prettyBenchmarkHistory,
} from "./pretty_benchmark_history.ts";
/** Returns the calculated delta for the specific benchmark in a formatted string.
*
* Meant to be used as:
* ```ts
* prettyBenchmarkProgress({rowExtras: deltaProgressRowExtra(history)});
* ```
* .*/
export function deltaProgressRowExtra(history: prettyBenchmarkHistory) {
return (
result: BenchmarkResult,
options?: prettyBenchmarkProgressOptions,
) => {
let deltaString = getCliDeltaString(history, result);
if (options?.nocolor) {
deltaString = stripColor(deltaString);
}
return ` [${deltaString}]`;
};
}
/** Returns the calculated delta for the specific benchmark in a formatted string.
*
* Meant to be used as:
* ```ts
* prettyBenchmarkResult({infoCell: deltaResultInfoCell(history)});
* ```
* .*/
export function deltaResultInfoCell(history: prettyBenchmarkHistory) {
return (result: BenchmarkResult, options?: prettyBenchmarkResultOptions) => {
let deltaString = getCliDeltaString(history, result);
if (options?.nocolor) {
deltaString = stripColor(deltaString);
}
return ` ${deltaString}`;
};
}
/** Defines a delta column, which shows the changes for the benchmark. Shows `-` when there was no previous measurements for the benchmark in the history.
*
* Calculates delta on `measuredRunsAvgMs` by default, which can be changed in the options with `key`.*/
export function deltaColumn<T = unknown>(
history: prettyBenchmarkHistory<T>,
options?: { key: DeltaKey<T> },
): ColumnDefinition {
const workingKey = options?.key || "measuredRunsAvgMs";
return {
title: `Change in ${options?.key || "average"}`,
formatter: (result, cd) => {
const delta = history.getDeltaForBenchmark(result, [workingKey]);
if (delta) {
const perc = (delta[workingKey as string].percent * 100).toFixed(0);
const diff = rtime(Math.abs(delta.measuredRunsAvgMs.amount));
const notSpaceChar = " ";
const smallSpace = " ";
if (delta[workingKey as string].amount > 0) {
return `🔺 ${`+${perc}`.padStart(4, notSpaceChar)}% (${
diff.padStart(6)
}ms)`;
} else {
return `🟢${smallSpace} ${perc.padStart(4, notSpaceChar)}% (${
diff.padStart(6)
}ms)`;
}
}
return "-";
},
};
}
/** Defines a column for each different `runBenchmarks` results in the history.
*
* The title of the columns are the dates they were run or the `id`'s of them if they are present.
*
* Shows `measuredRunsAvgMs` by default, which can be changed in the options with `key`.*/
export function historyColumns<T = unknown>(
history: prettyBenchmarkHistory<T>,
options?: {
key?: DeltaKey<T>;
titleFormatter?: (dateString: string, id?: string) => string;
},
): ColumnDefinition[] {
if (history.getData().history.length === 0) {
return [];
}
const dateFormatter = (dateString: string) => {
const parsedDate = new Date(dateString);
return parsedDate.toISOString().split("T").join("<br/>").replace(/Z/, "");
};
return history.getData().history.map((run) => {
return {
title: (typeof options?.titleFormatter === "function"
? options.titleFormatter(run.date, run.id)
: run.id ?? dateFormatter(run.date)),
toFixed: 3,
align: "right",
formatter: (result: BenchmarkResult) => {
if (!run.benchmarks[result.name]) {
return "-";
}
const workingKey = options?.key ?? "measuredRunsAvgMs";
if (workingKey === "measuredRunsAvgMs" || workingKey === "totalMs") {
//deno-lint-ignore no-explicit-any
return (run.benchmarks[result.name] as any)[workingKey as any] || "-";
} else {
return run.benchmarks[result.name].extras?.[workingKey] || "-";
}
},
};
});
}
function getCliDeltaString(
history: prettyBenchmarkHistory,
result: BenchmarkResult,
) {
const delta = history.getDeltaForBenchmark(result);
let deltaString = `${colors.gray(" ▪ no history ▪ ".padEnd(19))}`;
if (delta) {
const perc = (delta.measuredRunsAvgMs.percent * 100).toFixed(0);
const diff = rtime(Math.abs(delta.measuredRunsAvgMs.amount));
if (delta.measuredRunsAvgMs.amount > 0) {
deltaString = `${
colors.red(` ▲ ${`+${perc}`.padStart(4)}% (${diff.padStart(6)}ms)`)
}`;
} else {
deltaString = `${
colors.green(` ▼ ${perc.padStart(4)}% (${diff.padStart(6)}ms)`)
}`;
}
}
return deltaString;
}