Skip to content

scratcharchive/test-wasm-efficiency

Repository files navigation

WebAssembly vs Native Convolution Benchmark

This project benchmarks a 1D convolution implementation comparing different execution methods:

  • WebAssembly in browser
  • WebAssembly in Node.js
  • Native shared library from Node.js
  • Native CLI executable

Prerequisites

  • CMake (for native builds)
  • Emscripten (for WebAssembly builds)
  • Node.js with build tools for native modules
  • Python3 or any other local web server (for browser tests)

Installation

  1. Install Node.js dependencies:
# Install required packages for native and WASM benchmarks
npm install

Note: The ffi-napi and ref-napi packages require Python and a C/C++ compiler to be installed for building native modules. On Linux, you may need to install build-essential. On macOS, you need Xcode Command Line Tools.

  1. Build both native and WebAssembly versions:
./build.sh

This will:

  • Create native builds (CLI executable and shared library) in the build directory
  • Create WebAssembly builds (conv.js and conv.wasm)

Running Benchmarks

1. Native CLI

Run the native executable directly:

./build/conv1d_cli

2. Node.js Benchmarks (Native + WASM)

The Node.js benchmark compares the native shared library against the WebAssembly version:

# Run the benchmark
node benchmark.js

This will:

  • Load both the native shared library and WebAssembly module
  • Run convolution operations with specified input/kernel sizes (default: input size 100000, kernel size 100)
  • Perform 100 iterations of each operation
  • Report timing comparisons between native and WASM versions

The benchmark outputs:

  • WASM execution time in milliseconds
  • Native execution time in milliseconds

Common issues:

  • If you get errors about missing .so files, ensure build.sh completed successfully
  • If you get node-gyp errors during npm install, ensure you have Python and build tools installed

3. Browser Benchmark (WASM)

  1. Start a local web server. For example, using Python:
# Python 3
python3 -m http.server

# Python 2
python -m SimpleHTTPServer
  1. Open http://localhost:8000/benchmark.html in your web browser

The browser benchmark provides an interactive interface where you can:

  • Adjust input size
  • Adjust kernel size
  • Set number of iterations
  • Run benchmarks and see results in a table

Project Structure

  • conv.c - Core convolution implementation
  • main.c - CLI program implementation
  • benchmark.js - Node.js benchmark script
  • benchmark.html - Browser-based benchmark
  • build.sh - Build script for native and WASM versions
  • CMakeLists.txt - CMake configuration for native builds

Notes

  • The browser benchmark only tests WebAssembly performance since native code cannot run in browsers
  • The Node.js benchmark compares both native and WebAssembly performance
  • Memory is automatically managed in all versions, but explicit cleanup is performed in WebAssembly tests
  • The convolution implementation uses double precision floating point numbers

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published