JairusSW / as-tral

AssemblyScript benchmarking library

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

logo


Bringing minimalist, state-of-the-art benchmarking to AssemblyScript; as-tral is a port of the statistics-oriented Rust benchmarking library criterion.rs.

Quickstart

In an existing AssemblyScript project, install as-tral.

npm i -D @as-tral/cli

Next, create a __benches__ folder in your assembly directory. Add a file named as-tral.d.ts in there.

assembly/
└── __benches__/
    └── as-tral.d.ts

In as-tral.d.ts, copy and paste

/// <reference types="@as-tral/cli/as-tral" />

Stick any file with a .ts extension in __benches__. You can even have multiple.

assembly/
└── __benches__/
    ├── as-tral.d.ts
    └── my-benchmark.ts

Your benchmark will live in this file. Let's create an example benchmark.

// to ensure accurate benchmarks, we must make sure that binaryen doesn't do any sneaky
// optimizations on our input without us knowing. Thus, we must use `blackbox`.
const input = blackbox("The quick brown fox jumped over the lazy dog.".repeat(10));

// our string here must be a compile time constant.
// open an issue if you'd like to see this constraint lifted.
bench("string split", () => {
    // this function body will be run many times.
    // we must make sure our compiler won't throw away the computation,
    // so we use `blackbox` here again.
    blackbox(input.split(" "));
});

Now, let's run as-tral.

rom@i9-cabin:~/demo$ npx astral
Compiling assembly/__benches__/hello.ts

Benchmarking string split: Warming up for 3000ms
Benchmarking string split: Collecting 100 samples in estimated 5018.6ms (1.2M iterations)
Benchmarking string split: Analyzing
string split            time: [3770.9ns 3775ns 3778.9ns]
Found 4 outliers among 100 measurements (4%)
  3 (3%) low mild
  1 (1%) high mild

Pretty fast!

Flags

The CLI interprets all flags before -- as AssemblyScript compiler options. For example, to enable ESM bindings,

npx astral --bindings esm

If you also want to save to a certain baseline,

npx astral --bindings esm -- --save-baseline mybaseline 

Loading from a certain baseline is similar.

npx astral --bindings esm -- --save-baseline mybaseline --baseline myotherbaseline

About

AssemblyScript benchmarking library

License:Apache License 2.0


Languages

Language:TypeScript 72.2%Language:JavaScript 27.8%