on 2297bbf help sign in
elk/bench/src/Native/Benchmark.js
102 lines 4.0 kB history raw
1Elm.Native.Benchmark = {};
2Elm.Native.Benchmark.make = function(localRuntime) {
4 localRuntime.Native = localRuntime.Native || {};
5 localRuntime.Native.Benchmark = localRuntime.Native.Benchmark || {};
6 if (localRuntime.Native.Benchmark.values)
7 {
8 return localRuntime.Native.Benchmark.values;
9 }
11 var List = Elm.Native.List.make(localRuntime);
12 var Task = Elm.Native.Task.make(localRuntime);
13 var Signal = Elm.Signal.make(localRuntime);
14 var Utils = Elm.Native.Utils.make(localRuntime);
16 //Nothing special happens here, but we need to use it as a Native function
17 //So that we can accept thunks of any type
18 function makeBenchmark(name, thunk)
19 {
20 return {name : name, thunk : thunk};
21 }
23 //Generate the task for running a benchmark suite
24 //Possibly updating a given mailbox with a string describing
25 //Our progress of running the benchmarks so far
26 function runWithProgress(maybeTaskFn, inSuite)
27 {
29 return Task.asyncFunction(function(callback) {
30 var bjsSuite = new Benchmark.Suite;
31 var benchArray;
32 var retData = [];
33 var finalString = "";
34 var numCompleted = 0;
35 var numToRun;
37 switch (inSuite.ctor)
38 {
39 case "Suite":
40 benchArray = List.toArray(inSuite._1);
41 break;
42 case "SingleBenchmark":
43 benchArray = [inSuite._0 ];
44 break;
45 }
47 numToRun = benchArray.length;
48 Task.perform(maybeTaskFn("Running benchmark 1 of " + numToRun));
50 //Initialize each benchmark in the suite
51 for (i = 0; i < benchArray.length; i++)
52 {
53 var ourThunk = function (){
54 //Run the thing we're timing, then mark the asynch benchmark as done
55 benchArray[i].thunk();
56 deferred.resolve();
57 }
58 bjsSuite.add(benchArray[i].name, benchArray[i].thunk );
59 }
61 //Every time a benchmark finishes, we store its results
62 //and update our progress string
63 bjsSuite.on('cycle', function(event) {
64 numCompleted += 1;
65 retData.push(
66 { name : event.target.options.name
67 , hz : event.target.hz
68 , marginOfError : event.target.stats.moe
69 , moePercent : event.target.stats.rme
70 }
71 );
72 finalString += String(event.target) + "\n";
73 var intermedString =
74 "Running benchmark "
75 + (numCompleted + 1)
76 + " of " + numToRun
77 + "\nLast result: " + String(event.target);
78 Task.perform(maybeTaskFn(intermedString));
79 //retString += String(event.target) + "\n";
80 });
82 //When the last benchmark finishes, we show all results collected
83 bjsSuite.on('complete', function(event) {
84 Task.perform(maybeTaskFn("Final results:\n\n" + finalString) );
85 return callback(Task.succeed(Utils.Tuple2(finalString, List.fromArray(retData))));
86 });
88 //Finally: actually run the suite
89 Task.perform(
90 Task.asyncFunction(function(otherCallback){
91 bjsSuite.run({ 'async': true });
92 }));
93 });
94 }
96 return localRuntime.Native.Benchmark.values = {
97 makeBenchmark: F2(makeBenchmark),
98 runWithProgress: F2(runWithProgress)
99 };
102};