@danielkalen/simplybind
Version:
Magically simple, framework-less one-way/two-way data binding for frontend/backend in ~5kb.
213 lines (211 loc) • 9.01 kB
JavaScript
// Generated by CoffeeScript 1.10.0
Promise.config({
'longStackTraces': false
});
(function($) {
var TestSuite, markup;
markup = {
list: "<div class='testSuite-list'></div>",
gap: "<div class='testSuite-list-gap'></div>",
item: "<div class='testSuite-list-item {{nonSharedTest}}'> <div class='testSuite-list-item-innerwrap'> <div class='testSuite-list-item-title'>{{title}}</div> <div class='testSuite-list-item-subtitle'>{{subtitle}}</div> <div class='testSuite-list-item-button __execute'> <div class='testSuite-list-item-button-text'>Run Benchmark</div> </div> <div class='testSuite-list-item-results __results'> <div class='testSuite-list-item-results-item'> <div class='testSuite-list-item-results-item-label'>Time:</div> <div class='testSuite-list-item-results-item-value __results-time'></div> </div> <div class='testSuite-list-item-results-item'> <div class='testSuite-list-item-results-item-label'>Result:</div> <div class='testSuite-list-item-results-item-value __results-result'></div> </div> <div class='testSuite-list-item-results-item'> <div class='testSuite-list-item-results-item-label'>Average:</div> <div class='testSuite-list-item-results-item-value __results-avg'></div> </div> </div> <div class='testSuite-list-item-setup __setup'></div> </div> </div> "
};
window.timeUnits = {
'millisecond': 1,
'second': 1000,
'minute': 60000,
'hour': 3600000,
'day': 24 * 3600000,
'week': 7 * 24 * 3600000,
'month': 28 * 24 * 3600000,
'year': 364 * 24 * 3600000
};
TestSuite = function(arg) {
var itemMarkup, ref, ref1, ref2, title$;
this.title = arg.title, this.subtitle = arg.subtitle, this.measureMethod = (ref = arg.measureMethod) != null ? ref : 'sync', this.setupFn = arg.setupFn, this.testFn = arg.testFn, this.teardownFn = arg.teardownFn, this.timesToRun = (ref1 = arg.timesToRun) != null ? ref1 : 1, this.warmUps = (ref2 = arg.warmUps) != null ? ref2 : 1, this.manualTiming = arg.manualTiming, this.nonSharedTest = arg.nonSharedTest;
if (this.constructor !== TestSuite) {
return new TestSuite(arguments[0]);
}
this.runCount = 0;
this.els = {};
this.results = [];
this.testScope = {};
if (this.teardownFn == null) {
this.teardownFn = (function(_this) {
return function() {
return _this.els.setup.empty();
};
})(this);
}
this.testFn = this.testFn.bind(this.testScope) || (function() {}).bind(this.testScope);
title$ = $('.testSuite_heading-title');
this.libraryVersion = title$.children('span')[0].textContent;
this.libraryName = title$[0].textContent.replace(this.libraryVersion, '').replace(/\s+$/, '');
itemMarkup = markup.item.replace('{{title}}', this.title || '').replace('{{subtitle}}', this.subtitle || '').replace('{{nonSharedTest}}', this.nonSharedTest ? 'nonSharedTest' : '');
this.els.container = $(document.body).children('.testSuite');
if (this.els.container.children().length) {
this.els.list = this.els.container.children();
} else {
this.els.list = $(markup.list).appendTo(this.els.container);
setTimeout((function(_this) {
return function() {
return _this.els.list.append(markup.gap).append(markup.gap);
};
})(this));
}
this.els.item = $(itemMarkup).appendTo(this.els.list).after(' ');
this.els.button = this.els.item.find('.__execute');
this.els.results = this.els.item.find('.__results');
this.els.resultsTime = this.els.results.find('.__results-time');
this.els.resultsResult = this.els.results.find('.__results-result');
this.els.resultsAvg = this.els.results.find('.__results-avg');
this.els.setup = this.els.item.find('.__setup');
this.els.button.on('click', (function(_this) {
return function() {
return _this.run();
};
})(this));
this.els.item.data('TestSuite', this);
return this;
};
TestSuite.prototype.setup = function() {
return new Promise((function(_this) {
return function(resolve) {
if (_this.setupFn) {
_this.setupFn.call(_this.testScope, _this.els.setup);
}
return resolve();
};
})(this));
};
TestSuite.prototype.teardown = function() {
if (this.setupFn) {
return this.teardownFn.call(this.testScope, this.els.setup);
}
};
TestSuite.prototype.storeResults = function(force) {
var postData;
if (location.hostname && (this.results.length >= 5 || force) && window.storeResults) {
postData = {
'library': this.libraryName,
'version': this.libraryVersion,
'testName': this.title,
'testDesc': this.subtitle,
'result': this.average,
'UA': navigator.userAgent,
'nonSharedTest': this.nonSharedTest || false
};
return $.post('/set', postData);
}
};
TestSuite.prototype.run = function() {
var beginTest, performIteration, warmUp;
if (!this.running) {
this.running = true;
this.runCount += 1;
performIteration = (function(_this) {
return function(timesToRun) {
return new Promise(function(resolve) {
var endTime, iteration, ref, results, results1, startTime, totalTime;
switch (_this.measureMethod) {
case 'sync':
totalTime = 0;
iteration = 0;
results = [];
while (iteration++ < timesToRun) {
if (_this.manualTiming) {
ref = _this.testFn(), startTime = ref.startTime, endTime = ref.endTime;
} else {
startTime = performance.now();
_this.testFn();
endTime = performance.now();
}
totalTime += endTime - startTime;
if (iteration === timesToRun) {
results.push(resolve(totalTime));
} else {
results.push(void 0);
}
}
return results;
break;
case 'async':
totalTime = 0;
iteration = 0;
results1 = [];
while (iteration++ < timesToRun) {
results1.push((function(iteration) {
return setTimeout(function() {
var ref1;
if (_this.manualTiming) {
ref1 = _this.testFn(), startTime = ref1.startTime, endTime = ref1.endTime;
} else {
startTime = performance.now();
_this.testFn();
endTime = performance.now();
}
totalTime += endTime - startTime;
if (iteration === timesToRun) {
return resolve(totalTime);
}
});
})(iteration));
}
return results1;
}
});
};
})(this);
warmUp = (function(_this) {
return function() {
return performIteration(_this.warmUps);
};
})(this);
beginTest = (function(_this) {
return function() {
return performIteration(_this.timesToRun);
};
})(this);
return this.setup().then(warmUp).then(beginTest).then((function(_this) {
return function(result) {
var average, formatTime;
formatTime = function(time, returnTime) {
var perSec, split;
if (returnTime) {
split = time.toString().split('.');
if (split.length > 1) {
return split[0] + "." + (split[1].slice(0, 3)) + " ms";
} else {
return time + " ms";
}
} else {
time = time / _this.timesToRun;
perSec = timeUnits.minute / time;
return humanize.numberFormat(perSec, 0) + ' op/s';
}
};
if (_this.runCount <= 2) {
average = '---------';
} else {
_this.results.push(result);
average = _this.results.reduce(function(a, b) {
return a + b;
}) / _this.results.length;
}
_this.els.resultsTime.html(formatTime(result, true));
_this.els.resultsResult.html(formatTime(result));
_this.els.resultsAvg.html(_this.average = _this.runCount <= 2 ? average : formatTime(average));
_this.els.results.addClass('hasResults');
_this.teardown();
_this.storeResults(result > 10000);
return _this.running = false;
};
})(this));
}
};
window.storeResults = true;
$('#storeResults').on('click', function() {
window.storeResults = !window.storeResults;
return $(this).toggleClass('enabled');
});
return window.TestSuite = TestSuite;
})(jQuery);