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