Skip to content

Commit 8edddaf

Browse files
committed
feat(profile): add profile feature
1 parent f8b479d commit 8edddaf

File tree

2 files changed

+72
-5
lines changed

2 files changed

+72
-5
lines changed

index.html

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,12 @@ <h1>Vue3 benchmarks</h1>
7272
<button id="start" onclick="start()">Start</button>
7373
<button id="abort" onclick="abort()" disabled>Abort</button>
7474

75+
<label for="standalone">profile run</label>
76+
<input type="text" id="standalone" placeholder="Example: ref:write ref" value="" />
77+
<label for="iterations">profile iterations</label>
78+
<input type="text" id="iterations" value="100_000" />
79+
<button onclick="standalone()">Start profile run</button>
80+
7581
<p>Link usage: <pre>?v=3.0.0-rc.12&b=ref,computed</pre></p>
7682

7783
<div id="started" style="display: none">

src/main.js

Lines changed: 66 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ const abortButton = document.getElementById("abort");
1212
const startButton = document.getElementById("start");
1313
const results = document.getElementById("results");
1414

15+
const standaloneInput = document.getElementById("standalone");
16+
const iterationsInput = document.getElementById("iterations");
17+
1518
// This allows aborting and outputting while tests are being run.
1619
Benchmark.options.async = true;
1720

@@ -38,17 +41,22 @@ function addResult(name, result, fn) {
3841
// @see https://semver.org/#is-there-a-suggested-regular-expression-regex-to-check-a-semver-string
3942
const semverRegex = /^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$/;
4043

41-
window.start = function() {
42-
const benchmarks = benchmarksInput.value.trim() !== "" ? benchmarksInput.value.split(",") : available;
43-
44-
document.getElementById("started").style.display = "block";
45-
44+
function getUrl() {
4645
let url = urlInput.value;
4746
if (url.trim() === "") {
4847
url = "vue.global.js";
4948
} else if (semverRegex.test(url)) {
5049
url = `https://cdnjs.cloudflare.com/ajax/libs/vue/${url}/vue.global.js`;
5150
}
51+
return url;
52+
}
53+
54+
window.start = function() {
55+
const benchmarks = benchmarksInput.value.trim() !== "" ? benchmarksInput.value.split(",") : available;
56+
57+
document.getElementById("started").style.display = "block";
58+
59+
let url = getUrl();
5260

5361
log("Use Vue3: " + url);
5462
log("Benchmarks: " + benchmarks.join(","));
@@ -112,6 +120,57 @@ async function runTests(benchmarks) {
112120
}
113121
}
114122

123+
window.standalone = function() {
124+
const v = standaloneInput.value;
125+
const index = v.indexOf(":");
126+
if (index === -1) {
127+
alert("Format: {name}:{benchmark}");
128+
} else {
129+
const name = v.substr(0, index).trim();
130+
const bench = v.substr(index + 1).trim();
131+
startStandalone(name, bench);
132+
}
133+
}
134+
135+
async function startStandalone(name, benchmarkName) {
136+
let url = getUrl();
137+
138+
log("Use Vue3: " + url);
139+
log("Benchmarks: " + name + ":" + benchmarkName);
140+
141+
await injectScript(url);
142+
143+
window.go = undefined;
144+
await injectScript(`src/${name}.js`);
145+
146+
suite = go();
147+
148+
let bench;
149+
for (const key in suite) {
150+
if (parseInt(key) >= 0) {
151+
const b = suite[key];
152+
if (b.name === benchmarkName) {
153+
bench = b;
154+
}
155+
}
156+
}
157+
158+
if (!bench) {
159+
alert("Benchmark not found.");
160+
return;
161+
}
162+
163+
const iterations = parseInt(iterationsInput.value.replace(/_/g, "")) || 1e4;
164+
log("Iterations: " + iterations);
165+
166+
const f = bench.fn;
167+
console.profile(benchmarkName);
168+
for (let i = 0; i < iterations; i++) {
169+
f();
170+
}
171+
console.profileEnd(benchmarkName);
172+
}
173+
115174
function injectScript(src) {
116175
return new Promise((resolve, reject) => {
117176
const script = document.createElement('script');
@@ -121,3 +180,5 @@ function injectScript(src) {
121180
document.head.appendChild(script);
122181
});
123182
}
183+
184+

0 commit comments

Comments
 (0)