UI for Zipcoin Blue

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. /*
  2. Copyright (C) 2012 Ariya Hidayat <ariya.hidayat@gmail.com>
  3. Copyright (C) 2011 Ariya Hidayat <ariya.hidayat@gmail.com>
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions are met:
  6. * Redistributions of source code must retain the above copyright
  7. notice, this list of conditions and the following disclaimer.
  8. * Redistributions in binary form must reproduce the above copyright
  9. notice, this list of conditions and the following disclaimer in the
  10. documentation and/or other materials provided with the distribution.
  11. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  12. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  13. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  14. ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
  15. DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  16. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  17. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  18. ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  19. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  20. THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  21. */
  22. /*jslint browser: true node: true */
  23. /*global load:true, print:true */
  24. var setupBenchmarks,
  25. parsers,
  26. fixtureList,
  27. suite;
  28. parsers = [
  29. 'Esprima',
  30. 'parse-js',
  31. 'Acorn'
  32. ];
  33. fixtureList = [
  34. 'Underscore 1.4.1',
  35. 'Backbone 0.9.2',
  36. 'CodeMirror 2.34',
  37. 'jQuery 1.8.2'
  38. ];
  39. function slug(name) {
  40. 'use strict';
  41. return name.toLowerCase().replace(/\.js/g, 'js').replace(/\s/g, '-');
  42. }
  43. function kb(bytes) {
  44. 'use strict';
  45. return (bytes / 1024).toFixed(1);
  46. }
  47. function inject(fname) {
  48. 'use strict';
  49. var head = document.getElementsByTagName('head')[0],
  50. script = document.createElement('script');
  51. script.src = fname;
  52. script.type = 'text/javascript';
  53. head.appendChild(script);
  54. }
  55. if (typeof window !== 'undefined') {
  56. // Run all tests in a browser environment.
  57. setupBenchmarks = function () {
  58. 'use strict';
  59. function id(i) {
  60. return document.getElementById(i);
  61. }
  62. function setText(id, str) {
  63. var el = document.getElementById(id);
  64. if (typeof el.innerText === 'string') {
  65. el.innerText = str;
  66. } else {
  67. el.textContent = str;
  68. }
  69. }
  70. function enableRunButtons() {
  71. id('run').disabled = false;
  72. }
  73. function disableRunButtons() {
  74. id('run').disabled = true;
  75. }
  76. function createTable() {
  77. var str = '',
  78. i,
  79. index,
  80. test,
  81. name;
  82. str += '<table>';
  83. str += '<thead><tr><th>Source</th><th>Size (KiB)</th>';
  84. for (i = 0; i < parsers.length; i += 1) {
  85. str += '<th>' + parsers[i] + '</th>';
  86. }
  87. str += '</tr></thead>';
  88. str += '<tbody>';
  89. for (index = 0; index < fixtureList.length; index += 1) {
  90. test = fixtureList[index];
  91. name = slug(test);
  92. str += '<tr>';
  93. str += '<td>' + test + '</td>';
  94. if (window.data && window.data[name]) {
  95. str += '<td id="' + name + '-size">' + kb(window.data[name].length) + '</td>';
  96. } else {
  97. str += '<td id="' + name + '-size"></td>';
  98. }
  99. for (i = 0; i < parsers.length; i += 1) {
  100. str += '<td id="' + name + '-' + slug(parsers[i]) + '-time"></td>';
  101. }
  102. str += '</tr>';
  103. }
  104. str += '<tr><td><b>Total</b></td>';
  105. str += '<td id="total-size"></td>';
  106. for (i = 0; i < parsers.length; i += 1) {
  107. str += '<td id="' + slug(parsers[i]) + '-total"></td>';
  108. }
  109. str += '</tr>';
  110. str += '</tbody>';
  111. str += '</table>';
  112. id('result').innerHTML = str;
  113. }
  114. function loadFixtures() {
  115. var index = 0,
  116. totalSize = 0;
  117. function load(test, callback) {
  118. var xhr = new XMLHttpRequest(),
  119. src = '3rdparty/' + test + '.js';
  120. window.data = window.data || {};
  121. window.data[test] = '';
  122. try {
  123. xhr.timeout = 30000;
  124. xhr.open('GET', src, true);
  125. xhr.ontimeout = function () {
  126. setText('status', 'Error: time out while loading ' + test);
  127. callback.apply();
  128. };
  129. xhr.onreadystatechange = function () {
  130. var success = false,
  131. size = 0;
  132. if (this.readyState === XMLHttpRequest.DONE) {
  133. if (this.status === 200) {
  134. window.data[test] = this.responseText;
  135. size = this.responseText.length;
  136. totalSize += size;
  137. success = true;
  138. }
  139. }
  140. if (success) {
  141. setText(test + '-size', kb(size));
  142. } else {
  143. setText('status', 'Please wait. Error loading ' + src);
  144. setText(test + '-size', 'Error');
  145. }
  146. callback.apply();
  147. };
  148. xhr.send(null);
  149. } catch (e) {
  150. setText('status', 'Please wait. Error loading ' + src);
  151. callback.apply();
  152. }
  153. }
  154. function loadNextTest() {
  155. var test;
  156. if (index < fixtureList.length) {
  157. test = fixtureList[index];
  158. index += 1;
  159. setText('status', 'Please wait. Loading ' + test +
  160. ' (' + index + ' of ' + fixtureList.length + ')');
  161. window.setTimeout(function () {
  162. load(slug(test), loadNextTest);
  163. }, 100);
  164. } else {
  165. setText('total-size', kb(totalSize));
  166. setText('status', 'Ready.');
  167. enableRunButtons();
  168. }
  169. }
  170. loadNextTest();
  171. }
  172. function setupParser() {
  173. var i, j;
  174. suite = [];
  175. for (i = 0; i < fixtureList.length; i += 1) {
  176. for (j = 0; j < parsers.length; j += 1) {
  177. suite.push({
  178. fixture: fixtureList[i],
  179. parser: parsers[j]
  180. });
  181. }
  182. }
  183. createTable();
  184. }
  185. function runBenchmarks() {
  186. var index = 0,
  187. totalTime = {};
  188. function reset() {
  189. var i, name;
  190. for (i = 0; i < suite.length; i += 1) {
  191. name = slug(suite[i].fixture) + '-' + slug(suite[i].parser);
  192. setText(name + '-time', '');
  193. }
  194. }
  195. function run() {
  196. var fixture, parser, test, source, fn, benchmark;
  197. if (index >= suite.length) {
  198. setText('status', 'Ready.');
  199. enableRunButtons();
  200. return;
  201. }
  202. fixture = suite[index].fixture;
  203. parser = suite[index].parser;
  204. source = window.data[slug(fixture)];
  205. test = slug(fixture) + '-' + slug(parser);
  206. setText(test + '-time', 'Running...');
  207. setText('status', 'Please wait. Parsing ' + fixture + '...');
  208. // Force the result to be held in this array, thus defeating any
  209. // possible "dead code elimination" optimization.
  210. window.tree = [];
  211. switch (parser) {
  212. case 'Esprima':
  213. fn = function () {
  214. var syntax = window.esprima.parse(source);
  215. window.tree.push(syntax.body.length);
  216. };
  217. break;
  218. case 'parse-js':
  219. fn = function () {
  220. var syntax = window.parseJS.parse(source);
  221. window.tree.push(syntax.length);
  222. };
  223. break;
  224. case 'Acorn':
  225. fn = function () {
  226. var syntax = window.acorn.parse(source);
  227. window.tree.push(syntax.body.length);
  228. };
  229. break;
  230. default:
  231. throw 'Unknown parser type ' + parser;
  232. }
  233. benchmark = new window.Benchmark(test, fn, {
  234. 'onComplete': function () {
  235. setText(this.name + '-time', (1000 * this.stats.mean).toFixed(1));
  236. if (!totalTime[parser]) {
  237. totalTime[parser] = this.stats.mean;
  238. } else {
  239. totalTime[parser] += this.stats.mean;
  240. }
  241. setText(slug(parser) + '-total', (1000 * totalTime[parser]).toFixed(1));
  242. }
  243. });
  244. window.setTimeout(function () {
  245. benchmark.run();
  246. index += 1;
  247. window.setTimeout(run, 211);
  248. }, 211);
  249. }
  250. disableRunButtons();
  251. setText('status', 'Please wait. Running benchmarks...');
  252. reset();
  253. run();
  254. }
  255. id('run').onclick = function () {
  256. runBenchmarks();
  257. };
  258. setText('benchmarkjs-version', ' version ' + window.Benchmark.version);
  259. setText('version', window.esprima.version);
  260. setupParser();
  261. createTable();
  262. disableRunButtons();
  263. loadFixtures();
  264. };
  265. } else {
  266. // TODO
  267. console.log('Not implemented yet!');
  268. }
  269. /* vim: set sw=4 ts=4 et tw=80 : */