2016-08-03 18:00:07 -04:00
|
|
|
/**
|
|
|
|
* @license
|
|
|
|
* Copyright Google Inc. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by an MIT-style license that can be
|
|
|
|
* found in the LICENSE file at https://angular.io/license
|
|
|
|
*/
|
2015-05-27 17:57:54 -04:00
|
|
|
|
2016-08-03 18:00:07 -04:00
|
|
|
import {AsyncTestCompleter, afterEach, beforeEach, ddescribe, describe, expect, iit, inject, it, xit} from '@angular/core/testing/testing_internal';
|
2016-08-26 19:34:08 -04:00
|
|
|
|
|
|
|
import {MeasureValues, Metric, Options, ReflectiveInjector, Reporter, Sampler, Validator, WebDriverAdapter} from '../index';
|
|
|
|
import {Date, DateWrapper, isBlank, isPresent, stringify} from '../src/facade/lang';
|
2015-05-27 17:57:54 -04:00
|
|
|
|
|
|
|
export function main() {
|
|
|
|
var EMPTY_EXECUTE = () => {};
|
|
|
|
|
|
|
|
describe('sampler', () => {
|
2016-02-19 14:49:31 -05:00
|
|
|
var sampler: Sampler;
|
2015-05-27 17:57:54 -04:00
|
|
|
|
|
|
|
function createSampler({driver, metric, reporter, validator, prepare, execute}: {
|
|
|
|
driver?: any,
|
|
|
|
metric?: Metric,
|
|
|
|
reporter?: Reporter,
|
|
|
|
validator?: Validator,
|
|
|
|
prepare?: any,
|
|
|
|
execute?: any
|
|
|
|
} = {}) {
|
|
|
|
var time = 1000;
|
|
|
|
if (isBlank(metric)) {
|
|
|
|
metric = new MockMetric([]);
|
|
|
|
}
|
|
|
|
if (isBlank(reporter)) {
|
|
|
|
reporter = new MockReporter([]);
|
|
|
|
}
|
|
|
|
if (isBlank(driver)) {
|
|
|
|
driver = new MockDriverAdapter([]);
|
|
|
|
}
|
2016-03-21 06:57:17 -04:00
|
|
|
var providers = [
|
2016-08-03 18:00:07 -04:00
|
|
|
Options.DEFAULT_PROVIDERS, Sampler.PROVIDERS, {provide: Metric, useValue: metric},
|
|
|
|
{provide: Reporter, useValue: reporter}, {provide: WebDriverAdapter, useValue: driver},
|
|
|
|
{provide: Options.EXECUTE, useValue: execute}, {provide: Validator, useValue: validator},
|
2016-06-02 20:30:40 -04:00
|
|
|
{provide: Options.NOW, useValue: () => DateWrapper.fromMillis(time++)}
|
2015-05-27 17:57:54 -04:00
|
|
|
];
|
|
|
|
if (isPresent(prepare)) {
|
2016-06-02 20:30:40 -04:00
|
|
|
providers.push({provide: Options.PREPARE, useValue: prepare});
|
2015-05-27 17:57:54 -04:00
|
|
|
}
|
|
|
|
|
2016-03-21 06:57:17 -04:00
|
|
|
sampler = ReflectiveInjector.resolveAndCreate(providers).get(Sampler);
|
2015-05-27 17:57:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
it('should call the prepare and execute callbacks using WebDriverAdapter.waitFor',
|
2016-08-26 19:34:08 -04:00
|
|
|
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
|
|
|
|
var log: any[] = [];
|
2015-05-27 17:57:54 -04:00
|
|
|
var count = 0;
|
2016-08-26 19:34:08 -04:00
|
|
|
var driver = new MockDriverAdapter([], (callback: Function) => {
|
2015-05-27 17:57:54 -04:00
|
|
|
var result = callback();
|
2015-06-17 14:17:21 -04:00
|
|
|
log.push(result);
|
2016-08-02 18:53:34 -04:00
|
|
|
return Promise.resolve(result);
|
2015-05-27 17:57:54 -04:00
|
|
|
});
|
|
|
|
createSampler({
|
|
|
|
driver: driver,
|
|
|
|
validator: createCountingValidator(2),
|
|
|
|
prepare: () => { return count++; },
|
|
|
|
execute: () => { return count++; }
|
|
|
|
});
|
|
|
|
sampler.sample().then((_) => {
|
|
|
|
expect(count).toBe(4);
|
|
|
|
expect(log).toEqual([0, 1, 2, 3]);
|
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
it('should call prepare, beginMeasure, execute, endMeasure for every iteration',
|
2016-08-26 19:34:08 -04:00
|
|
|
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
|
2015-05-27 17:57:54 -04:00
|
|
|
var workCount = 0;
|
2016-08-26 19:34:08 -04:00
|
|
|
var log: any[] = [];
|
2015-05-27 17:57:54 -04:00
|
|
|
createSampler({
|
|
|
|
metric: createCountingMetric(log),
|
|
|
|
validator: createCountingValidator(2),
|
2015-06-17 14:17:21 -04:00
|
|
|
prepare: () => { log.push(`p${workCount++}`); },
|
|
|
|
execute: () => { log.push(`w${workCount++}`); }
|
2015-05-27 17:57:54 -04:00
|
|
|
});
|
|
|
|
sampler.sample().then((_) => {
|
|
|
|
expect(log).toEqual([
|
|
|
|
'p0',
|
|
|
|
['beginMeasure'],
|
|
|
|
'w1',
|
|
|
|
['endMeasure', false, {'script': 0}],
|
|
|
|
'p2',
|
|
|
|
['beginMeasure'],
|
|
|
|
'w3',
|
|
|
|
['endMeasure', false, {'script': 1}],
|
|
|
|
]);
|
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
|
|
|
|
it('should call execute, endMeasure for every iteration if there is no prepare callback',
|
2016-08-26 19:34:08 -04:00
|
|
|
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
|
|
|
|
var log: any[] = [];
|
2015-05-27 17:57:54 -04:00
|
|
|
var workCount = 0;
|
|
|
|
createSampler({
|
|
|
|
metric: createCountingMetric(log),
|
|
|
|
validator: createCountingValidator(2),
|
2015-06-17 14:17:21 -04:00
|
|
|
execute: () => { log.push(`w${workCount++}`); },
|
2015-05-27 17:57:54 -04:00
|
|
|
prepare: null
|
|
|
|
});
|
|
|
|
sampler.sample().then((_) => {
|
|
|
|
expect(log).toEqual([
|
|
|
|
['beginMeasure'],
|
|
|
|
'w0',
|
|
|
|
['endMeasure', true, {'script': 0}],
|
|
|
|
'w1',
|
|
|
|
['endMeasure', true, {'script': 1}],
|
|
|
|
]);
|
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
|
|
|
|
it('should only collect metrics for execute and ignore metrics from prepare',
|
2016-08-26 19:34:08 -04:00
|
|
|
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
|
2015-05-27 17:57:54 -04:00
|
|
|
var scriptTime = 0;
|
|
|
|
var iterationCount = 1;
|
|
|
|
createSampler({
|
|
|
|
validator: createCountingValidator(2),
|
2016-08-03 18:00:07 -04:00
|
|
|
metric: new MockMetric(
|
|
|
|
[],
|
|
|
|
() => {
|
2016-08-02 18:53:34 -04:00
|
|
|
var result = Promise.resolve({'script': scriptTime});
|
2016-08-03 18:00:07 -04:00
|
|
|
scriptTime = 0;
|
|
|
|
return result;
|
|
|
|
}),
|
2015-05-27 17:57:54 -04:00
|
|
|
prepare: () => { scriptTime = 1 * iterationCount; },
|
2015-06-03 16:42:57 -04:00
|
|
|
execute: () => {
|
|
|
|
scriptTime = 10 * iterationCount;
|
|
|
|
iterationCount++;
|
|
|
|
}
|
2015-05-27 17:57:54 -04:00
|
|
|
});
|
|
|
|
sampler.sample().then((state) => {
|
|
|
|
expect(state.completeSample.length).toBe(2);
|
|
|
|
expect(state.completeSample[0]).toEqual(mv(0, 1000, {'script': 10}));
|
|
|
|
expect(state.completeSample[1]).toEqual(mv(1, 1001, {'script': 20}));
|
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
|
|
|
|
it('should call the validator for every execution and store the valid sample',
|
2016-08-26 19:34:08 -04:00
|
|
|
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
|
|
|
|
var log: any[] = [];
|
|
|
|
var validSample = [mv(null, null, {})];
|
2015-05-27 17:57:54 -04:00
|
|
|
|
|
|
|
createSampler({
|
|
|
|
metric: createCountingMetric(),
|
|
|
|
validator: createCountingValidator(2, validSample, log),
|
|
|
|
execute: EMPTY_EXECUTE
|
|
|
|
});
|
|
|
|
sampler.sample().then((state) => {
|
|
|
|
expect(state.validSample).toBe(validSample);
|
|
|
|
// TODO(tbosch): Why does this fail??
|
|
|
|
// expect(log).toEqual([
|
|
|
|
// ['validate', [{'script': 0}], null],
|
|
|
|
// ['validate', [{'script': 0}, {'script': 1}], validSample]
|
|
|
|
// ]);
|
|
|
|
|
|
|
|
expect(log.length).toBe(2);
|
|
|
|
expect(log[0]).toEqual(['validate', [mv(0, 1000, {'script': 0})], null]);
|
2015-06-03 16:42:57 -04:00
|
|
|
expect(log[1]).toEqual(
|
|
|
|
['validate', [mv(0, 1000, {'script': 0}), mv(1, 1001, {'script': 1})], validSample]);
|
2015-05-27 17:57:54 -04:00
|
|
|
|
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
|
2016-08-26 19:34:08 -04:00
|
|
|
it('should report the metric values',
|
|
|
|
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
|
|
|
|
var log: any[] = [];
|
|
|
|
var validSample = [mv(null, null, {})];
|
2015-05-27 17:57:54 -04:00
|
|
|
createSampler({
|
|
|
|
validator: createCountingValidator(2, validSample),
|
|
|
|
metric: createCountingMetric(),
|
|
|
|
reporter: new MockReporter(log),
|
|
|
|
execute: EMPTY_EXECUTE
|
|
|
|
});
|
|
|
|
sampler.sample().then((_) => {
|
|
|
|
// TODO(tbosch): Why does this fail??
|
|
|
|
// expect(log).toEqual([
|
|
|
|
// ['reportMeasureValues', 0, {'script': 0}],
|
|
|
|
// ['reportMeasureValues', 1, {'script': 1}],
|
|
|
|
// ['reportSample', [{'script': 0}, {'script': 1}], validSample]
|
|
|
|
// ]);
|
|
|
|
expect(log.length).toBe(3);
|
|
|
|
expect(log[0]).toEqual(['reportMeasureValues', mv(0, 1000, {'script': 0})]);
|
|
|
|
expect(log[1]).toEqual(['reportMeasureValues', mv(1, 1001, {'script': 1})]);
|
|
|
|
expect(log[2]).toEqual([
|
2016-08-03 18:00:07 -04:00
|
|
|
'reportSample', [mv(0, 1000, {'script': 0}), mv(1, 1001, {'script': 1})], validSample
|
2015-05-27 17:57:54 -04:00
|
|
|
]);
|
|
|
|
|
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-08-26 19:34:08 -04:00
|
|
|
function mv(runIndex: number, time: number, values: {[key: string]: number}) {
|
2015-05-27 17:57:54 -04:00
|
|
|
return new MeasureValues(runIndex, DateWrapper.fromMillis(time), values);
|
|
|
|
}
|
|
|
|
|
2016-08-26 19:34:08 -04:00
|
|
|
function createCountingValidator(
|
|
|
|
count: number, validSample: MeasureValues[] = null, log: any[] = []) {
|
|
|
|
return new MockValidator(log, (completeSample: MeasureValues[]) => {
|
2015-05-27 17:57:54 -04:00
|
|
|
count--;
|
|
|
|
if (count === 0) {
|
|
|
|
return isPresent(validSample) ? validSample : completeSample;
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-08-26 19:34:08 -04:00
|
|
|
function createCountingMetric(log: any[] = []) {
|
2015-05-27 17:57:54 -04:00
|
|
|
var scriptTime = 0;
|
|
|
|
return new MockMetric(log, () => { return {'script': scriptTime++}; });
|
|
|
|
}
|
|
|
|
|
|
|
|
class MockDriverAdapter extends WebDriverAdapter {
|
2016-08-26 19:34:08 -04:00
|
|
|
constructor(private _log: any[] = [], private _waitFor: Function = null) { super(); }
|
2015-05-27 17:57:54 -04:00
|
|
|
waitFor(callback: Function): Promise<any> {
|
|
|
|
if (isPresent(this._waitFor)) {
|
|
|
|
return this._waitFor(callback);
|
|
|
|
} else {
|
2016-08-02 18:53:34 -04:00
|
|
|
return Promise.resolve(callback());
|
2015-05-27 17:57:54 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class MockValidator extends Validator {
|
2016-08-26 19:34:08 -04:00
|
|
|
constructor(private _log: any[] = [], private _validate: Function = null) { super(); }
|
2015-08-28 14:29:19 -04:00
|
|
|
validate(completeSample: MeasureValues[]): MeasureValues[] {
|
2015-05-27 17:57:54 -04:00
|
|
|
var stableSample = isPresent(this._validate) ? this._validate(completeSample) : completeSample;
|
2015-06-17 14:17:21 -04:00
|
|
|
this._log.push(['validate', completeSample, stableSample]);
|
2015-05-27 17:57:54 -04:00
|
|
|
return stableSample;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class MockMetric extends Metric {
|
2016-08-26 19:34:08 -04:00
|
|
|
constructor(private _log: any[] = [], private _endMeasure: Function = null) { super(); }
|
2015-05-27 17:57:54 -04:00
|
|
|
beginMeasure() {
|
2015-06-17 14:17:21 -04:00
|
|
|
this._log.push(['beginMeasure']);
|
2016-08-02 18:53:34 -04:00
|
|
|
return Promise.resolve(null);
|
2015-05-27 17:57:54 -04:00
|
|
|
}
|
2016-08-26 19:34:08 -04:00
|
|
|
endMeasure(restart: boolean) {
|
2015-05-27 17:57:54 -04:00
|
|
|
var measureValues = isPresent(this._endMeasure) ? this._endMeasure() : {};
|
2015-06-17 14:17:21 -04:00
|
|
|
this._log.push(['endMeasure', restart, measureValues]);
|
2016-08-02 18:53:34 -04:00
|
|
|
return Promise.resolve(measureValues);
|
2015-05-27 17:57:54 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class MockReporter extends Reporter {
|
2016-08-26 19:34:08 -04:00
|
|
|
constructor(private _log: any[] = []) { super(); }
|
|
|
|
reportMeasureValues(values: MeasureValues): Promise<any> {
|
2015-06-17 14:17:21 -04:00
|
|
|
this._log.push(['reportMeasureValues', values]);
|
2016-08-02 18:53:34 -04:00
|
|
|
return Promise.resolve(null);
|
2015-05-27 17:57:54 -04:00
|
|
|
}
|
2016-08-26 19:34:08 -04:00
|
|
|
reportSample(completeSample: MeasureValues[], validSample: MeasureValues[]): Promise<any> {
|
2015-06-17 14:17:21 -04:00
|
|
|
this._log.push(['reportSample', completeSample, validSample]);
|
2016-08-02 18:53:34 -04:00
|
|
|
return Promise.resolve(null);
|
2015-05-27 17:57:54 -04:00
|
|
|
}
|
|
|
|
}
|