OLD | NEW |
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 library protoc.benchmark; | 5 library protoc.benchmark; |
6 | 6 |
7 import 'generated/benchmark.pb.dart' as pb; | 7 import 'generated/benchmark.pb.dart' as pb; |
8 | 8 |
9 typedef Benchmark CreateBenchmarkFunc(pb.Request request); | 9 typedef Benchmark CreateBenchmarkFunc(pb.Request request); |
10 | 10 |
11 // Describes how to construct a benchmark. | 11 // Describes how to construct a benchmark. |
12 class BenchmarkType { | 12 class BenchmarkType { |
13 final pb.BenchmarkID id; | 13 final pb.BenchmarkID id; |
14 final CreateBenchmarkFunc create; | 14 final CreateBenchmarkFunc create; |
15 const BenchmarkType(this.id, this.create); | 15 const BenchmarkType(this.id, this.create); |
16 } | 16 } |
17 | 17 |
18 /// A benchmark that also reports the counts for various operations. | 18 /// A benchmark that also reports the counts for various operations. |
19 /// (A modification of BenchmarkBase from the benchmark_harness library.) | 19 /// (A modification of BenchmarkBase from the benchmark_harness library.) |
20 abstract class Benchmark { | 20 abstract class Benchmark { |
21 static const int _DEFAULT_REPS = 10; | 21 static const int _DEFAULT_REPS = 10; |
22 | 22 |
23 final pb.BenchmarkID id; | 23 final pb.BenchmarkID id; |
24 Benchmark(this.id); | 24 Benchmark(this.id); |
25 | 25 |
26 String get summary => id.name; | 26 String get summary => id.name; |
27 | 27 |
28 pb.Request makeRequest( | 28 pb.Request makeRequest( |
29 [Duration duration = const Duration(seconds: 1), int samples = 3]) => | 29 [Duration duration = const Duration(milliseconds: 50), |
| 30 int samples = 20]) => |
30 new pb.Request() | 31 new pb.Request() |
31 ..id = id | 32 ..id = id |
32 ..params = makeParams() | 33 ..params = makeParams() |
33 ..duration = duration.inMilliseconds | 34 ..duration = duration.inMilliseconds |
34 ..samples = samples; | 35 ..samples = samples; |
35 | 36 |
36 pb.Params makeParams(); | 37 pb.Params makeParams(); |
37 | 38 |
38 /// Runs a benchmark for the requested number of times. | 39 /// Runs a benchmark for the requested number of times. |
39 /// | 40 /// |
40 /// The length of each iterator is the number of samples | 41 /// The length of each iterator is the number of samples |
41 /// requested. If you create more than one iterator, each | 42 /// requested. If you create more than one iterator, each |
42 /// iterator runs benchmarks independently and will return | 43 /// iterator runs benchmarks independently and will return |
43 /// different samples. | 44 /// different samples. |
44 Iterable<pb.Sample> measure(pb.Request r) sync* { | 45 Iterable<pb.Sample> measure(pb.Request r, int samples) sync* { |
45 if (r.id != id) { | 46 if (r.id != id) { |
46 throw new ArgumentError("invalid benchmark id: ${r.id}"); | 47 throw new ArgumentError("invalid benchmark id: ${r.id}"); |
47 } | 48 } |
48 if (r.params != makeParams()) { | 49 if (r.params != makeParams()) { |
49 throw new ArgumentError("parameters don't match: ${r.params}"); | 50 throw new ArgumentError("parameters don't match: ${r.params}"); |
50 } | 51 } |
51 | 52 |
52 int sampleMillis = r.duration; | 53 int sampleMillis = r.duration; |
53 int samples = r.samples; | |
54 setup(); | 54 setup(); |
55 | 55 |
56 for (int i = 0; i < samples; i++) { | 56 for (int i = 0; i < samples; i++) { |
57 yield _measureOnce(sampleMillis); | 57 yield _measureOnce(sampleMillis); |
58 } | 58 } |
59 teardown(); | 59 teardown(); |
60 } | 60 } |
61 | 61 |
62 pb.Sample _measureOnce(int sampleMillis) { | 62 pb.Sample _measureOnce(int sampleMillis) { |
63 // Warmup for at least 100ms. Discard result. | 63 // Warmup for at least 100ms. Discard result. |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
115 while (elapsed < minimumMicros) { | 115 while (elapsed < minimumMicros) { |
116 reps += runner(); | 116 reps += runner(); |
117 elapsed = watch.elapsedMicroseconds; | 117 elapsed = watch.elapsedMicroseconds; |
118 } | 118 } |
119 return new pb.Sample() | 119 return new pb.Sample() |
120 ..duration = elapsed | 120 ..duration = elapsed |
121 ..loopCount = reps | 121 ..loopCount = reps |
122 ..counts = new pb.Counts(); | 122 ..counts = new pb.Counts(); |
123 } | 123 } |
124 } | 124 } |
OLD | NEW |