// JSLitmus.js // // Copyright (c) 2010, Robert Kieffer, http://broofa.com // Available under MIT license (http://en.wikipedia.org/wiki/MIT_License) (function() { // Private methods and state // Get platform info but don't go crazy trying to recognize everything // that's out there. This is just for the major platforms and OSes. var platform = 'unknown platform', ua = navigator.userAgent; // Detect OS var oses = ['Windows','iPhone OS','(Intel |PPC )?Mac OS X','Linux'].join('|'); var pOS = new RegExp('((' + oses + ') [^ \);]*)').test(ua) ? RegExp.$1 : null; if (!pOS) pOS = new RegExp('((' + oses + ')[^ \);]*)').test(ua) ? RegExp.$1 : null; // Detect browser var pName = /(Chrome|MSIE|Safari|Opera|Firefox)/.test(ua) ? RegExp.$1 : null; // Detect version var vre = new RegExp('(Version|' + pName + ')[ \/]([^ ;]*)'); var pVersion = (pName && vre.test(ua)) ? RegExp.$2 : null; var platform = (pOS && pName && pVersion) ? pName + ' ' + pVersion + ' on ' + pOS : 'unknown platform'; /** * A smattering of methods that are needed to implement the JSLitmus testbed. */ var jsl = { /** * Enhanced version of escape() */ escape: function(s) { s = s.replace(/,/g, '\\,'); s = escape(s); s = s.replace(/\+/g, '%2b'); s = s.replace(/ /g, '+'); return s; }, /** * Get an element by ID. */ $: function(id) { return document.getElementById(id); }, /** * Null function */ F: function() {}, /** * Set the status shown in the UI */ status: function(msg) { var el = jsl.$('jsl_status'); if (el) el.innerHTML = msg || ''; }, /** * Convert a number to an abbreviated string like, "15K" or "10M" */ toLabel: function(n) { if (n == Infinity) { return 'Infinity'; } else if (n > 1e9) { n = Math.round(n/1e8); return n/10 + 'B'; } else if (n > 1e6) { n = Math.round(n/1e5); return n/10 + 'M'; } else if (n > 1e3) { n = Math.round(n/1e2); return n/10 + 'K'; } return n; }, /** * Copy properties from src to dst */ extend: function(dst, src) { for (var k in src) dst[k] = src[k]; return dst; }, /** * Like Array.join(), but for the key-value pairs in an object */ join: function(o, delimit1, delimit2) { if (o.join) return o.join(delimit1); // If it's an array var pairs = []; for (var k in o) pairs.push(k + delimit1 + o[k]); return pairs.join(delimit2); }, /** * Array#indexOf isn't supported in IE, so we use this as a cross-browser solution */ indexOf: function(arr, o) { if (arr.indexOf) return arr.indexOf(o); for (var i = 0; i < this.length; i++) if (arr[i] === o) return i; return -1; } }; /** * Test manages a single test (created with * JSLitmus.test()) * * @private */ var Test = function (name, f) { if (!f) throw new Error('Undefined test function'); if (!/function[^\(]*\(([^,\)]*)/.test(f.toString())) { throw new Error('"' + name + '" test: Test is not a valid Function object'); } this.loopArg = RegExp.$1; this.name = name; this.f = f; }; jsl.extend(Test, /** @lends Test */ { /** Calibration tests for establishing iteration loop overhead */ CALIBRATIONS: [ new Test('calibrating loop', function(count) {while (count--);}), new Test('calibrating function', jsl.F) ], /** * Run calibration tests. Returns true if calibrations are not yet * complete (in which case calling code should run the tests yet again). * onCalibrated - Callback to invoke when calibrations have finished */ calibrate: function(onCalibrated) { for (var i = 0; i < Test.CALIBRATIONS.length; i++) { var cal = Test.CALIBRATIONS[i]; if (cal.running) return true; if (!cal.count) { cal.isCalibration = true; cal.onStop = onCalibrated; //cal.MIN_TIME = .1; // Do calibrations quickly cal.run(2e4); return true; } } return false; } }); jsl.extend(Test.prototype, {/** @lends Test.prototype */ /** Initial number of iterations */ INIT_COUNT: 10, /** Max iterations allowed (i.e. used to detect bad looping functions) */ MAX_COUNT: 1e9, /** Minimum time a test should take to get valid results (secs) */ MIN_TIME: .5, /** Callback invoked when test state changes */ onChange: jsl.F, /** Callback invoked when test is finished */ onStop: jsl.F, /** * Reset test state */ reset: function() { delete this.count; delete this.time; delete this.running; delete this.error; }, /** * Run the test (in a timeout). We use a timeout to make sure the browser * has a chance to finish rendering any UI changes we've made, like * updating the status message. */ run: function(count) { count = count || this.INIT_COUNT; jsl.status(this.name + ' x ' + count); this.running = true; var me = this; setTimeout(function() {me._run(count);}, 200); }, /** * The nuts and bolts code that actually runs a test */ _run: function(count) { var me = this; // Make sure calibration tests have run if (!me.isCalibration && Test.calibrate(function() {me.run(count);})) return; this.error = null; try { var start, f = this.f, now, i = count; // Start the timer start = new Date(); // Now for the money shot. If this is a looping function ... if (this.loopArg) { // ... let it do the iteration itself f(count); } else { // ... otherwise do the iteration for it while (i--) f(); } // Get time test took (in secs) this.time = Math.max(1,new Date() - start)/1000; // Store iteration count and per-operation time taken this.count = count; this.period = this.time/count; // Do we need to do another run? this.running = this.time <= this.MIN_TIME; // ... if so, compute how many times we should iterate if (this.running) { // Bump the count to the nearest power of 2 var x = this.MIN_TIME/this.time; var pow = Math.pow(2, Math.max(1, Math.ceil(Math.log(x)/Math.log(2)))); count *= pow; if (count > this.MAX_COUNT) { throw new Error('Max count exceeded. If this test uses a looping function, make sure the iteration loop is working properly.'); } } } catch (e) { // Exceptions are caught and displayed in the test UI this.reset(); this.error = e; } // Figure out what to do next if (this.running) { me.run(count); } else { jsl.status(''); me.onStop(me); } // Finish up this.onChange(this); }, /** * Get the number of operations per second for this test. * * @param normalize if true, iteration loop overhead taken into account */ getHz: function(/**Boolean*/ normalize) { var p = this.period; // Adjust period based on the calibration test time if (normalize && !this.isCalibration) { var cal = Test.CALIBRATIONS[this.loopArg ? 0 : 1]; // If the period is within 20% of the calibration time, then zero the // it out p = p < cal.period*1.2 ? 0 : p - cal.period; } return Math.round(1/p); }, /** * Get a friendly string describing the test */ toString: function() { return this.name + ' - ' + this.time/this.count + ' secs'; } }); // CSS we need for the UI var STYLESHEET = ''; // HTML markup for the UI var MARKUP = '
' + platform + ' | |
---|---|
Test | Ops/sec |