2015-03-13 06:10:11 -04:00
|
|
|
import {
|
|
|
|
afterEach,
|
|
|
|
AsyncTestCompleter,
|
|
|
|
beforeEach,
|
|
|
|
ddescribe,
|
|
|
|
describe,
|
|
|
|
expect,
|
|
|
|
iit,
|
|
|
|
inject,
|
|
|
|
it,
|
|
|
|
xit,
|
|
|
|
} from 'angular2/test_lib';
|
2015-02-11 13:13:49 -05:00
|
|
|
|
2015-02-17 17:30:24 -05:00
|
|
|
import { isBlank, isPresent, BaseException, stringify, Date, DateWrapper } from 'angular2/src/facade/lang';
|
2015-02-11 13:13:49 -05:00
|
|
|
import { ListWrapper, List } from 'angular2/src/facade/collection';
|
|
|
|
import { PromiseWrapper, Promise } from 'angular2/src/facade/async';
|
|
|
|
|
|
|
|
import {
|
2015-04-29 16:01:18 -04:00
|
|
|
Sampler, WebDriverAdapter,
|
2015-02-11 13:13:49 -05:00
|
|
|
Validator, Metric, Reporter, Browser,
|
2015-02-17 17:30:24 -05:00
|
|
|
bind, Injector, Options, MeasureValues
|
2015-03-02 12:23:09 -05:00
|
|
|
} from 'benchpress/common';
|
2015-02-11 13:13:49 -05:00
|
|
|
|
|
|
|
export function main() {
|
|
|
|
var EMPTY_EXECUTE = () => {};
|
|
|
|
|
|
|
|
describe('sampler', () => {
|
|
|
|
var sampler;
|
|
|
|
|
|
|
|
function createSampler({
|
|
|
|
driver,
|
|
|
|
metric,
|
|
|
|
reporter,
|
|
|
|
validator,
|
|
|
|
prepare,
|
|
|
|
execute
|
|
|
|
} = {}) {
|
2015-02-17 17:30:24 -05:00
|
|
|
var time = 1000;
|
2015-02-11 13:13:49 -05:00
|
|
|
if (isBlank(metric)) {
|
|
|
|
metric = new MockMetric([]);
|
|
|
|
}
|
|
|
|
if (isBlank(reporter)) {
|
|
|
|
reporter = new MockReporter([]);
|
|
|
|
}
|
|
|
|
if (isBlank(driver)) {
|
|
|
|
driver = new MockDriverAdapter([]);
|
|
|
|
}
|
2015-03-06 20:34:27 -05:00
|
|
|
var bindings = [
|
|
|
|
Options.DEFAULT_BINDINGS,
|
|
|
|
Sampler.BINDINGS,
|
2015-02-11 13:13:49 -05:00
|
|
|
bind(Metric).toValue(metric),
|
|
|
|
bind(Reporter).toValue(reporter),
|
|
|
|
bind(WebDriverAdapter).toValue(driver),
|
|
|
|
bind(Options.EXECUTE).toValue(execute),
|
2015-02-17 17:30:24 -05:00
|
|
|
bind(Validator).toValue(validator),
|
2015-03-06 20:34:27 -05:00
|
|
|
bind(Options.NOW).toValue( () => DateWrapper.fromMillis(time++) )
|
|
|
|
];
|
2015-02-11 13:13:49 -05:00
|
|
|
if (isPresent(prepare)) {
|
|
|
|
ListWrapper.push(bindings, bind(Options.PREPARE).toValue(prepare));
|
|
|
|
}
|
|
|
|
|
2015-04-10 20:05:31 -04:00
|
|
|
sampler = Injector.resolveAndCreate(bindings).get(Sampler);
|
2015-02-11 13:13:49 -05:00
|
|
|
}
|
|
|
|
|
2015-03-13 06:10:11 -04:00
|
|
|
it('should call the prepare and execute callbacks using WebDriverAdapter.waitFor', inject([AsyncTestCompleter], (async) => {
|
2015-02-11 13:13:49 -05:00
|
|
|
var log = [];
|
|
|
|
var count = 0;
|
|
|
|
var driver = new MockDriverAdapter([], (callback) => {
|
|
|
|
var result = callback();
|
|
|
|
ListWrapper.push(log, result);
|
|
|
|
return PromiseWrapper.resolve(result);
|
|
|
|
});
|
|
|
|
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]);
|
2015-03-13 06:10:11 -04:00
|
|
|
async.done();
|
2015-02-11 13:13:49 -05:00
|
|
|
});
|
|
|
|
|
2015-03-13 06:10:11 -04:00
|
|
|
}));
|
2015-02-11 13:13:49 -05:00
|
|
|
|
2015-04-29 16:01:18 -04:00
|
|
|
it('should call prepare, beginMeasure, execute, endMeasure for every iteration', inject([AsyncTestCompleter], (async) => {
|
2015-02-11 13:13:49 -05:00
|
|
|
var workCount = 0;
|
|
|
|
var log = [];
|
|
|
|
createSampler({
|
|
|
|
metric: createCountingMetric(log),
|
|
|
|
validator: createCountingValidator(2),
|
|
|
|
prepare: () => {
|
|
|
|
ListWrapper.push(log, `p${workCount++}`);
|
|
|
|
},
|
|
|
|
execute: () => {
|
|
|
|
ListWrapper.push(log, `w${workCount++}`);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
sampler.sample().then( (_) => {
|
|
|
|
expect(log).toEqual([
|
|
|
|
'p0',
|
|
|
|
['beginMeasure'],
|
|
|
|
'w1',
|
|
|
|
['endMeasure', false, {'script': 0}],
|
|
|
|
'p2',
|
|
|
|
['beginMeasure'],
|
|
|
|
'w3',
|
|
|
|
['endMeasure', false, {'script': 1}],
|
|
|
|
]);
|
2015-03-13 06:10:11 -04:00
|
|
|
async.done();
|
2015-02-11 13:13:49 -05:00
|
|
|
});
|
2015-03-13 06:10:11 -04:00
|
|
|
}));
|
2015-02-11 13:13:49 -05:00
|
|
|
|
2015-04-29 16:01:18 -04:00
|
|
|
it('should call execute, endMeasure for every iteration if there is no prepare callback', inject([AsyncTestCompleter], (async) => {
|
2015-02-11 13:13:49 -05:00
|
|
|
var log = [];
|
|
|
|
var workCount = 0;
|
|
|
|
createSampler({
|
|
|
|
metric: createCountingMetric(log),
|
|
|
|
validator: createCountingValidator(2),
|
|
|
|
execute: () => {
|
|
|
|
ListWrapper.push(log, `w${workCount++}`);
|
|
|
|
},
|
|
|
|
prepare: null
|
|
|
|
});
|
|
|
|
sampler.sample().then( (_) => {
|
|
|
|
expect(log).toEqual([
|
|
|
|
['beginMeasure'],
|
|
|
|
'w0',
|
|
|
|
['endMeasure', true, {'script': 0}],
|
|
|
|
'w1',
|
|
|
|
['endMeasure', true, {'script': 1}],
|
|
|
|
]);
|
2015-03-13 06:10:11 -04:00
|
|
|
async.done();
|
2015-02-11 13:13:49 -05:00
|
|
|
});
|
2015-03-13 06:10:11 -04:00
|
|
|
}));
|
2015-02-11 13:13:49 -05:00
|
|
|
|
2015-03-13 06:10:11 -04:00
|
|
|
it('should only collect metrics for execute and ignore metrics from prepare', inject([AsyncTestCompleter], (async) => {
|
2015-02-11 13:13:49 -05:00
|
|
|
var scriptTime = 0;
|
|
|
|
var iterationCount = 1;
|
|
|
|
createSampler({
|
|
|
|
validator: createCountingValidator(2),
|
|
|
|
metric: new MockMetric([], () => {
|
|
|
|
var result = PromiseWrapper.resolve({'script': scriptTime});
|
|
|
|
scriptTime = 0;
|
|
|
|
return result;
|
|
|
|
}),
|
|
|
|
prepare: () => {
|
|
|
|
scriptTime = 1 * iterationCount;
|
|
|
|
},
|
|
|
|
execute: () => {
|
|
|
|
scriptTime = 10 * iterationCount;
|
|
|
|
iterationCount++;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
sampler.sample().then( (state) => {
|
|
|
|
expect(state.completeSample.length).toBe(2);
|
2015-02-17 17:30:24 -05:00
|
|
|
expect(state.completeSample[0]).toEqual(mv(0, 1000, {'script': 10}));
|
|
|
|
expect(state.completeSample[1]).toEqual(mv(1, 1001, {'script': 20}));
|
2015-03-13 06:10:11 -04:00
|
|
|
async.done();
|
2015-02-11 13:13:49 -05:00
|
|
|
});
|
2015-03-13 06:10:11 -04:00
|
|
|
}));
|
2015-02-11 13:13:49 -05:00
|
|
|
|
2015-03-13 06:10:11 -04:00
|
|
|
it('should call the validator for every execution and store the valid sample', inject([AsyncTestCompleter], (async) => {
|
2015-02-11 13:13:49 -05:00
|
|
|
var log = [];
|
|
|
|
var validSample = [{}];
|
|
|
|
|
|
|
|
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(
|
2015-02-17 17:30:24 -05:00
|
|
|
['validate', [mv(0, 1000, {'script': 0})], null]
|
2015-02-11 13:13:49 -05:00
|
|
|
);
|
|
|
|
expect(log[1]).toEqual(
|
2015-02-17 17:30:24 -05:00
|
|
|
['validate', [mv(0, 1000, {'script': 0}), mv(1, 1001, {'script': 1})], validSample]
|
2015-02-11 13:13:49 -05:00
|
|
|
);
|
|
|
|
|
2015-03-13 06:10:11 -04:00
|
|
|
async.done();
|
2015-02-11 13:13:49 -05:00
|
|
|
});
|
2015-03-13 06:10:11 -04:00
|
|
|
}));
|
2015-02-11 13:13:49 -05:00
|
|
|
|
2015-03-13 06:10:11 -04:00
|
|
|
it('should report the metric values', inject([AsyncTestCompleter], (async) => {
|
2015-02-11 13:13:49 -05:00
|
|
|
var log = [];
|
|
|
|
var validSample = [{}];
|
|
|
|
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(
|
2015-02-17 17:30:24 -05:00
|
|
|
['reportMeasureValues', mv(0, 1000, {'script': 0})]
|
2015-02-11 13:13:49 -05:00
|
|
|
);
|
|
|
|
expect(log[1]).toEqual(
|
2015-02-17 17:30:24 -05:00
|
|
|
['reportMeasureValues', mv(1, 1001, {'script': 1})]
|
2015-02-11 13:13:49 -05:00
|
|
|
);
|
|
|
|
expect(log[2]).toEqual(
|
2015-02-17 17:30:24 -05:00
|
|
|
['reportSample', [mv(0, 1000, {'script': 0}), mv(1, 1001, {'script': 1})], validSample]
|
2015-02-11 13:13:49 -05:00
|
|
|
);
|
|
|
|
|
2015-03-13 06:10:11 -04:00
|
|
|
async.done();
|
2015-02-11 13:13:49 -05:00
|
|
|
});
|
2015-03-13 06:10:11 -04:00
|
|
|
}));
|
2015-02-11 13:13:49 -05:00
|
|
|
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-02-17 17:30:24 -05:00
|
|
|
function mv(runIndex, time, values) {
|
|
|
|
return new MeasureValues(runIndex, DateWrapper.fromMillis(time), values);
|
|
|
|
}
|
|
|
|
|
2015-02-11 13:13:49 -05:00
|
|
|
function createCountingValidator(count, validSample = null, log = null) {
|
|
|
|
return new MockValidator(log, (completeSample) => {
|
|
|
|
count--;
|
|
|
|
if (count === 0) {
|
|
|
|
return isPresent(validSample) ? validSample : completeSample;
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function createCountingMetric(log = null) {
|
|
|
|
var scriptTime = 0;
|
|
|
|
return new MockMetric(log, () => {
|
|
|
|
return { 'script': scriptTime++ };
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
class MockDriverAdapter extends WebDriverAdapter {
|
|
|
|
_log:List;
|
|
|
|
_waitFor:Function;
|
|
|
|
constructor(log = null, waitFor = null) {
|
|
|
|
super();
|
|
|
|
if (isBlank(log)) {
|
|
|
|
log = [];
|
|
|
|
}
|
|
|
|
this._log = log;
|
|
|
|
this._waitFor = waitFor;
|
|
|
|
}
|
|
|
|
waitFor(callback:Function):Promise {
|
|
|
|
if (isPresent(this._waitFor)) {
|
|
|
|
return this._waitFor(callback);
|
|
|
|
} else {
|
|
|
|
return PromiseWrapper.resolve(callback());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class MockValidator extends Validator {
|
|
|
|
_validate:Function;
|
|
|
|
_log:List;
|
|
|
|
constructor(log = null, validate = null) {
|
|
|
|
super();
|
|
|
|
this._validate = validate;
|
|
|
|
if (isBlank(log)) {
|
|
|
|
log = [];
|
|
|
|
}
|
|
|
|
this._log = log;
|
|
|
|
}
|
2015-02-17 17:30:24 -05:00
|
|
|
validate(completeSample:List<MeasureValues>):List<MeasureValues> {
|
2015-02-11 13:13:49 -05:00
|
|
|
var stableSample = isPresent(this._validate) ? this._validate(completeSample) : completeSample;
|
|
|
|
ListWrapper.push(this._log, ['validate', completeSample, stableSample]);
|
|
|
|
return stableSample;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class MockMetric extends Metric {
|
|
|
|
_endMeasure:Function;
|
|
|
|
_log:List;
|
|
|
|
constructor(log = null, endMeasure = null) {
|
|
|
|
super();
|
|
|
|
this._endMeasure = endMeasure;
|
|
|
|
if (isBlank(log)) {
|
|
|
|
log = [];
|
|
|
|
}
|
|
|
|
this._log = log;
|
|
|
|
}
|
|
|
|
beginMeasure() {
|
|
|
|
ListWrapper.push(this._log, ['beginMeasure']);
|
|
|
|
return PromiseWrapper.resolve(null);
|
|
|
|
}
|
|
|
|
endMeasure(restart) {
|
|
|
|
var measureValues = isPresent(this._endMeasure) ? this._endMeasure() : {};
|
|
|
|
ListWrapper.push(this._log, ['endMeasure', restart, measureValues]);
|
|
|
|
return PromiseWrapper.resolve(measureValues);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class MockReporter extends Reporter {
|
|
|
|
_log:List;
|
|
|
|
constructor(log = null) {
|
|
|
|
super();
|
|
|
|
if (isBlank(log)) {
|
|
|
|
log = [];
|
|
|
|
}
|
|
|
|
this._log = log;
|
|
|
|
}
|
2015-02-17 17:30:24 -05:00
|
|
|
reportMeasureValues(values):Promise {
|
|
|
|
ListWrapper.push(this._log, ['reportMeasureValues', values]);
|
2015-02-11 13:13:49 -05:00
|
|
|
return PromiseWrapper.resolve(null);
|
|
|
|
}
|
|
|
|
reportSample(completeSample, validSample):Promise {
|
|
|
|
ListWrapper.push(this._log, ['reportSample', completeSample, validSample]);
|
|
|
|
return PromiseWrapper.resolve(null);
|
|
|
|
}
|
2015-03-13 06:10:11 -04:00
|
|
|
}
|