diff --git a/.gitignore b/.gitignore index e8db92e..b29abc1 100644 --- a/.gitignore +++ b/.gitignore @@ -14,4 +14,6 @@ logs *.log *.swo example/app-framework/cmake-build/ -example/app-framework/out/ \ No newline at end of file +example/app-framework/out/ +.vscode/ +tests/benchmark/compile_output/ \ No newline at end of file diff --git a/tests/benchmark/any_basic_type_access.js b/tests/benchmark/any_basic_type_access.js index 39cc03e..a9733d6 100644 --- a/tests/benchmark/any_basic_type_access.js +++ b/tests/benchmark/any_basic_type_access.js @@ -1,16 +1,14 @@ +"use strict"; /* * Copyright (C) 2023 Intel Corporation. All rights reserved. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ -"use strict"; - -var size = 1e6; -var val = 0; -var res = 0; -const expect = 499998500001; - function main() { + var size = 1e6; + var val = 0; + var res = 0; + var expect = 499998500001; for (var i = 0; i < size; i++) { res += val; val = i; @@ -21,4 +19,4 @@ function main() { return res; } -console.log(main()); +main() diff --git a/tests/benchmark/any_basic_type_access.ts b/tests/benchmark/any_basic_type_access.ts index 3e7ca6d..728c716 100644 --- a/tests/benchmark/any_basic_type_access.ts +++ b/tests/benchmark/any_basic_type_access.ts @@ -3,12 +3,11 @@ * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ -const size = 1e6; -let val: any = 0; -let res = 0; -const expect = 499998500001; - export function main() { + const size = 1e6; + let val: any = 0; + let res = 0; + const expect = 499998500001; for (let i = 0; i < size; i++) { res += val; val = i; diff --git a/tests/benchmark/any_complex_type_access.js b/tests/benchmark/any_complex_type_access.js index 35d2841..45b2b6e 100644 --- a/tests/benchmark/any_complex_type_access.js +++ b/tests/benchmark/any_complex_type_access.js @@ -1,10 +1,9 @@ +"use strict"; /* * Copyright (C) 2023 Intel Corporation. All rights reserved. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ -"use strict"; - var Foo = /** @class */ (function () { function Foo() { this.x = 0; @@ -12,12 +11,12 @@ var Foo = /** @class */ (function () { } return Foo; }()); -var val = new Foo(); -val.z = 0; -var size = 4e5; -var res = 0; -var expect = 159998800002; function main() { + var val = new Foo(); + val.z = 0; + var size = 4e5; + var res = 0; + var expect = 159998800002; for (var i = 0; i < size; i++) { res += val.x; val.x = i; @@ -29,4 +28,5 @@ function main() { } return res; } -console.log(main()); + +main() diff --git a/tests/benchmark/any_complex_type_access.ts b/tests/benchmark/any_complex_type_access.ts index 5a223a0..5ffdc82 100644 --- a/tests/benchmark/any_complex_type_access.ts +++ b/tests/benchmark/any_complex_type_access.ts @@ -7,13 +7,13 @@ class Foo { x = 0; y = false; } -const val: any = new Foo(); -val.z = 0; -const size = 4e5; -let res = 0; -const expect = 159998800002; export function main() { + const val: any = new Foo(); + val.z = 0; + const size = 4e5; + let res = 0; + const expect = 159998800002; for (let i = 0; i < size; i++) { res += val.x; val.x = i; diff --git a/tests/benchmark/array_access.js b/tests/benchmark/array_access.js index a1b1e7b..17c0851 100644 --- a/tests/benchmark/array_access.js +++ b/tests/benchmark/array_access.js @@ -1,24 +1,24 @@ +"use strict"; /* * Copyright (C) 2023 Intel Corporation. All rights reserved. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ -"use strict"; - -const size = 1e4; -const arr = new Array(size); -const expect = 49999995000000; -let res = 0; - function main() { - for (let i = 0, j = 0; i < 1e7; i++, j++) { + var size = 1e4; + var arr = new Array(size); + var expect = 49999995000000; + var res = 0; + for (var i = 0, j = 0; i < 1e7; i++, j++) { arr[j] = i; res += arr[j]; - if (j >= size - 1) j = 0; + if (j >= size - 1) + j = 0; } if (res !== expect) { console.log('Validate result error in array access'); } return res; } -console.log(main()); + +main() diff --git a/tests/benchmark/array_access.ts b/tests/benchmark/array_access.ts index 8901804..f4e2fa2 100644 --- a/tests/benchmark/array_access.ts +++ b/tests/benchmark/array_access.ts @@ -3,12 +3,12 @@ * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ -const size = 1e4; -const arr = new Array(size); -const expect = 49999995000000; -let res = 0; - export function main() { + const size = 1e4; + const arr = new Array(size); + const expect = 49999995000000; + let res = 0; + for (let i = 0, j = 0; i < 1e7; i++, j++) { arr[j] = i; res += arr[j]; diff --git a/tests/benchmark/array_access_i32.js b/tests/benchmark/array_access_i32.js index 09a2f18..eb7495e 100644 --- a/tests/benchmark/array_access_i32.js +++ b/tests/benchmark/array_access_i32.js @@ -1,24 +1,24 @@ +"use strict"; /* * Copyright (C) 2023 Intel Corporation. All rights reserved. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ -"use strict"; - -const size = 1e4; -const arr = new Array(size); -const expect = 49999995000000; -let res = 0; - function main() { - for (let i = 0, j = 0; i < 1e7; i++, j++) { + var size = 1e4; + var arr = new Array(1e4); + var expect = 49999995000000; + var res = 0; + for (var i = 0, j = 0; i < 1e7; i++, j++) { arr[j] = i; res += arr[j]; - if (j >= size - 1) j = 0; + if (j >= size - 1) + j = 0; } if (res !== expect) { console.log('Validate result error in array access (i32 index)'); } return res; } -console.log(main()); + +main() diff --git a/tests/benchmark/array_access_i32.ts b/tests/benchmark/array_access_i32.ts index 4551cb4..db8345e 100644 --- a/tests/benchmark/array_access_i32.ts +++ b/tests/benchmark/array_access_i32.ts @@ -3,12 +3,13 @@ * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ -const size: i32 = 1e4; -const arr = new Array(1e4); -const expect = 49999995000000; -let res = 0; +type i32 = number; export function main() { + const size: i32 = 1e4; + const arr = new Array(1e4); + const expect = 49999995000000; + let res = 0; for (let i = 0, j: i32 = 0; i < 1e7; i++, j++) { arr[j] = i; res += arr[j]; diff --git a/tests/benchmark/binarytrees_class.js b/tests/benchmark/binarytrees_class.js index b56141d..f15fb8e 100644 --- a/tests/benchmark/binarytrees_class.js +++ b/tests/benchmark/binarytrees_class.js @@ -1,4 +1,3 @@ -/* This file is generated by tsc */ "use strict"; /* This file is modified base on: * https://github.com/hanabi1224/Programming-Language-Benchmarks/blob/main/bench/algorithm/binarytrees/1.ts @@ -29,6 +28,7 @@ function main() { // console.log(`${iterations} trees of depth ${depth} check: ${sum}`) } } + function checksum(node) { if (!node) { return 1; @@ -47,4 +47,5 @@ function createTree(depth) { return new Tree(null, null); } } -main(); + +main() diff --git a/tests/benchmark/binarytrees_interface.js b/tests/benchmark/binarytrees_interface.js index cf0792a..4d0205c 100644 --- a/tests/benchmark/binarytrees_interface.js +++ b/tests/benchmark/binarytrees_interface.js @@ -1,8 +1,8 @@ -/* This file is generated by tsc */ "use strict"; /* This file is modified base on: * https://github.com/hanabi1224/Programming-Language-Benchmarks/blob/main/bench/algorithm/binarytrees/1.ts */ + function main() { var maxDepth = Math.max(10, 0); var stretchDepth = maxDepth + 1; @@ -19,6 +19,7 @@ function main() { //console.log(`${iterations} trees of depth ${depth} check: ${sum}`) } } + function checksum(node) { if (!node) { return 1; @@ -37,4 +38,5 @@ function createTree(depth) { return { left: null, right: null }; } } -main(); + +main() diff --git a/tests/benchmark/class_access.js b/tests/benchmark/class_access.js index b176a92..2c978eb 100644 --- a/tests/benchmark/class_access.js +++ b/tests/benchmark/class_access.js @@ -1,10 +1,9 @@ +"use strict"; /* * Copyright (C) 2023 Intel Corporation. All rights reserved. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ -"use strict"; - var Foo = /** @class */ (function () { function Foo(x) { this.x = x; @@ -14,11 +13,11 @@ var Foo = /** @class */ (function () { }; return Foo; }()); -var size = 1e7; -var expect = 99999970000002; -var res = 0; -var f = new Foo(0); function main() { + var size = 1e7; + var expect = 99999970000002; + var res = 0; + var f = new Foo(0); for (var i = 0; i < size; i++) { res += f.x; res += f.bar(); @@ -30,4 +29,4 @@ function main() { return res; } -console.log(main()); +main() diff --git a/tests/benchmark/class_access.ts b/tests/benchmark/class_access.ts index 3cbe436..6d12974 100644 --- a/tests/benchmark/class_access.ts +++ b/tests/benchmark/class_access.ts @@ -13,12 +13,12 @@ class Foo { } } -const size = 1e7; -const expect = 99999970000002; -let res = 0; -const f = new Foo(0); - export function main() { + const size = 1e7; + const expect = 99999970000002; + let res = 0; + const f = new Foo(0); + for (let i = 0; i < size; i++) { res += f.x; res += f.bar(); diff --git a/tests/benchmark/class_allocation.js b/tests/benchmark/class_allocation.js index fc565c1..f859534 100644 --- a/tests/benchmark/class_allocation.js +++ b/tests/benchmark/class_allocation.js @@ -1,10 +1,9 @@ +"use strict"; /* * Copyright (C) 2023 Intel Corporation. All rights reserved. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ -"use strict"; - var Foo = /** @class */ (function () { function Foo(x) { this.x = x; @@ -14,15 +13,16 @@ var Foo = /** @class */ (function () { }; return Foo; }()); -var size = 1e7; -var len = 10; -var arr = new Array(len); function main() { + var size = 1e7; + var len = 10; + var arr = new Array(len); for (var i = 0, j = 0; i < size; i++, j++) { - if (j >= len) j = 0; + if (j >= len) + j = 0; arr[j] = new Foo(i); } return arr; } -console.log(main()); +main() diff --git a/tests/benchmark/class_allocation.ts b/tests/benchmark/class_allocation.ts index 6e5e1ae..bf9072f 100644 --- a/tests/benchmark/class_allocation.ts +++ b/tests/benchmark/class_allocation.ts @@ -13,11 +13,11 @@ class Foo { } } -const size = 1e7; -const len = 10; - -const arr = new Array(len); export function main() { + const size = 1e7; + const len = 10; + const arr = new Array(len); + for (let i = 0, j = 0; i < size; i++, j++) { if (j >= len) j = 0; arr[j] = new Foo(i); diff --git a/tests/benchmark/compile_benchmark.js b/tests/benchmark/compile_benchmark.js new file mode 100644 index 0000000..2397d19 --- /dev/null +++ b/tests/benchmark/compile_benchmark.js @@ -0,0 +1,61 @@ +/* + * Copyright (C) 2023 Intel Corporation. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + */ + +import fs from 'fs'; +import path from 'path'; +import { execSync } from 'child_process'; +import { fileURLToPath } from 'url'; +import { dirname } from 'path'; + +const __filename = fileURLToPath(import.meta.url); +const benchmark_dir = dirname(__filename); +const benchmarks = fs.readdirSync(benchmark_dir); +const compile_output_dir = path.join(benchmark_dir, 'compile_output'); +const ts2wasm_script = path.join(benchmark_dir, '../../build/cli/ts2wasm.js'); +const optimize_level = 3; + +let tsc_cmd; +try { + tsc_cmd = execSync('which tsc').toString().trim(); +} catch (error) { + if (process.env.TSC_PATH) { + tsc_cmd = process.env.TSC_PATH; + } else { + const default_tsc_path = '/usr/local/bin/tsc'; + if (fs.existsSync(default_tsc_path)) { + tsc_cmd = default_tsc_path; + } else { + console.error("Error: TSC_PATH is not defined, and no default node path is provided."); + process.exit(1); + } + } +} + +console.log(`\x1b[33m======================== options ========================\x1b[0m`); +console.log(`TSC_PATH: ${tsc_cmd}`); +console.log(`\x1b[33m======================== compiling ========================\x1b[0m`); + +for (let benchmark of benchmarks) { + let filename = path.basename(benchmark); + let prefix = path.basename(filename, path.extname(filename)); + let extension = path.extname(filename).slice(1); + + if (extension != 'ts') + continue; + + fs.mkdirSync(compile_output_dir, { recursive: true }, (err) => { + if (err) { + console.error(`Failed to create ${compile_output_dir}:`, err); + } + }) + + console.log(`Compiling ${prefix} benchmark: `); + execSync(`node ${ts2wasm_script} ${filename} --opt ${optimize_level} --output ${compile_output_dir}/${prefix}.wasm > tmp.txt`); + console.log(` wasm target success`); + execSync(`${tsc_cmd} ${filename} --outDir ${compile_output_dir} -m esnext`); + console.log(` js target success`); +} + +execSync(`rm -f tmp.txt`); diff --git a/tests/benchmark/fibonacci.js b/tests/benchmark/fibonacci.js index 912daea..d3d521f 100644 --- a/tests/benchmark/fibonacci.js +++ b/tests/benchmark/fibonacci.js @@ -1,5 +1,9 @@ -/* This file is generated by tsc */ "use strict"; +/* + * Copyright (C) 2023 Intel Corporation. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + */ + function fibonacci(num) { if (num < 2) { return num; @@ -11,4 +15,5 @@ function fibonacci(num) { function main() { fibonacci(35); } + main() diff --git a/tests/benchmark/interface_access_field_fastpath.js b/tests/benchmark/interface_access_field_fastpath.js index f10be8e..a70ecb7 100644 --- a/tests/benchmark/interface_access_field_fastpath.js +++ b/tests/benchmark/interface_access_field_fastpath.js @@ -1,10 +1,9 @@ +"use strict"; /* * Copyright (C) 2023 Intel Corporation. All rights reserved. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ -"use strict"; - var Bar = /** @class */ (function () { function Bar() { this.x = 0; @@ -12,20 +11,19 @@ var Bar = /** @class */ (function () { } return Bar; }()); -var size = 1e6; -var expect = 499998500001; -var f = new Bar(); -var res = 0; function main() { + var size = 1e6; + var expect = 499998500001; + var f = new Bar(); + var res = 0; for (var i = 0; i < size; i++) { res += f.x; f.x = i; } if (res !== expect) { - console.log( - 'Validate result error in interface access field (fast path)', - ); + console.log('Validate result error in interface access field (fast path)'); } return res; } -console.log(main()); + +main() diff --git a/tests/benchmark/interface_access_field_fastpath.ts b/tests/benchmark/interface_access_field_fastpath.ts index f9709b6..e1f654d 100644 --- a/tests/benchmark/interface_access_field_fastpath.ts +++ b/tests/benchmark/interface_access_field_fastpath.ts @@ -14,12 +14,13 @@ class Bar { y = false; z?: string; } -const size = 1e6; -const expect = 499998500001; -const f: Foo = new Bar(); -let res = 0; export function main() { + const size = 1e6; + const expect = 499998500001; + const f: Foo = new Bar(); + let res = 0; + for (let i = 0; i < size; i++) { res += f.x; f.x = i; diff --git a/tests/benchmark/interface_access_field_slowpath.js b/tests/benchmark/interface_access_field_slowpath.js index 97bed23..aa1c768 100644 --- a/tests/benchmark/interface_access_field_slowpath.js +++ b/tests/benchmark/interface_access_field_slowpath.js @@ -1,10 +1,9 @@ +"use strict"; /* * Copyright (C) 2023 Intel Corporation. All rights reserved. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ -"use strict"; - var Bar = /** @class */ (function () { function Bar() { this.y = false; @@ -12,20 +11,19 @@ var Bar = /** @class */ (function () { } return Bar; }()); -var size = 1e6; -var expect = 499998500001; -var f = new Bar(); -var res = 0; function main() { + var size = 1e6; + var expect = 499998500001; + var f = new Bar(); + var res = 0; for (var i = 0; i < size; i++) { res += f.x; f.x = i; } if (res !== expect) { - console.log( - 'Validate result error in interface access field (slow path)', - ); + console.log('Validate result error in interface access field (slow path)'); } return res; } -console.log(main()); + +main() diff --git a/tests/benchmark/interface_access_field_slowpath.ts b/tests/benchmark/interface_access_field_slowpath.ts index 40d00bb..99551cd 100644 --- a/tests/benchmark/interface_access_field_slowpath.ts +++ b/tests/benchmark/interface_access_field_slowpath.ts @@ -13,12 +13,13 @@ class Bar { y = false; x = 0; } -const size = 1e6; -const expect = 499998500001; -const f: Foo = new Bar(); -let res = 0; export function main() { + const size = 1e6; + const expect = 499998500001; + const f: Foo = new Bar(); + let res = 0; + for (let i = 0; i < size; i++) { res += f.x; f.x = i; diff --git a/tests/benchmark/interface_access_method_fastpath.js b/tests/benchmark/interface_access_method_fastpath.js index eacdb7d..8415340 100644 --- a/tests/benchmark/interface_access_method_fastpath.js +++ b/tests/benchmark/interface_access_method_fastpath.js @@ -1,10 +1,9 @@ +"use strict"; /* * Copyright (C) 2023 Intel Corporation. All rights reserved. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ -"use strict"; - var Bar = /** @class */ (function () { function Bar(x) { this.x = x; @@ -15,19 +14,18 @@ var Bar = /** @class */ (function () { }; return Bar; }()); -var size = 1e6; -var expect = 499999500000; -var f = new Bar(0); -var res = 0; function main() { + var size = 1e6; + var expect = 499999500000; + var f = new Bar(0); + var res = 0; for (var i = 0; i < size; i++) { res += f.bar(i); } if (res !== expect) { - console.log( - 'Validate result error in interface access method (fast path)', - ); + console.log('Validate result error in interface access method (fast path)'); } return res; } -console.log(main()); + +main() diff --git a/tests/benchmark/interface_access_method_fastpath.ts b/tests/benchmark/interface_access_method_fastpath.ts index d18ce8e..6ffddad 100644 --- a/tests/benchmark/interface_access_method_fastpath.ts +++ b/tests/benchmark/interface_access_method_fastpath.ts @@ -20,12 +20,13 @@ class Bar { this.x = x; } } -const size = 1e6; -const expect = 499999500000; -const f: Foo = new Bar(0); -let res = 0; export function main() { + const size = 1e6; + const expect = 499999500000; + const f: Foo = new Bar(0); + let res = 0; + for (let i = 0; i < size; i++) { res += f.bar(i); } diff --git a/tests/benchmark/interface_access_method_slowpath.js b/tests/benchmark/interface_access_method_slowpath.js index 2f70aa6..bc46d75 100644 --- a/tests/benchmark/interface_access_method_slowpath.js +++ b/tests/benchmark/interface_access_method_slowpath.js @@ -1,10 +1,9 @@ +"use strict"; /* * Copyright (C) 2023 Intel Corporation. All rights reserved. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ -"use strict"; - var Bar = /** @class */ (function () { function Bar(x) { this.y = 'str'; @@ -20,19 +19,18 @@ var Bar = /** @class */ (function () { }; return Bar; }()); -var size = 1e6; -var expect = 499999500000; -var f = new Bar(0); -var res = 0; function main() { + var size = 1e6; + var expect = 499999500000; + var f = new Bar(0); + var res = 0; for (var i = 0; i < size; i++) { res += f.bar(i); } if (res !== expect) { - console.log( - 'Validate result error in interface access method (slow path)', - ); + console.log('Validate result error in interface access method (slow path)'); } return res; } -console.log(main()); + +main() diff --git a/tests/benchmark/interface_access_method_slowpath.ts b/tests/benchmark/interface_access_method_slowpath.ts index cb5f501..36928f5 100644 --- a/tests/benchmark/interface_access_method_slowpath.ts +++ b/tests/benchmark/interface_access_method_slowpath.ts @@ -26,12 +26,13 @@ class Bar { this.x = x; } } -const size = 1e6; -const expect = 499999500000; -const f: Foo = new Bar(0); -let res = 0; export function main() { + const size = 1e6; + const expect = 499999500000; + const f: Foo = new Bar(0); + let res = 0; + for (let i = 0; i < size; i++) { res += f.bar(i); } diff --git a/tests/benchmark/mandelbrot.js b/tests/benchmark/mandelbrot.js index c3698eb..21b939d 100644 --- a/tests/benchmark/mandelbrot.js +++ b/tests/benchmark/mandelbrot.js @@ -1,4 +1,3 @@ -/* This file is generated by tsc */ "use strict"; /* This file is modified base on: * https://github.com/ColinEberhardt/wasm-mandelbrot/blob/master/assemblyscript/mandelbrot.ts @@ -54,4 +53,5 @@ function main() { var data = new Array(WIDTH * HEIGHT * 4); mandelbrot(data, HEIGHT, WIDTH, 10000, -0.743644786, 0.1318252536, 0.00029336); } -main(); + +main() diff --git a/tests/benchmark/mandelbrot_i32.js b/tests/benchmark/mandelbrot_i32.js index c3698eb..b8d2e73 100644 --- a/tests/benchmark/mandelbrot_i32.js +++ b/tests/benchmark/mandelbrot_i32.js @@ -1,10 +1,8 @@ -/* This file is generated by tsc */ "use strict"; /* This file is modified base on: * https://github.com/ColinEberhardt/wasm-mandelbrot/blob/master/assemblyscript/mandelbrot.ts */ -// const WIDTH = 1200; -// const HEIGHT = 800; + function colour(iteration, offset, scale) { iteration = (iteration * scale + offset) & 1023; if (iteration < 256) { @@ -41,9 +39,16 @@ function mandelbrot(data, HEIGHT, WIDTH, maxIterations, cx, cy, diameter) { var iterations = iterateEquation(rx, ry, maxIterations); var outside = iterations == maxIterations; var idx = (x + y * WIDTH) << 2; - data[idx + 0] = outside ? 0 : colour(iterations, 0, 4); - data[idx + 1] = outside ? 0 : colour(iterations, 128, 4); - data[idx + 2] = outside ? 0 : colour(iterations, 356, 4); + var maxIterationValue = 0; + data[idx + 0] = outside + ? maxIterationValue + : colour(iterations, 0, 4); + data[idx + 1] = outside + ? maxIterationValue + : colour(iterations, 128, 4); + data[idx + 2] = outside + ? maxIterationValue + : colour(iterations, 356, 4); data[idx + 3] = 255; } } @@ -54,4 +59,5 @@ function main() { var data = new Array(WIDTH * HEIGHT * 4); mandelbrot(data, HEIGHT, WIDTH, 10000, -0.743644786, 0.1318252536, 0.00029336); } -main(); + +main() diff --git a/tests/benchmark/merkletrees.js b/tests/benchmark/merkletrees.js index 489eef2..6901235 100644 --- a/tests/benchmark/merkletrees.js +++ b/tests/benchmark/merkletrees.js @@ -1,8 +1,8 @@ -/* This file is generated by tsc */ "use strict"; /* This file is modified base on: * https://github.com/hanabi1224/Programming-Language-Benchmarks/blob/main/bench/algorithm/merkletrees/1.ts */ + var TreeNode = /** @class */ (function () { function TreeNode(value, left, right) { this.hash = null; @@ -72,4 +72,5 @@ function main() { // `long lived tree of depth ${maxDepth}\t root hash: ${longLivedTree.getHash()} check: ${longLivedTree.check()}` // ); } -main(); + +main() diff --git a/tests/benchmark/nbody_class.js b/tests/benchmark/nbody_class.js index c1a0672..b9f1194 100644 --- a/tests/benchmark/nbody_class.js +++ b/tests/benchmark/nbody_class.js @@ -1,4 +1,3 @@ -/* This file is generated by tsc */ "use strict"; /* The Computer Language Benchmarks Game https://salsa.debian.org/benchmarksgame-team/benchmarksgame/ @@ -7,6 +6,7 @@ modified for typescript by Isaac Gouy modified for deno runtime by hanabi1224 */ + /* This file is modified base on: * https://github.com/hanabi1224/Programming-Language-Benchmarks/blob/main/bench/algorithm/nbody/6.ts */ @@ -159,4 +159,5 @@ function main() { } energy(); } -main(); + +main() diff --git a/tests/benchmark/nbody_interface.js b/tests/benchmark/nbody_interface.js index 609bc62..52bef85 100644 --- a/tests/benchmark/nbody_interface.js +++ b/tests/benchmark/nbody_interface.js @@ -1,4 +1,3 @@ -/* This file is generated by tsc */ "use strict"; /* The Computer Language Benchmarks Game https://salsa.debian.org/benchmarksgame-team/benchmarksgame/ @@ -7,6 +6,7 @@ modified for typescript by Isaac Gouy modified for deno runtime by hanabi1224 */ + /* This file is modified base on: * https://github.com/hanabi1224/Programming-Language-Benchmarks/blob/main/bench/algorithm/nbody/6.ts */ @@ -147,4 +147,5 @@ function main() { } energy(); } -main(); + +main() diff --git a/tests/benchmark/quicksort.js b/tests/benchmark/quicksort.js index e132a93..f24ee1a 100644 --- a/tests/benchmark/quicksort.js +++ b/tests/benchmark/quicksort.js @@ -1,22 +1,18 @@ -/* This file is generated by tsc */ "use strict"; /* This file is modified base on: * https://browserbench.org/JetStream/wasm/quicksort.c */ var SORTELEMENTS = 1e5; -var seed = 0; -var maximum, minimum; -var sortList = new Array(SORTELEMENTS + 1); -function rand() { +var maximum = 0; +var minimum = 65536; +function rand(seed) { seed = (seed * 1309 + 13849) & 65535; return seed; } -function initArr() { - seed = 74755; - maximum = 0; - minimum = 65536; +function initArr(sortList) { + var seed = 74755; for (var i = 1; i <= SORTELEMENTS; i++) { - sortList[i] = rand(); + sortList[i] = rand(seed); if (sortList[i] > maximum) { maximum = sortList[i]; } @@ -49,11 +45,12 @@ function quickSort(a, l, r) { quickSort(a, i, r); } function main() { - initArr(); + var sortList = new Array(SORTELEMENTS + 1); + initArr(sortList); quickSort(sortList, 1, SORTELEMENTS); if (sortList[1] != minimum || sortList[SORTELEMENTS] != maximum) { console.log('Validate result error in quicksort'); } } -main(); +main() diff --git a/tests/benchmark/quicksort.ts b/tests/benchmark/quicksort.ts index 1d7b584..a78d020 100644 --- a/tests/benchmark/quicksort.ts +++ b/tests/benchmark/quicksort.ts @@ -2,22 +2,19 @@ * https://browserbench.org/JetStream/wasm/quicksort.c */ const SORTELEMENTS = 1e5; -let seed = 0; -let maximum: number, minimum: number; -const sortList = new Array(SORTELEMENTS + 1); +let maximum = 0; +let minimum = 65536; -function rand() { +function rand(seed: number) { seed = (seed * 1309 + 13849) & 65535; return seed; } -function initArr() { - seed = 74755; - maximum = 0; - minimum = 65536; +function initArr(sortList: number[]) { + const seed = 74755; for (let i = 1; i <= SORTELEMENTS; i++) { - sortList[i] = rand(); + sortList[i] = rand(seed); if (sortList[i] > maximum) { maximum = sortList[i]; } else if (sortList[i] < minimum) { @@ -49,7 +46,8 @@ function quickSort(a: number[], l: number, r: number) { } export function main() { - initArr(); + const sortList = new Array(SORTELEMENTS + 1); + initArr(sortList); quickSort(sortList, 1, SORTELEMENTS); if (sortList[1] != minimum || sortList[SORTELEMENTS] != maximum) { console.log('Validate result error in quicksort'); diff --git a/tests/benchmark/quicksort_float.js b/tests/benchmark/quicksort_float.js index 3e79124..f459bf9 100644 --- a/tests/benchmark/quicksort_float.js +++ b/tests/benchmark/quicksort_float.js @@ -1,22 +1,19 @@ -/* This file is generated by tsc */ "use strict"; + /* This file is modified base on: * https://browserbench.org/JetStream/wasm/quicksort.c */ var SORTELEMENTS = 1e5; -var seed = 0; -var maximum, minimum; -var sortList = new Array(SORTELEMENTS + 1); -function rand() { +var maximum = 0; +var minimum = 65536; +function rand(seed) { seed = (seed * 1309 + 13849) & 65535; return seed; } -function initArr() { - seed = 74755; - maximum = 0; - minimum = 65536; +function initArr(sortList) { + var seed = 74755; for (var i = 1; i <= SORTELEMENTS; i++) { - const val = rand(); + var val = rand(seed); sortList[i] = val + val / 65536; if (sortList[i] > maximum) { maximum = sortList[i]; @@ -50,11 +47,12 @@ function quickSort(a, l, r) { quickSort(a, i, r); } function main() { - initArr(); + var sortList = new Array(SORTELEMENTS + 1); + initArr(sortList); quickSort(sortList, 1, SORTELEMENTS); if (sortList[1] != minimum || sortList[SORTELEMENTS] != maximum) { console.log('Validate result error in quicksort'); } } -main(); +main() diff --git a/tests/benchmark/quicksort_float.ts b/tests/benchmark/quicksort_float.ts index 2691502..b2473eb 100644 --- a/tests/benchmark/quicksort_float.ts +++ b/tests/benchmark/quicksort_float.ts @@ -2,22 +2,19 @@ * https://browserbench.org/JetStream/wasm/quicksort.c */ const SORTELEMENTS = 1e5; -let seed = 0; -let maximum: number, minimum: number; -const sortList = new Array(SORTELEMENTS + 1); +let maximum = 0; +let minimum = 65536; -function rand() { +function rand(seed: number) { seed = (seed * 1309 + 13849) & 65535; return seed; } -function initArr() { - seed = 74755; - maximum = 0; - minimum = 65536; +function initArr(sortList: number[]) { + const seed = 74755; for (let i = 1; i <= SORTELEMENTS; i++) { - const val = rand(); + const val = rand(seed); sortList[i] = val + val / 65536; if (sortList[i] > maximum) { maximum = sortList[i]; @@ -50,7 +47,8 @@ function quickSort(a: number[], l: number, r: number) { } export function main() { - initArr(); + const sortList = new Array(SORTELEMENTS + 1); + initArr(sortList); quickSort(sortList, 1, SORTELEMENTS); if (sortList[1] != minimum || sortList[SORTELEMENTS] != maximum) { console.log('Validate result error in quicksort'); diff --git a/tests/benchmark/run_benchmark_on_chrome.html b/tests/benchmark/run_benchmark_on_chrome.html new file mode 100644 index 0000000..a44f4c9 --- /dev/null +++ b/tests/benchmark/run_benchmark_on_chrome.html @@ -0,0 +1,611 @@ + + + + + + + run benchmark + + + +
+ + + + + + + + + +
+ +
+

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
benchmarkwasm result(ms)js result(ms)wasm result / js result
any_basic_type_access00NAN
array_access00NAN
array_access_i3200NAN
binarytrees_class00NAN
class_access00NAN
class_allocation00NAN
fibonacci00NAN
interface_access_field_fastpath00NAN
interface_access_method_fastpath00NAN
mandelbrot00NAN
mandelbrot_i3200NAN
nbody_class00NAN
nbody_interface00NAN
quicksort00NAN
quicksort_float00NAN
spectral_norm00NAN
spectral_norm_i3200NAN
+

1. WebAssembly and JavaScript both warm-up n times before measuring the execution time

+

2. _i32" suffix means using i32 type in WebAssembly, no effect when building to JavaScript

+

3. The data varies from run to run, the data shown on the table is the average of `runTimes` runs

+ + + + \ No newline at end of file diff --git a/tests/benchmark/run_benchmark_on_chrome.js b/tests/benchmark/run_benchmark_on_chrome.js new file mode 100644 index 0000000..2d844c4 --- /dev/null +++ b/tests/benchmark/run_benchmark_on_chrome.js @@ -0,0 +1,51 @@ +/* + * Copyright (C) 2023 Intel Corporation. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + */ + +import { importObject, setWasmMemory } from '../../tools/validate/run_module/import_object.js'; + +const compile_output_dir = './compile_output/'; + +export async function run_wasm_file(fileName, warmupTimes, runTimes) { + return fetch(compile_output_dir.concat(fileName, '.wasm')) + .then((response) => response.arrayBuffer()) + .then((bytes) => WebAssembly.instantiate(bytes, importObject)) + .then((results) => { + const exports = results.instance.exports; + setWasmMemory(exports.default); + const startFunc = exports._entry; + const funcName = 'main'; + const exportedFunc = exports[funcName]; + startFunc(); + if (warmupTimes) { + for (let i = 0; i < parseInt(warmupTimes); i++) { + exportedFunc(); + } + } + let run_time_total = 0; + if (!runTimes) { + runTimes = 1; + } + for (let i = 0; i < parseInt(runTimes); i++) { + const start_time = performance.now(); + const res = exportedFunc(); + const end_time = performance.now(); + run_time_total += end_time - start_time; + if (typeof res !== 'object' || res === null) { + console.log(`${fileName}.wasm result is : ${res}`); + } else { + console.log(`${fileName}.wasm result is object`); + } + } + const run_time_average = run_time_total / runTimes; + const cell1 = document.getElementById(`${fileName}_1`); + cell1.textContent = run_time_average.toFixed(2); + const cell2_value = document.getElementById(`${fileName}_2`).textContent; + const cell3 = document.getElementById(`${fileName}_3`); + cell3.textContent = (run_time_average / cell2_value).toFixed(2); + }) + .catch((error)=> { + console.error(`${fileName}.wasm occurs error`); + }) +} diff --git a/tests/benchmark/spectral_norm.js b/tests/benchmark/spectral_norm.js index 33c796b..565f5ae 100644 --- a/tests/benchmark/spectral_norm.js +++ b/tests/benchmark/spectral_norm.js @@ -1,4 +1,3 @@ -/* This file is generated by tsc */ "use strict"; /* The Computer Language Benchmarks Game https://salsa.debian.org/benchmarksgame-team/benchmarksgame/ @@ -51,4 +50,5 @@ function main() { var n = 2000; approximate(n); } -main(); + +main() diff --git a/tests/benchmark/spectral_norm_i32.js b/tests/benchmark/spectral_norm_i32.js index 33c796b..e3abea2 100644 --- a/tests/benchmark/spectral_norm_i32.js +++ b/tests/benchmark/spectral_norm_i32.js @@ -1,12 +1,9 @@ -/* This file is generated by tsc */ "use strict"; /* The Computer Language Benchmarks Game https://salsa.debian.org/benchmarksgame-team/benchmarksgame/ contributed by Isaac Gouy */ -/* This file is from: - * https://benchmarksgame-team.pages.debian.net/benchmarksgame/program/spectralnorm-typescript-1.html - */ + function approximate(n) { var u = new Array(n), v = new Array(n); for (var i = 0; i < n; ++i) { @@ -51,4 +48,5 @@ function main() { var n = 2000; approximate(n); } -main(); + +main()