Files
sdk/pkg/dev_compiler/test/codegen/expect/BenchmarkBase.js
T
Leaf Petersen 8040611a79 This CL implements tagging of functions and methods with function types.
For every class, we now generate a setSignature call which attaches properties to the constructor recording the method signatures, the static function signatures, and the names of all of the static methods. This call also attaches a getter to every static method which returns the type of the function.  Methods are only decorated with runtime types when torn off.  At a tear-off, the type is looked up in the constructor, and then attached to the bound function.

Top level functions and statement level functions get annotated with their type immediately after their declaration.  We could consider moving all of the top level function annotations to the end of the file, but for now I've left it inline.

Closures (function expressions) get wrapped in calls to a dart.fn helper, with type information attached in one of various forms.  This is currently the least attractive part of this CL.  We may want to iterate on the syntax for this.

I've added some support for NSM checking to the dsend/dcall case as well.

We may wish to iterate on the syntax, and on the runtime representation of types, but this should move us forward from a functionality standpoint.

BUG=
R=jmesserly@google.com, vsm@google.com

Review URL: https://codereview.chromium.org/1138793002
2015-05-19 16:24:35 -07:00

92 lines
2.8 KiB
JavaScript

var BenchmarkBase = dart.defineLibrary(BenchmarkBase, {});
var core = dart.import(core);
(function(exports, core) {
'use strict';
class Expect extends core.Object {
static equals(expected, actual) {
if (!dart.equals(expected, actual)) {
throw `Values not equal: ${expected} vs ${actual}`;
}
}
static listEquals(expected, actual) {
if (expected[core.$length] != actual[core.$length]) {
throw `Lists have different lengths: ${expected[core.$length]} vs ${actual[core.$length]}`;
}
for (let i = 0; dart.notNull(i) < dart.notNull(actual[core.$length]); i = dart.notNull(i) + 1) {
Expect.equals(expected[core.$get](i), actual[core.$get](i));
}
}
fail(message) {
throw message;
}
}
dart.setSignature(Expect, {
methods: () => ({fail: dart.functionType(dart.dynamic, [dart.dynamic])}),
statics: () => ({
equals: dart.functionType(dart.void, [dart.dynamic, dart.dynamic]),
listEquals: dart.functionType(dart.void, [core.List, core.List])
}),
names: ['equals', 'listEquals']
});
class BenchmarkBase extends core.Object {
BenchmarkBase(name) {
this.name = name;
}
run() {}
warmup() {
this.run();
}
exercise() {
for (let i = 0; dart.notNull(i) < 10; i = dart.notNull(i) + 1) {
this.run();
}
}
setup() {}
teardown() {}
static measureFor(f, timeMinimum) {
let time = 0;
let iter = 0;
let watch = new core.Stopwatch();
watch.start();
let elapsed = 0;
while (dart.notNull(elapsed) < dart.notNull(timeMinimum)) {
dart.dcall(f);
elapsed = watch.elapsedMilliseconds;
iter = dart.notNull(iter) + 1;
}
return 1000.0 * dart.notNull(elapsed) / dart.notNull(iter);
}
measure() {
this.setup();
BenchmarkBase.measureFor(dart.fn((() => {
this.warmup();
}).bind(this)), 100);
let result = BenchmarkBase.measureFor(dart.fn((() => {
this.exercise();
}).bind(this)), 2000);
this.teardown();
return result;
}
report() {
let score = this.measure();
core.print(`${this.name}(RunTime): ${score} us.`);
}
}
dart.setSignature(BenchmarkBase, {
methods: () => ({
run: dart.functionType(dart.void, []),
warmup: dart.functionType(dart.void, []),
exercise: dart.functionType(dart.void, []),
setup: dart.functionType(dart.void, []),
teardown: dart.functionType(dart.void, []),
measure: dart.functionType(core.double, []),
report: dart.functionType(dart.void, [])
}),
statics: () => ({measureFor: dart.functionType(core.double, [core.Function, core.int])}),
names: ['measureFor']
});
// Exports:
exports.Expect = Expect;
exports.BenchmarkBase = BenchmarkBase;
})(BenchmarkBase, core);