Skip to content

Instantly share code, notes, and snippets.

@back2dos
Last active December 24, 2015 21:19
Show Gist options
  • Select an option

  • Save back2dos/6865102 to your computer and use it in GitHub Desktop.

Select an option

Save back2dos/6865102 to your computer and use it in GitHub Desktop.
Promising benchmarks. Surprising results.
package ;
import tink.core.Future;
using tink.core.Outcome;
import haxe.Timer.*;
class Main {
macro static function benchmark(name:String, makeTrigger, getFuture, watch, trigger)
return macro {
name: $v{name},
run: function (cb) {
var left = count * hcount;
var start = stamp();
if (hcount == 0)
left = 1;
function handle(x)
if (--left == 0) cb(stamp() - start);
for (i in 0...count) {
$makeTrigger;
$getFuture;
for (i in 0...hcount)
$watch;
$trigger;
}
if (hcount == 0)
handle(null);
}
};
#if !macro
static var count = 500;
static var hcount = 3;
static var mincount = 0;
static var Bluebird = untyped require('bluebird');
static var benchmarks = [
benchmark('tink sync', null, var f = Future.sync(Success(1)), f.handle(handle), null),
benchmark('tink plain', var t = Future.trigger(), var f = t.asFuture(), f.handle(handle), t.trigger(i)),
benchmark('tink', var t = Future.trigger(), var f = t.asFuture(), f.handle(handle), t.trigger(Success(i))),
benchmark('promhx', var p = new promhx.Promise(), null, p.then(handle), p.resolve(i)),
benchmark('bluebird', var b = Bluebird.pending(), var p = b.promise, p.then(handle), b.fulfill(i)),
];
static function main()
run();
static var protocol:Array<Map<String, Float>> = [];
static function msecs(op:String, f:Float)
trace(op +' took '+ Std.int(f * 1000)+' msecs');
static function run() {
if (hcount < mincount) {
trace('--- OVERALL ---');
for (b in benchmarks) {
var time = .0;
for (p in protocol)
time += p[b.name];
msecs(b.name, time);
}
return;
}
trace('--- $hcount handlers ---');
var times = [for (b in benchmarks) b.name => .0];
var backAndForth = benchmarks.copy();
backAndForth.reverse();
backAndForth = backAndForth.concat(benchmarks);
var queue = [for (i in 0...if (hcount == 0) 100 else Std.int(100 / hcount)) for (b in backAndForth) b];
function next()
if (queue.length == 0) {
protocol.push(times);
for (b in benchmarks)
msecs(b.name, times.get(b.name));
hcount--;
delay(run, 1000);
}
else {
var b = queue.shift();
b.run(function (time) {
times[b.name] += time;
next();
});
}
next();
}
#end
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment