Skip to content

Commit

Permalink
Add after()
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky committed Jul 30, 2019
1 parent d343895 commit 3441d73
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 6 deletions.
4 changes: 3 additions & 1 deletion src/child.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ const run = async function() {

const duration = await getParentMessage('run')

const result = await benchmark(main, before, duration)
const result = await benchmark(main, before, after, duration)
await sendParentMessage('result', result)
}

const before = undefined

const main = Math.random

const after = undefined

run()
38 changes: 33 additions & 5 deletions src/temp.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { getResult, getMedian, sortNumbers, MAX_LOOPS } from './stats.js'

// Measure how long a task takes.
// Run the benchmark for a specific amount of time.
export const benchmark = async function(main, before, duration) {
export const benchmark = async function(main, before, after, duration) {
initialMeasure()

const biasDuration = duration * BIAS_DURATION_RATIO
Expand All @@ -15,6 +15,7 @@ export const benchmark = async function(main, before, duration) {
const result = await benchmarkMain(
main,
before,
after,
mainDuration,
nowBias,
loopBias,
Expand All @@ -28,7 +29,7 @@ export const benchmark = async function(main, before, duration) {
// next functions passed to it.
// We fix this by doing a cold start using an empty function
const initialMeasure = function() {
measure(noopTwo, undefined, 0, 0, 1)
measure(noopTwo, undefined, undefined, 0, 0, 1)
}

// eslint-disable-next-line no-empty-function
Expand Down Expand Up @@ -58,6 +59,7 @@ const getNowBias = async function(biasDuration) {
const { times } = await benchmarkMain(
noop,
undefined,
undefined,
biasDuration,
0,
0,
Expand All @@ -72,6 +74,7 @@ const getLoopBias = async function(biasDuration, nowBias, minTime) {
const { times } = await benchmarkMain(
noop,
undefined,
undefined,
biasDuration,
nowBias,
0,
Expand Down Expand Up @@ -106,6 +109,7 @@ const MIN_NOW_BIAS = 1e2
const benchmarkMain = async function(
main,
before,
after,
duration,
nowBias,
loopBias,
Expand All @@ -117,6 +121,7 @@ const benchmarkMain = async function(
const { times, count } = await benchmarkLoop(
main,
before,
after,
nowBias,
loopBias,
minTime,
Expand All @@ -135,6 +140,7 @@ const benchmarkMain = async function(
const benchmarkLoop = async function(
main,
before,
after,
nowBias,
loopBias,
minTime,
Expand Down Expand Up @@ -171,7 +177,15 @@ const benchmarkLoop = async function(
)

// eslint-disable-next-line no-await-in-loop
const time = await measure(main, before, nowBias, loopBias, repeat, isAsync)
const time = await measure(
main,
before,
after,
nowBias,
loopBias,
repeat,
isAsync,
)

// eslint-disable-next-line fp/no-mutating-methods
times.push(time)
Expand Down Expand Up @@ -203,6 +217,7 @@ const shouldStop = function(runEnd, times) {
const measure = async function(
main,
before,
after,
nowBias,
loopBias,
repeat,
Expand All @@ -213,17 +228,19 @@ const measure = async function(
return Math.abs(now() - now())
}

const beforeArgs = await getBeforeArgs(before, repeat)
const beforeArgs = await performBefore(before, repeat)

const duration = await getDuration(main, repeat, isAsync, beforeArgs)

await performAfter(after, repeat, beforeArgs)

// The final time might be negative if the task is as fast or faster than the
// iteration code itself. In this case, we return `0`.
const time = Math.max((duration - nowBias) / repeat - loopBias, 0)
return time
}

const getBeforeArgs = function(before, repeat) {
const performBefore = function(before, repeat) {
if (before === undefined) {
return
}
Expand All @@ -232,6 +249,17 @@ const getBeforeArgs = function(before, repeat) {
return Promise.all(beforeArgs)
}

const performAfter = function(after, repeat, beforeArgs = []) {
if (after === undefined) {
return
}

const promises = Array.from({ length: repeat }, (value, index) =>
after(beforeArgs[index]),
)
return Promise.all(promises)
}

// We separate async and sync measurements because following a promise (`await`)
// takes several microseconds, which does not work when measuring fast
// synchronous functions.
Expand Down

0 comments on commit 3441d73

Please sign in to comment.