Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky committed Aug 2, 2019
1 parent bcb3ba2 commit 47b4bff
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 72 deletions.
73 changes: 27 additions & 46 deletions src/info/group.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,66 +2,47 @@ import { groupBy } from '../utils/group.js'
import { sortBy } from '../utils/sort.js'
import { getMean } from '../stats/methods.js'

// Group iterations by `taskId` and compute:
// - `taskMean`: mean of all iterations medians (of the same task)
// - `taskRank`: rank among all `taskMean`
export const getTaskGroups = function(iterations) {
return groupIterations({
iterations,
groupProp: 'taskId',
meanProp: 'taskMean',
rankProp: 'taskRank',
})
// Retrieve all tasks.
// Also compute the mean of all iterations medians (of the same task)
// The array is sorted by mean.
export const getTasks = function(iterations) {
const tasks = groupIterations(iterations, 'taskId')
const tasksA = tasks.map(getTask)
return tasksA
}

// Group iterations by `parameter` and compute:
// - `parameterMean`: mean of all iterations medians (of the same task)
// - `parameterRank`: rank among all `taskMean`
export const getParameterGroups = function(iterations) {
return groupIterations({
iterations,
groupProp: 'parameter',
meanProp: 'parameterMean',
rankProp: 'parameterRank',
})
const getTask = function({ groupId: taskId, mean, iteration: { title } }) {
return { taskId, title, mean }
}

const groupIterations = function({
iterations,
groupProp,
meanProp,
rankProp,
}) {
const groups = Object.entries(groupBy(iterations, groupProp))
// Retrieve all parameters.
// Also compute the mean of all iterations medians (of the same parameter)
// The array is sorted by mean.
export const getParameters = function(iterations) {
const parameters = groupIterations(iterations, 'parameter')
const parametersA = parameters.map(getParameter)
return parametersA
}

const getParameter = function({ groupId: parameter, mean }) {
return { parameter, mean }
}

const groupIterations = function(iterations, groupId) {
const groups = Object.entries(groupBy(iterations, groupId))

const groupsA = groups.map(getGroupMean)
sortBy(groupsA, ['mean'])
const groupsB = groupsA
.map(addRank)
.map(group => renameProps({ group, meanProp, rankProp }))

const groupsC = Object.fromEntries(groupsB)
return groupsC
return groupsA
}

const getGroupMean = function([groupId, iterations]) {
const medians = iterations.map(getIterationMedian)
const mean = getMean(medians)
return { groupId, mean }
const [iteration] = iterations
return { groupId, mean, iteration }
}

const getIterationMedian = function({ stats: { median } }) {
return median
}

const addRank = function({ groupId, mean }, rank) {
return { groupId, mean, rank }
}

const renameProps = function({
group: { groupId, mean, rank },
meanProp,
rankProp,
}) {
return [groupId, { [meanProp]: mean, [rankProp]: rank }]
}
29 changes: 15 additions & 14 deletions src/info/main.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,29 @@
import { sortBy } from '../utils/sort.js'

import { getTaskGroups, getParameterGroups } from './group.js'
import { getTasks, getParameters } from './group.js'

// Add more information to the final benchmark and normalize/sort results
export const addBenchmarkInfo = function({ iterations, ...benchmark }) {
const taskGroups = getTaskGroups(iterations)
const parameterGroups = getParameterGroups(iterations)
export const addBenchmarkInfo = function({ benchmark: { iterations }, opts }) {
const tasks = getTasks(iterations)
const parameters = getParameters(iterations)

const iterationsA = iterations.map(iteration =>
addIterationInfo({ iteration, taskGroups, parameterGroups }),
addIterationInfo({ iteration, tasks, parameters }),
)

sortBy(iterationsA, ['taskRank', 'stats.median'])
// The fastest tasks will be first, then the fastest iterations within each
// task
sortBy(iterationsA, ['task', 'stats.median'])

return { ...benchmark, iterations: iterationsA }
return { opts, tasks, parameters, iterations: iterationsA }
}

const addIterationInfo = function({
iteration,
iteration: { taskId, parameter },
taskGroups,
parameterGroups,
iteration: { name, taskId, parameter, stats },
tasks,
parameters,
}) {
const { taskMean, taskRank } = taskGroups[taskId]
const { parameterMean, parameterRank } = parameterGroups[parameter]
return { ...iteration, taskRank, taskMean, parameterRank, parameterMean }
const taskA = tasks.findIndex(task => task.taskId === taskId)
const parameterA = parameters.findIndex(task => task.parameter === parameter)
return { name, task: taskA, parameter: parameterA, stats }
}
12 changes: 4 additions & 8 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,9 @@ const spyd = async function(opts) {
opts: optsA,
})

const benchmark = await getBenchmark({
iterations,
progressState,
opts: optsA,
})
const benchmark = await getBenchmark(iterations, progressState)

const benchmarkA = addBenchmarkInfo(benchmark)
const benchmarkA = addBenchmarkInfo({ benchmark, opts: optsA })

await stopProgress(progressInfo)

Expand All @@ -33,11 +29,11 @@ const spyd = async function(opts) {
return benchmarkA
}

const getBenchmark = async function({ iterations, progressState, opts }) {
const getBenchmark = async function(iterations, progressState) {
const iterationsA = await pMapSeries(iterations, (iteration, index) =>
runProcesses({ ...iteration, index, progressState }),
)
return { iterations: iterationsA, opts }
return { iterations: iterationsA }
}

// We do not use `export default` because Babel transpiles it in a way that
Expand Down
8 changes: 4 additions & 4 deletions src/processes/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import { shouldStop } from './stop.js'
// Each task (and parameter combination) is run serially to avoid influencing
// the timing of another.
export const runProcesses = async function({
name,
taskPath,
taskId,
name,
title,
parameter,
index,
Expand All @@ -37,8 +37,8 @@ export const runProcesses = async function({

const iteration = getIterationResult({
results,
taskId,
name,
taskId,
title,
parameter,
})
Expand Down Expand Up @@ -116,11 +116,11 @@ const addTaskInfo = function({ error, taskId, parameter }) {
// Convert results to `benchmark.iterations[*]` object passed to reporters
const getIterationResult = function({
results,
taskId,
name,
taskId,
title,
parameter,
}) {
const stats = getStats(results)
return { taskId, name, title, parameter, stats }
return { name, taskId, title, parameter, stats }
}

0 comments on commit 47b4bff

Please sign in to comment.