From 2b34e1096369b2293c94e9e69bd441f6b8e0bdef Mon Sep 17 00:00:00 2001 From: Su Yihan Date: Thu, 7 Mar 2024 18:00:17 +0800 Subject: [PATCH] hardcode js run code Signed-off-by: Su Yihan --- .gitignore | 4 +- tests/benchmark/array_access_i32.ts | 1 + tests/benchmark/compile_benchmark.js | 61 +++ tests/benchmark/run_benchmark_on_chrome.html | 453 +++++++++++++++++++ tests/benchmark/run_benchmark_on_chrome.js | 43 ++ 5 files changed, 561 insertions(+), 1 deletion(-) create mode 100644 tests/benchmark/compile_benchmark.js create mode 100644 tests/benchmark/run_benchmark_on_chrome.html create mode 100644 tests/benchmark/run_benchmark_on_chrome.js diff --git a/.gitignore b/.gitignore index e8db92ea..b29abc1b 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/array_access_i32.ts b/tests/benchmark/array_access_i32.ts index 4551cb46..7cd950f8 100644 --- a/tests/benchmark/array_access_i32.ts +++ b/tests/benchmark/array_access_i32.ts @@ -3,6 +3,7 @@ * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ +type i32 = number; const size: i32 = 1e4; const arr = new Array(1e4); const expect = 49999995000000; diff --git a/tests/benchmark/compile_benchmark.js b/tests/benchmark/compile_benchmark.js new file mode 100644 index 00000000..2397d199 --- /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/run_benchmark_on_chrome.html b/tests/benchmark/run_benchmark_on_chrome.html new file mode 100644 index 00000000..dab4fa37 --- /dev/null +++ b/tests/benchmark/run_benchmark_on_chrome.html @@ -0,0 +1,453 @@ + + + + + + + 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
+ + + + \ 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 00000000..d1ff2876 --- /dev/null +++ b/tests/benchmark/run_benchmark_on_chrome.js @@ -0,0 +1,43 @@ +/* + * 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 function run_wasm_file(fileName, warmupTimes, cell) { + 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]; + if (warmupTimes) { + for (let i = 0; i < parseInt(warmupTimes); i++) { + startFunc(); + exportedFunc(); + } + } + const start_time = performance.now(); + startFunc(); + const res = exportedFunc(); + const end_time = performance.now(); + if (typeof res !== 'object' || res === null) { + console.log(`${fileName}.wasm result is : ${res}`); + } + const run_time = end_time - start_time; + const cell1 = document.getElementById(`${fileName}_1`); + cell1.textContent = run_time.toFixed(2); + const cell2_value = document.getElementById(`${fileName}_2`).textContent; + const cell3 = document.getElementById(`${fileName}_3`); + cell3.textContent = (run_time / cell2_value).toFixed(2); + }) + .catch((error)=> { + console.error(`${fileName}.wasm occurs error`); + }) +}