-
Notifications
You must be signed in to change notification settings - Fork 30k
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
benchmark: add progress indicator to compare.js #10823
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
'use strict'; | ||
|
||
const readline = require('readline'); | ||
|
||
function pad(input, minLength, fill) { | ||
var result = input + ''; | ||
return fill.repeat(Math.max(0, minLength - result.length)) + result; | ||
} | ||
|
||
function fraction(numerator, denominator) { | ||
const fdenominator = denominator + ''; | ||
const fnumerator = pad(numerator, fdenominator.length, ' '); | ||
return `${fnumerator}/${fdenominator}`; | ||
} | ||
|
||
function getTime(diff) { | ||
const time = Math.ceil(diff[0] + diff[1] / 1e9); | ||
const seconds = pad(time % 60, 2, '0'); | ||
const minutes = pad(Math.floor(time / 60) % (60 * 60), 2, '0'); | ||
const hours = pad(Math.floor(time / (60 * 60)), 2, '0'); | ||
return `${hours}:${minutes}:${seconds}`; | ||
} | ||
|
||
// A run is an item in the job queue: { binary, filename, iter } | ||
// A config is an item in the subqueue: { binary, filename, iter, configs } | ||
class BenchmarkProgress { | ||
constructor(queue, benchmarks) { | ||
this.queue = queue; // Scheduled runs. | ||
this.benchmarks = benchmarks; // Filenames of scheduled benchmarks. | ||
this.completedRuns = 0; // Number of completed runs. | ||
this.scheduledRuns = queue.length; // Number of scheduled runs. | ||
// Time when starting to run benchmarks. | ||
this.startTime = process.hrtime(); | ||
// Number of times each file will be run (roughly). | ||
this.runsPerFile = queue.length / benchmarks.length; | ||
this.currentFile = ''; // Filename of current benchmark. | ||
this.currentFileConfig; // Configurations for current file | ||
// Number of configurations already run for the current file. | ||
this.completedConfig = 0; | ||
// Total number of configurations for the current file | ||
this.scheduledConfig = 0; | ||
this.interval = 0; // result of setInterval for updating the elapsed time | ||
} | ||
|
||
startQueue(index) { | ||
this.kStartOfQueue = index; | ||
this.currentFile = this.queue[index].filename; | ||
this.interval = setInterval(() => { | ||
if (this.completedRuns === this.scheduledRuns) { | ||
clearInterval(this.interval); | ||
} else { | ||
this.updateProgress(); | ||
} | ||
}, 1000); | ||
} | ||
|
||
startSubqueue(data, index) { | ||
// This subqueue is generated by a new benchmark | ||
if (data.name !== this.currentFile || index === this.kStartOfQueue) { | ||
this.currentFile = data.name; | ||
this.scheduledConfig = data.queueLength; | ||
} | ||
this.completedConfig = 0; | ||
this.updateProgress(); | ||
} | ||
|
||
completeConfig(data) { | ||
this.completedConfig++; | ||
this.updateProgress(); | ||
} | ||
|
||
completeRun(job) { | ||
this.completedRuns++; | ||
this.updateProgress(); | ||
} | ||
|
||
getProgress() { | ||
// Get time as soon as possible. | ||
const diff = process.hrtime(this.startTime); | ||
|
||
const completedRuns = this.completedRuns; | ||
const scheduledRuns = this.scheduledRuns; | ||
const finished = completedRuns === scheduledRuns; | ||
|
||
// Calculate numbers for fractions. | ||
const runsPerFile = this.runsPerFile; | ||
const completedFiles = Math.floor(completedRuns / runsPerFile); | ||
const scheduledFiles = this.benchmarks.length; | ||
const completedRunsForFile = finished ? runsPerFile : | ||
completedRuns % runsPerFile; | ||
const completedConfig = this.completedConfig; | ||
const scheduledConfig = this.scheduledConfig; | ||
|
||
// Calculate the percentage. | ||
let runRate = 0; // Rate of current incomplete run. | ||
if (completedConfig !== scheduledConfig) { | ||
runRate = completedConfig / scheduledConfig; | ||
} | ||
const completedRate = ((completedRuns + runRate) / scheduledRuns); | ||
const percent = pad(Math.floor(completedRate * 100), 3, ' '); | ||
|
||
const caption = finished ? 'Done\n' : this.currentFile; | ||
return `[${getTime(diff)}|% ${percent}` + | ||
`| ${fraction(completedFiles, scheduledFiles)} files ` + | ||
`| ${fraction(completedRunsForFile, runsPerFile)} runs ` + | ||
`| ${fraction(completedConfig, scheduledConfig)} configs]` + | ||
`: ${caption}`; | ||
} | ||
|
||
updateProgress(finished) { | ||
if (!process.stderr.isTTY || process.stdout.isTTY) { | ||
return; | ||
} | ||
readline.clearLine(process.stderr); | ||
readline.cursorTo(process.stderr, 0); | ||
process.stderr.write(this.getProgress()); | ||
} | ||
} | ||
|
||
module.exports = BenchmarkProgress; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Why is this interval required? It seams to me that all state updating functions calls
updateProgress
.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.
Presumably to at least regularly update the elapsed time.
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.
Ah, of cause!
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, see the comments of
interval
in the constructor.