-
-
Notifications
You must be signed in to change notification settings - Fork 36
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
perf: use samplesLength
instead of multiple use samples.length
#59
Changes from 1 commit
da31817
c4f5f5a
5eb29e2
e9e883c
3bfe822
60e45cb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -57,6 +57,8 @@ export default class Task extends EventTarget { | |
async run() { | ||
this.dispatchEvent(createBenchEvent('start', this)); | ||
let totalTime = 0; // ms | ||
let min = Infinity; | ||
let max = -Infinity; | ||
const samples: number[] = []; | ||
|
||
await this.bench.setup(this, 'run'); | ||
|
@@ -90,9 +92,11 @@ export default class Task extends EventTarget { | |
taskTime = this.bench.now() - taskStart; | ||
} | ||
|
||
samples.push(taskTime); | ||
this.runs += 1; | ||
totalTime += taskTime; | ||
samples.push(taskTime); | ||
if (taskTime > max) max = taskTime; | ||
if (taskTime < min) min = taskTime; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what do these lines do exactly? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Get the max and min values, This way we don't have to call |
||
|
||
if (this.opts.afterEach != null) { | ||
await this.opts.afterEach.call(this); | ||
|
@@ -112,29 +116,26 @@ export default class Task extends EventTarget { | |
|
||
await this.bench.teardown(this, 'run'); | ||
|
||
samples.sort((a, b) => a - b); | ||
|
||
if (!this.result?.error) { | ||
const min = samples[0]!; | ||
const max = samples[samples.length - 1]!; | ||
const period = totalTime / this.runs; | ||
const hz = 1000 / period; | ||
|
||
// benchmark.js: https://github.com/bestiejs/benchmark.js/blob/42f3b732bac3640eddb3ae5f50e445f3141016fd/benchmark.js#L1912-L1927 | ||
const simplesLength = samples.length; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
const mean = getMean(samples); | ||
const variance = getVariance(samples, mean); | ||
const sd = Math.sqrt(variance); | ||
const sem = sd / Math.sqrt(samples.length); | ||
const df = samples.length - 1; | ||
const sem = sd / Math.sqrt(simplesLength); | ||
const df = simplesLength - 1; | ||
const critical = tTable[String(Math.round(df) || 1)] || tTable.infinity!; | ||
const moe = sem * critical; | ||
const rme = (moe / mean) * 100 || 0; | ||
|
||
// mitata: https://github.com/evanwashere/mitata/blob/3730a784c9d83289b5627ddd961e3248088612aa/src/lib.mjs#L12 | ||
const p75 = samples[Math.ceil(samples.length * (75 / 100)) - 1]!; | ||
const p99 = samples[Math.ceil(samples.length * (99 / 100)) - 1]!; | ||
const p995 = samples[Math.ceil(samples.length * (99.5 / 100)) - 1]!; | ||
const p999 = samples[Math.ceil(samples.length * (99.9 / 100)) - 1]!; | ||
const p75 = samples[Math.ceil(simplesLength * (75 / 100)) - 1]!; | ||
const p99 = samples[Math.ceil(simplesLength * (99 / 100)) - 1]!; | ||
const p995 = samples[Math.ceil(simplesLength * (99.5 / 100)) - 1]!; | ||
const p999 = samples[Math.ceil(simplesLength * (99.9 / 100)) - 1]!; | ||
|
||
if (this.bench.signal?.aborted) { | ||
return this; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if we want to get the min and max, we need to return them actually, and when you do, add it to the documentation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And aren't they in the wrong order? min should be -Infinity.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I do not understand what you're saying. There were min and max in the first place just a different way to get them.
I think you may have misunderstood. If min is -Infinity it means that there is no value smaller than this min
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh I get it now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, we used to get them by samples[samples.length] for instance, but since we do not sort now, we need to return them along other results like
mean
,variance
, ...There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, I got it. Looks like we still need to call
sort()
otherwise the result will be incorrect