Skip to content

Commit

Permalink
feat: Added warmup option.
Browse files Browse the repository at this point in the history
  • Loading branch information
Shogun committed Mar 5, 2020
1 parent f8c08ba commit 60fa5da
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ The supported options are the following:

- `iterations`: The number of iterations to run for each test. Must be a positive number. The default is `10000`.
- `errorThreshold`: If active, it stops the test run before the desider number of iterations if the standard error is below the provided value and at least 10% of the iterations have been run. Must be a number between `0` (which disables this option) and `100`. The default is `1`.
- `warmup`: Run the suite twice, the first time without collecting results. The default is `true`.
- `print`: If print results on the console in a pretty tabular way. The default is `true`. It can be a boolean or a printing options object. The supported printing options are:
- `colors`: If use colors. Default is `true`.
- `compare`: If compare tests in the output. Default is `false`.
Expand Down
23 changes: 21 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,13 @@ function cronometro(tests, options, callback) {
};
}
// Parse and validate options
const { iterations, errorThreshold, print } = { iterations: 1e4, errorThreshold: 1, print: true, ...options };
const { iterations, errorThreshold, print, warmup } = {
iterations: 1e4,
warmup: true,
errorThreshold: 1,
print: true,
...options
};
// tslint:disable-next-line strict-type-predicates
if (typeof iterations !== 'number' || iterations < 1) {
callback(new Error('The iterations option must be a positive number.'));
Expand Down Expand Up @@ -144,7 +150,20 @@ function cronometro(tests, options, callback) {
callback(null, results);
}
};
schedule(() => processQueue(context));
const boot = {
queue: warmup ? context.queue.slice(0) : [],
results: {},
iterations,
errorThreshold: errorThreshold / 100,
callback(error) {
if (error) {
callback(error);
return;
}
schedule(() => processQueue(context));
}
};
schedule(() => processQueue(boot));
return promise;
}
exports.cronometro = cronometro;
Expand Down
26 changes: 24 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ function processQueue(context: Context): void {
}

const testContext = context as TestContext

testContext.current = {
name: next[0],
test: next[1],
Expand Down Expand Up @@ -140,7 +141,13 @@ export function cronometro(
}

// Parse and validate options
const { iterations, errorThreshold, print } = { iterations: 1e4, errorThreshold: 1, print: true, ...options }
const { iterations, errorThreshold, print, warmup } = {
iterations: 1e4,
warmup: true,
errorThreshold: 1,
print: true,
...options
}

// tslint:disable-next-line strict-type-predicates
if (typeof iterations !== 'number' || iterations < 1) {
Expand Down Expand Up @@ -181,7 +188,22 @@ export function cronometro(
}
}

schedule(() => processQueue(context))
const boot: Context = {
queue: warmup ? context.queue.slice(0) : [],
results: {},
iterations,
errorThreshold: errorThreshold / 100,
callback(error?: Error | null): void {
if (error) {
callback!(error)
return
}

schedule(() => processQueue(context))
}
}

schedule(() => processQueue(boot))

return promise
}
Expand Down
1 change: 1 addition & 0 deletions src/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export interface PrintOptions {
export interface Options {
iterations: number
print: boolean | PrintOptions
warmup: boolean
}

export type StaticTest = () => any
Expand Down
1 change: 1 addition & 0 deletions types/models.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export interface PrintOptions {
export interface Options {
iterations: number;
print: boolean | PrintOptions;
warmup: boolean;
}
export declare type StaticTest = () => any;
export declare type AsyncTest = (cb: Callback) => any;
Expand Down

0 comments on commit 60fa5da

Please sign in to comment.