1/* 2 * Javascript Micro benchmark 3 * 4 * Copyright (c) 2017-2019 Fabrice Bellard 5 * Copyright (c) 2017-2019 Charlie Gordon 6 * 7 * Permission is hereby granted, free of charge, to any person obtaining a copy 8 * of this software and associated documentation files (the "Software"), to deal 9 * in the Software without restriction, including without limitation the rights 10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 * copies of the Software, and to permit persons to whom the Software is 12 * furnished to do so, subject to the following conditions: 13 * 14 * The above copyright notice and this permission notice shall be included in 15 * all copies or substantial portions of the Software. 16 * 17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 23 * THE SOFTWARE. 24 */ 25import * as std from "std"; 26 27function pad(str, n) { 28 str += ""; 29 while (str.length < n) 30 str += " "; 31 return str; 32} 33 34function pad_left(str, n) { 35 str += ""; 36 while (str.length < n) 37 str = " " + str; 38 return str; 39} 40 41function pad_center(str, n) { 42 str += ""; 43 while (str.length < n) { 44 if ((n - str.length) & 1) 45 str = str + " "; 46 else 47 str = " " + str; 48 } 49 return str; 50} 51 52function toPrec(n, prec) { 53 var i, s; 54 for (i = 0; i < prec; i++) 55 n *= 10; 56 s = "" + Math.round(n); 57 for (i = s.length - prec; i <= 0; i++) 58 s = "0" + s; 59 if (prec > 0) 60 s = s.substring(0, i) + "." + s.substring(i); 61 return s; 62} 63 64var ref_data; 65var log_data; 66 67var heads = [ "TEST", "N", "TIME (ns)", "REF (ns)", "SCORE (%)" ]; 68var widths = [ 22, 10, 9, 9, 9 ]; 69var precs = [ 0, 0, 2, 2, 2 ]; 70var total = [ 0, 0, 0, 0, 0 ]; 71var total_score = 0; 72var total_scale = 0; 73 74if (typeof console == "undefined") { 75 var console = { log: print }; 76} 77 78function log_line() { 79 var i, n, s, a; 80 s = ""; 81 for (i = 0, n = arguments.length; i < n; i++) { 82 if (i > 0) 83 s += " "; 84 a = arguments[i]; 85 if (typeof a == "number") { 86 total[i] += a; 87 a = toPrec(a, precs[i]); 88 s += pad_left(a, widths[i]); 89 } else { 90 s += pad_left(a, widths[i]); 91 } 92 } 93 console.log(s); 94} 95 96var clocks_per_sec = 1000000; 97var max_iterations = 100; 98var clock_threshold = 2000; /* favoring short measuring spans */ 99var min_n_argument = 1; 100var get_clock; 101 102if (typeof globalThis.__date_clock != "function") { 103 console.log("using fallback millisecond clock"); 104 clocks_per_sec = 1000; 105 max_iterations = 10; 106 clock_threshold = 100; 107 get_clock = Date.now; 108} else { 109 get_clock = globalThis.__date_clock; 110} 111 112function log_one(text, n, ti) { 113 var ref; 114 115 if (ref_data) 116 ref = ref_data[text]; 117 else 118 ref = null; 119 120 ti = Math.round(ti * 100) / 100; 121 log_data[text] = ti; 122 if (typeof ref === "number") { 123 log_line(text, n, ti, ref, ti * 100 / ref); 124 total_score += ti * 100 / ref; 125 total_scale += 100; 126 } else { 127 log_line(text, n, ti); 128 total_score += 100; 129 total_scale += 100; 130 } 131} 132 133function bench(f, text) 134{ 135 var i, j, n, t, t1, ti, nb_its, ref, ti_n, ti_n1, min_ti; 136 137 nb_its = n = 1; 138 if (f.bench) { 139 ti_n = f(text); 140 } else { 141 ti_n = 1000000000; 142 min_ti = clock_threshold / 10; 143 for(i = 0; i < 30; i++) { 144 ti = 1000000000; 145 for (j = 0; j < max_iterations; j++) { 146 t = get_clock(); 147 while ((t1 = get_clock()) == t) 148 continue; 149 nb_its = f(n); 150 if (nb_its < 0) 151 return; // test failure 152 t1 = get_clock() - t1; 153 if (ti > t1) 154 ti = t1; 155 } 156 if (ti >= min_ti) { 157 ti_n1 = ti / nb_its; 158 if (ti_n > ti_n1) 159 ti_n = ti_n1; 160 } 161 if (ti >= clock_threshold && n >= min_n_argument) 162 break; 163 164 n = n * [ 2, 2.5, 2 ][i % 3]; 165 } 166 // to use only the best timing from the last loop, uncomment below 167 //ti_n = ti / nb_its; 168 } 169 /* nano seconds per iteration */ 170 log_one(text, n, ti_n * 1e9 / clocks_per_sec); 171} 172 173var global_res; /* to be sure the code is not optimized */ 174 175function empty_loop(n) { 176 var j; 177 for(j = 0; j < n; j++) { 178 } 179 return n; 180} 181 182function date_now(n) { 183 var j; 184 for(j = 0; j < n; j++) { 185 Date.now(); 186 } 187 return n; 188} 189 190function prop_read(n) 191{ 192 var obj, sum, j; 193 obj = {a: 1, b: 2, c:3, d:4 }; 194 sum = 0; 195 for(j = 0; j < n; j++) { 196 sum += obj.a; 197 sum += obj.b; 198 sum += obj.c; 199 sum += obj.d; 200 } 201 global_res = sum; 202 return n * 4; 203} 204 205function prop_write(n) 206{ 207 var obj, j; 208 obj = {a: 1, b: 2, c:3, d:4 }; 209 for(j = 0; j < n; j++) { 210 obj.a = j; 211 obj.b = j; 212 obj.c = j; 213 obj.d = j; 214 } 215 return n * 4; 216} 217 218function prop_create(n) 219{ 220 var obj, j; 221 for(j = 0; j < n; j++) { 222 obj = new Object(); 223 obj.a = 1; 224 obj.b = 2; 225 obj.c = 3; 226 obj.d = 4; 227 } 228 return n * 4; 229} 230 231function prop_delete(n) 232{ 233 var obj, j; 234 obj = {}; 235 for(j = 0; j < n; j++) { 236 obj[j] = 1; 237 } 238 for(j = 0; j < n; j++) { 239 delete obj[j]; 240 } 241 return n; 242} 243 244function array_read(n) 245{ 246 var tab, len, sum, i, j; 247 tab = []; 248 len = 10; 249 for(i = 0; i < len; i++) 250 tab[i] = i; 251 sum = 0; 252 for(j = 0; j < n; j++) { 253 sum += tab[0]; 254 sum += tab[1]; 255 sum += tab[2]; 256 sum += tab[3]; 257 sum += tab[4]; 258 sum += tab[5]; 259 sum += tab[6]; 260 sum += tab[7]; 261 sum += tab[8]; 262 sum += tab[9]; 263 } 264 global_res = sum; 265 return len * n; 266} 267 268function array_write(n) 269{ 270 var tab, len, i, j; 271 tab = []; 272 len = 10; 273 for(i = 0; i < len; i++) 274 tab[i] = i; 275 for(j = 0; j < n; j++) { 276 tab[0] = j; 277 tab[1] = j; 278 tab[2] = j; 279 tab[3] = j; 280 tab[4] = j; 281 tab[5] = j; 282 tab[6] = j; 283 tab[7] = j; 284 tab[8] = j; 285 tab[9] = j; 286 } 287 return len * n; 288} 289 290function array_prop_create(n) 291{ 292 var tab, i, j, len; 293 len = 1000; 294 for(j = 0; j < n; j++) { 295 tab = []; 296 for(i = 0; i < len; i++) 297 tab[i] = i; 298 } 299 return len * n; 300} 301 302function array_length_decr(n) 303{ 304 var tab, i, j, len; 305 len = 1000; 306 tab = []; 307 for(i = 0; i < len; i++) 308 tab[i] = i; 309 for(j = 0; j < n; j++) { 310 for(i = len - 1; i >= 0; i--) 311 tab.length = i; 312 } 313 return len * n; 314} 315 316function array_hole_length_decr(n) 317{ 318 var tab, i, j, len; 319 len = 1000; 320 tab = []; 321 for(i = 0; i < len; i++) { 322 if (i != 3) 323 tab[i] = i; 324 } 325 for(j = 0; j < n; j++) { 326 for(i = len - 1; i >= 0; i--) 327 tab.length = i; 328 } 329 return len * n; 330} 331 332function array_push(n) 333{ 334 var tab, i, j, len; 335 len = 500; 336 for(j = 0; j < n; j++) { 337 tab = []; 338 for(i = 0; i < len; i++) 339 tab.push(i); 340 } 341 return len * n; 342} 343 344function array_pop(n) 345{ 346 var tab, i, j, len, sum; 347 len = 500; 348 for(j = 0; j < n; j++) { 349 tab = []; 350 for(i = 0; i < len; i++) 351 tab[i] = i; 352 sum = 0; 353 for(i = 0; i < len; i++) 354 sum += tab.pop(); 355 global_res = sum; 356 } 357 return len * n; 358} 359 360function typed_array_read(n) 361{ 362 var tab, len, sum, i, j; 363 len = 10; 364 tab = new Int32Array(len); 365 for(i = 0; i < len; i++) 366 tab[i] = i; 367 sum = 0; 368 for(j = 0; j < n; j++) { 369 sum += tab[0]; 370 sum += tab[1]; 371 sum += tab[2]; 372 sum += tab[3]; 373 sum += tab[4]; 374 sum += tab[5]; 375 sum += tab[6]; 376 sum += tab[7]; 377 sum += tab[8]; 378 sum += tab[9]; 379 } 380 global_res = sum; 381 return len * n; 382} 383 384function typed_array_write(n) 385{ 386 var tab, len, i, j; 387 len = 10; 388 tab = new Int32Array(len); 389 for(i = 0; i < len; i++) 390 tab[i] = i; 391 for(j = 0; j < n; j++) { 392 tab[0] = j; 393 tab[1] = j; 394 tab[2] = j; 395 tab[3] = j; 396 tab[4] = j; 397 tab[5] = j; 398 tab[6] = j; 399 tab[7] = j; 400 tab[8] = j; 401 tab[9] = j; 402 } 403 return len * n; 404} 405 406var global_var0; 407 408function global_read(n) 409{ 410 var sum, j; 411 global_var0 = 0; 412 sum = 0; 413 for(j = 0; j < n; j++) { 414 sum += global_var0; 415 sum += global_var0; 416 sum += global_var0; 417 sum += global_var0; 418 } 419 global_res = sum; 420 return n * 4; 421} 422 423var global_write = 424 (1, eval)(`(function global_write(n) 425 { 426 var j; 427 for(j = 0; j < n; j++) { 428 global_var0 = j; 429 global_var0 = j; 430 global_var0 = j; 431 global_var0 = j; 432 } 433 return n * 4; 434 })`); 435 436function global_write_strict(n) 437{ 438 var j; 439 for(j = 0; j < n; j++) { 440 global_var0 = j; 441 global_var0 = j; 442 global_var0 = j; 443 global_var0 = j; 444 } 445 return n * 4; 446} 447 448function local_destruct(n) 449{ 450 var j, v1, v2, v3, v4; 451 var array = [ 1, 2, 3, 4, 5]; 452 var o = { a:1, b:2, c:3, d:4 }; 453 var a, b, c, d; 454 for(j = 0; j < n; j++) { 455 [ v1, v2,, v3, ...v4] = array; 456 ({ a, b, c, d } = o); 457 ({ a: a, b: b, c: c, d: d } = o); 458 } 459 return n * 12; 460} 461 462var global_v1, global_v2, global_v3, global_v4; 463var global_a, global_b, global_c, global_d; 464 465var global_destruct = 466 (1, eval)(`(function global_destruct(n) 467 { 468 var j, v1, v2, v3, v4; 469 var array = [ 1, 2, 3, 4, 5 ]; 470 var o = { a:1, b:2, c:3, d:4 }; 471 var a, b, c, d; 472 for(j = 0; j < n; j++) { 473 [ global_v1, global_v2,, global_v3, ...global_v4] = array; 474 ({ a: global_a, b: global_b, c: global_c, d: global_d } = o); 475 } 476 return n * 8; 477 })`); 478 479function global_destruct_strict(n) 480{ 481 var j, v1, v2, v3, v4; 482 var array = [ 1, 2, 3, 4, 5 ]; 483 var o = { a:1, b:2, c:3, d:4 }; 484 var a, b, c, d; 485 for(j = 0; j < n; j++) { 486 [ global_v1, global_v2,, global_v3, ...global_v4] = array; 487 ({ a: global_a, b: global_b, c: global_c, d: global_d } = o); 488 } 489 return n * 8; 490} 491 492function func_call(n) 493{ 494 function f(a) 495 { 496 return 1; 497 } 498 499 var j, sum; 500 sum = 0; 501 for(j = 0; j < n; j++) { 502 sum += f(j); 503 sum += f(j); 504 sum += f(j); 505 sum += f(j); 506 } 507 global_res = sum; 508 return n * 4; 509} 510 511function closure_var(n) 512{ 513 function f(a) 514 { 515 sum++; 516 } 517 518 var j, sum; 519 sum = 0; 520 for(j = 0; j < n; j++) { 521 f(j); 522 f(j); 523 f(j); 524 f(j); 525 } 526 global_res = sum; 527 return n * 4; 528} 529 530function int_arith(n) 531{ 532 var i, j, sum; 533 global_res = 0; 534 for(j = 0; j < n; j++) { 535 sum = 0; 536 for(i = 0; i < 1000; i++) { 537 sum += i * i; 538 } 539 global_res += sum; 540 } 541 return n * 1000; 542} 543 544function float_arith(n) 545{ 546 var i, j, sum, a, incr, a0; 547 global_res = 0; 548 a0 = 0.1; 549 incr = 1.1; 550 for(j = 0; j < n; j++) { 551 sum = 0; 552 a = a0; 553 for(i = 0; i < 1000; i++) { 554 sum += a * a; 555 a += incr; 556 } 557 global_res += sum; 558 } 559 return n * 1000; 560} 561 562function bigfloat_arith(n) 563{ 564 var i, j, sum, a, incr, a0; 565 global_res = 0; 566 a0 = BigFloat("0.1"); 567 incr = BigFloat("1.1"); 568 for(j = 0; j < n; j++) { 569 sum = 0; 570 a = a0; 571 for(i = 0; i < 1000; i++) { 572 sum += a * a; 573 a += incr; 574 } 575 global_res += sum; 576 } 577 return n * 1000; 578} 579 580function float256_arith(n) 581{ 582 return BigFloatEnv.setPrec(bigfloat_arith.bind(null, n), 237, 19); 583} 584 585function bigint_arith(n, bits) 586{ 587 var i, j, sum, a, incr, a0, sum0; 588 sum0 = global_res = BigInt(0); 589 a0 = BigInt(1) << BigInt(Math.floor((bits - 10) * 0.5)); 590 incr = BigInt(1); 591 for(j = 0; j < n; j++) { 592 sum = sum0; 593 a = a0; 594 for(i = 0; i < 1000; i++) { 595 sum += a * a; 596 a += incr; 597 } 598 global_res += sum; 599 } 600 return n * 1000; 601} 602 603function bigint64_arith(n) 604{ 605 return bigint_arith(n, 64); 606} 607 608function bigint256_arith(n) 609{ 610 return bigint_arith(n, 256); 611} 612 613function set_collection_add(n) 614{ 615 var s, i, j, len = 100; 616 s = new Set(); 617 for(j = 0; j < n; j++) { 618 for(i = 0; i < len; i++) { 619 s.add(String(i), i); 620 } 621 for(i = 0; i < len; i++) { 622 if (!s.has(String(i))) 623 throw Error("bug in Set"); 624 } 625 } 626 return n * len; 627} 628 629function array_for(n) 630{ 631 var r, i, j, sum; 632 r = []; 633 for(i = 0; i < 100; i++) 634 r[i] = i; 635 for(j = 0; j < n; j++) { 636 sum = 0; 637 for(i = 0; i < 100; i++) { 638 sum += r[i]; 639 } 640 global_res = sum; 641 } 642 return n * 100; 643} 644 645function array_for_in(n) 646{ 647 var r, i, j, sum; 648 r = []; 649 for(i = 0; i < 100; i++) 650 r[i] = i; 651 for(j = 0; j < n; j++) { 652 sum = 0; 653 for(i in r) { 654 sum += r[i]; 655 } 656 global_res = sum; 657 } 658 return n * 100; 659} 660 661function array_for_of(n) 662{ 663 var r, i, j, sum; 664 r = []; 665 for(i = 0; i < 100; i++) 666 r[i] = i; 667 for(j = 0; j < n; j++) { 668 sum = 0; 669 for(i of r) { 670 sum += i; 671 } 672 global_res = sum; 673 } 674 return n * 100; 675} 676 677function math_min(n) 678{ 679 var i, j, r; 680 r = 0; 681 for(j = 0; j < n; j++) { 682 for(i = 0; i < 1000; i++) 683 r = Math.min(i, 500); 684 global_res = r; 685 } 686 return n * 1000; 687} 688 689/* incremental string contruction as local var */ 690function string_build1(n) 691{ 692 var i, j, r; 693 r = ""; 694 for(j = 0; j < n; j++) { 695 for(i = 0; i < 100; i++) 696 r += "x"; 697 global_res = r; 698 } 699 return n * 100; 700} 701 702/* incremental string contruction as arg */ 703function string_build2(n, r) 704{ 705 var i, j; 706 r = ""; 707 for(j = 0; j < n; j++) { 708 for(i = 0; i < 100; i++) 709 r += "x"; 710 global_res = r; 711 } 712 return n * 100; 713} 714 715/* incremental string contruction by prepending */ 716function string_build3(n, r) 717{ 718 var i, j; 719 r = ""; 720 for(j = 0; j < n; j++) { 721 for(i = 0; i < 100; i++) 722 r = "x" + r; 723 global_res = r; 724 } 725 return n * 100; 726} 727 728/* incremental string contruction with multiple reference */ 729function string_build4(n) 730{ 731 var i, j, r, s; 732 r = ""; 733 for(j = 0; j < n; j++) { 734 for(i = 0; i < 100; i++) { 735 s = r; 736 r += "x"; 737 } 738 global_res = r; 739 } 740 return n * 100; 741} 742 743/* sort bench */ 744 745function sort_bench(text) { 746 function random(arr, n, def) { 747 for (var i = 0; i < n; i++) 748 arr[i] = def[(Math.random() * n) >> 0]; 749 } 750 function random8(arr, n, def) { 751 for (var i = 0; i < n; i++) 752 arr[i] = def[(Math.random() * 256) >> 0]; 753 } 754 function random1(arr, n, def) { 755 for (var i = 0; i < n; i++) 756 arr[i] = def[(Math.random() * 2) >> 0]; 757 } 758 function hill(arr, n, def) { 759 var mid = n >> 1; 760 for (var i = 0; i < mid; i++) 761 arr[i] = def[i]; 762 for (var i = mid; i < n; i++) 763 arr[i] = def[n - i]; 764 } 765 function comb(arr, n, def) { 766 for (var i = 0; i < n; i++) 767 arr[i] = def[(i & 1) * i]; 768 } 769 function crisscross(arr, n, def) { 770 for (var i = 0; i < n; i++) 771 arr[i] = def[(i & 1) ? n - i : i]; 772 } 773 function zero(arr, n, def) { 774 for (var i = 0; i < n; i++) 775 arr[i] = def[0]; 776 } 777 function increasing(arr, n, def) { 778 for (var i = 0; i < n; i++) 779 arr[i] = def[i]; 780 } 781 function decreasing(arr, n, def) { 782 for (var i = 0; i < n; i++) 783 arr[i] = def[n - 1 - i]; 784 } 785 function alternate(arr, n, def) { 786 for (var i = 0; i < n; i++) 787 arr[i] = def[i ^ 1]; 788 } 789 function jigsaw(arr, n, def) { 790 for (var i = 0; i < n; i++) 791 arr[i] = def[i % (n >> 4)]; 792 } 793 function incbutone(arr, n, def) { 794 for (var i = 0; i < n; i++) 795 arr[i] = def[i]; 796 if (n > 0) 797 arr[n >> 2] = def[n]; 798 } 799 function incbutfirst(arr, n, def) { 800 if (n > 0) 801 arr[0] = def[n]; 802 for (var i = 1; i < n; i++) 803 arr[i] = def[i]; 804 } 805 function incbutlast(arr, n, def) { 806 for (var i = 0; i < n - 1; i++) 807 arr[i] = def[i + 1]; 808 if (n > 0) 809 arr[n - 1] = def[0]; 810 } 811 812 var sort_cases = [ random, random8, random1, jigsaw, hill, comb, 813 crisscross, zero, increasing, decreasing, alternate, 814 incbutone, incbutlast, incbutfirst ]; 815 816 var n = sort_bench.array_size || 10000; 817 var array_type = sort_bench.array_type || Array; 818 var def, arr; 819 var i, j, x, y; 820 var total = 0; 821 822 var save_total_score = total_score; 823 var save_total_scale = total_scale; 824 825 // initialize default sorted array (n + 1 elements) 826 def = new array_type(n + 1); 827 if (array_type == Array) { 828 for (i = 0; i <= n; i++) { 829 def[i] = i + ""; 830 } 831 } else { 832 for (i = 0; i <= n; i++) { 833 def[i] = i; 834 } 835 } 836 def.sort(); 837 for (var f of sort_cases) { 838 var ti = 0, tx = 0; 839 for (j = 0; j < 100; j++) { 840 arr = new array_type(n); 841 f(arr, n, def); 842 var t1 = get_clock(); 843 arr.sort(); 844 t1 = get_clock() - t1; 845 tx += t1; 846 if (!ti || ti > t1) 847 ti = t1; 848 if (tx >= clocks_per_sec) 849 break; 850 } 851 total += ti; 852 853 i = 0; 854 x = arr[0]; 855 if (x !== void 0) { 856 for (i = 1; i < n; i++) { 857 y = arr[i]; 858 if (y === void 0) 859 break; 860 if (x > y) 861 break; 862 x = y; 863 } 864 } 865 while (i < n && arr[i] === void 0) 866 i++; 867 if (i < n) { 868 console.log("sort_bench: out of order error for " + f.name + 869 " at offset " + (i - 1) + 870 ": " + arr[i - 1] + " > " + arr[i]); 871 } 872 if (sort_bench.verbose) 873 log_one("sort_" + f.name, n, ti, n * 100); 874 } 875 total_score = save_total_score; 876 total_scale = save_total_scale; 877 return total / n / 1000; 878} 879sort_bench.bench = true; 880sort_bench.verbose = false; 881 882function int_to_string(n) 883{ 884 var s, r, j; 885 r = 0; 886 for(j = 0; j < n; j++) { 887 s = (j + 1).toString(); 888 } 889 return n; 890} 891 892function float_to_string(n) 893{ 894 var s, r, j; 895 r = 0; 896 for(j = 0; j < n; j++) { 897 s = (j + 0.1).toString(); 898 } 899 return n; 900} 901 902function string_to_int(n) 903{ 904 var s, r, j; 905 r = 0; 906 s = "12345"; 907 r = 0; 908 for(j = 0; j < n; j++) { 909 r += (s | 0); 910 } 911 global_res = r; 912 return n; 913} 914 915function string_to_float(n) 916{ 917 var s, r, j; 918 r = 0; 919 s = "12345.6"; 920 r = 0; 921 for(j = 0; j < n; j++) { 922 r -= s; 923 } 924 global_res = r; 925 return n; 926} 927 928function load_result(filename) 929{ 930 var f, str, res; 931 if (typeof std === "undefined") 932 return null; 933 f = std.open(filename, "r"); 934 if (!f) 935 return null; 936 str = f.readAsString(); 937 res = JSON.parse(str); 938 f.close(); 939 return res; 940} 941 942function save_result(filename, obj) 943{ 944 var f; 945 if (typeof std === "undefined") 946 return; 947 f = std.open(filename, "w"); 948 f.puts(JSON.stringify(obj, null, 2)); 949 f.puts("\n"); 950 f.close(); 951} 952 953function main(argc, argv, g) 954{ 955 var test_list = [ 956 empty_loop, 957 date_now, 958 prop_read, 959 prop_write, 960 prop_create, 961 prop_delete, 962 array_read, 963 array_write, 964 array_prop_create, 965 array_length_decr, 966 array_hole_length_decr, 967 array_push, 968 array_pop, 969 typed_array_read, 970 typed_array_write, 971 global_read, 972 global_write, 973 global_write_strict, 974 local_destruct, 975 global_destruct, 976 global_destruct_strict, 977 func_call, 978 closure_var, 979 int_arith, 980 float_arith, 981 set_collection_add, 982 array_for, 983 array_for_in, 984 array_for_of, 985 math_min, 986 string_build1, 987 string_build2, 988 //string_build3, 989 //string_build4, 990 sort_bench, 991 int_to_string, 992 float_to_string, 993 string_to_int, 994 string_to_float, 995 ]; 996 var tests = []; 997 var i, j, n, f, name; 998 999 if (typeof BigInt == "function") { 1000 /* BigInt test */ 1001 test_list.push(bigint64_arith); 1002 test_list.push(bigint256_arith); 1003 } 1004 if (typeof BigFloat == "function") { 1005 /* BigFloat test */ 1006 test_list.push(float256_arith); 1007 } 1008 1009 for (i = 1; i < argc;) { 1010 name = argv[i++]; 1011 if (name == "-a") { 1012 sort_bench.verbose = true; 1013 continue; 1014 } 1015 if (name == "-t") { 1016 name = argv[i++]; 1017 sort_bench.array_type = g[name]; 1018 if (typeof sort_bench.array_type != "function") { 1019 console.log("unknown array type: " + name); 1020 return 1; 1021 } 1022 continue; 1023 } 1024 if (name == "-n") { 1025 sort_bench.array_size = +argv[i++]; 1026 continue; 1027 } 1028 for (j = 0; j < test_list.length; j++) { 1029 f = test_list[j]; 1030 if (name === f.name) { 1031 tests.push(f); 1032 break; 1033 } 1034 } 1035 if (j == test_list.length) { 1036 console.log("unknown benchmark: " + name); 1037 return 1; 1038 } 1039 } 1040 if (tests.length == 0) 1041 tests = test_list; 1042 1043 ref_data = load_result("microbench.txt"); 1044 log_data = {}; 1045 log_line.apply(null, heads); 1046 n = 0; 1047 1048 for(i = 0; i < tests.length; i++) { 1049 f = tests[i]; 1050 bench(f, f.name, ref_data, log_data); 1051 if (ref_data && ref_data[f.name]) 1052 n++; 1053 } 1054 if (ref_data) 1055 log_line("total", "", total[2], total[3], total_score * 100 / total_scale); 1056 else 1057 log_line("total", "", total[2]); 1058 1059 if (tests == test_list) 1060 save_result("microbench-new.txt", log_data); 1061} 1062 1063if (!scriptArgs) 1064 scriptArgs = []; 1065main(scriptArgs.length, scriptArgs, this); 1066