Files
sdk/pkg/dev_compiler/test/codegen/BenchmarkBase.dart
T
Jenny Messerly 1ef4399df0 Run dartfmt --fix for dart2 on pkg/dev_compiler
This uses optional new/const and `=` in named argument defaults.

All changes are automated, except for:

- utils/dartdevc/BUILD.gn: run DDC build scripts with --preview-dart-2
- pkg/dev_compiler/tool/patch_sdk.dart: add a TODO that Analyzer doesn't
  supporting implicit const in libraries.dart
- pkg/dev_compiler/tool/input_sdk/libraries.dart: was not formatted due
  to the aforementioned Analyzer bug
- tools/bots/test_matrix.json: run DDC sourcemap suite in Dart 2 mode
- pkg/pkg.status: skip pkg/dev_compiler if running in Dart 1 mode

Change-Id: I9b80ccba0c2cc7b66efc662a0b16562e3660aee3
Reviewed-on: https://dart-review.googlesource.com/60402
Commit-Queue: Jenny Messerly <jmesserly@google.com>
Reviewed-by: Bob Nystrom <rnystrom@google.com>
2018-06-15 00:28:13 +00:00

90 lines
2.1 KiB
Dart

// Copyright 2011 Google Inc. All Rights Reserved.
library BenchmarkBase;
class Expect {
static void equals(var expected, var actual) {
if (expected != actual) {
throw "Values not equal: $expected vs $actual";
}
}
static void listEquals(List expected, List actual) {
if (expected.length != actual.length) {
throw "Lists have different lengths: ${expected.length} vs ${actual.length}";
}
for (int i = 0; i < actual.length; i++) {
equals(expected[i], actual[i]);
}
}
fail(message) {
throw message;
}
}
class BenchmarkBase {
final String name;
// Empty constructor.
const BenchmarkBase(String name) : this.name = name;
// The benchmark code.
// This function is not used, if both [warmup] and [exercise] are overwritten.
void run() {}
// Runs a short version of the benchmark. By default invokes [run] once.
void warmup() {
run();
}
// Exercises the benchmark. By default invokes [run] 10 times.
void exercise() {
for (int i = 0; i < 10; i++) {
run();
}
}
// Not measured setup code executed prior to the benchmark runs.
void setup() {}
// Not measures teardown code executed after the benchmark runs.
void teardown() {}
// Measures the score for this benchmark by executing it repeately until
// time minimum has been reached.
static double measureFor(Function f, int timeMinimum) {
int time = 0;
int iter = 0;
Stopwatch watch = Stopwatch();
watch.start();
int elapsed = 0;
while (elapsed < timeMinimum) {
f();
elapsed = watch.elapsedMilliseconds;
iter++;
}
return 1000.0 * elapsed / iter;
}
// Measures the score for the benchmark and returns it.
double measure() {
setup();
// Warmup for at least 100ms. Discard result.
measureFor(() {
this.warmup();
}, 100);
// Run the benchmark for at least 2000ms.
double result = measureFor(() {
this.exercise();
}, 2000);
teardown();
return result;
}
void report() {
double score = measure();
print("$name(RunTime): $score us.");
}
}