OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 library yaml.benchmark.benchmark; | |
6 | |
7 import 'dart:convert'; | |
8 import 'dart:io'; | |
9 | |
10 import 'package:path/path.dart' as p; | |
11 | |
12 import 'package:yaml/yaml.dart'; | |
13 | |
14 const numTrials = 10000000; | |
15 const runsPerTrial = 1000; | |
16 | |
17 final source = loadFile("input.yaml"); | |
18 final expected = loadFile("output.json"); | |
19 | |
20 void main(List<String> args) { | |
21 var best = 99999999.0; | |
nweiz
2015/09/02 00:00:35
double.INFINITY ;)
Bob Nystrom
2015/09/02 00:08:31
Done.
| |
22 | |
23 // Run the benchmark several times. This ensures the VM is warmed up and lets | |
24 // us see how much variance there is. | |
25 for (var i = 0; i <= numTrials; i++) { | |
26 var start = new DateTime.now(); | |
27 | |
28 // For a single benchmark, convert the source multiple times. | |
29 var result; | |
30 for (var j = 0; j < runsPerTrial; j++) { | |
31 result = loadYaml(source); | |
32 } | |
33 | |
34 var elapsed = | |
35 new DateTime.now().difference(start).inMilliseconds / runsPerTrial; | |
36 | |
37 // Keep track of the best run so far. | |
38 if (elapsed >= best) continue; | |
39 best = elapsed; | |
40 | |
41 // Sanity check to make sure the output is what we expect and to make sure | |
42 // the VM doesn't optimize "dead" code away. | |
43 if (JSON.encode(result) != expected) { | |
44 print("Incorrect output:\n${JSON.encode(result)}"); | |
45 exit(1); | |
46 } | |
47 | |
48 // Don't print the first run. It's always terrible since the VM hasn't | |
49 // warmed up yet. | |
50 if (i == 0) continue; | |
51 printResult("Run ${padLeft('#$i', 3)}", elapsed); | |
52 } | |
53 | |
54 printResult("Best ", best); | |
55 } | |
56 | |
57 String loadFile(String name) { | |
58 var path = p.join(p.dirname(p.fromUri(Platform.script)), name); | |
59 return new File(path).readAsStringSync(); | |
60 } | |
61 | |
62 void printResult(String label, double time) { | |
63 print("$label: ${padLeft(time.toStringAsFixed(3), 4)}ms " | |
64 "${'=' * ((time * 100).toInt())}"); | |
65 } | |
66 | |
67 String padLeft(input, int length) { | |
68 var result = input.toString(); | |
69 if (result.length < length) { | |
70 result = " " * (length - result.length) + result; | |
71 } | |
72 | |
73 return result; | |
74 } | |
OLD | NEW |