Git Product home page Git Product logo

zbench's Introduction

โšก zBench - A Simple Zig Benchmarking Library

MIT license GitHub code size in bytes PRs Welcome zBench logo

zBench is a simple benchmarking library for the Zig programming language. It is designed to provide easy-to-use functionality to measure and compare the performance of your code.

Content

Installation

For installation instructions, please refer to the documentation.

Usage

Create a new benchmark function in your Zig code. This function should take a single argument of type std.mem.Allocator. The function would run the code you wish to benchmark.

fn benchmarkMyFunction(allocator: std.mem.Allocator) void {
    // Code to benchmark here
}

You can then run your benchmarks in a test:

test "bench test" {
    var bench = zbench.Benchmark.init(std.testing.allocator, .{});
    defer bench.deinit();
    try bench.add("My Benchmark", myBenchmark, .{});
    try bench.run(std.io.getStdOut().writer());
}

Configuration

To customize your benchmark runs, zBench provides a Config struct that allows you to specify several options:

pub const Config = struct {
    iterations: u16 = 0,
    max_iterations: u16 = 16384,
    time_budget_ns: u64 = 2e9, // 2 seconds
    hooks: Hooks = .{},
    track_allocations: bool = false, 
};
  • iterations: The number of iterations the benchmark has been run. This field is usually managed by zBench itself.
  • max_iterations: Set the maximum number of iterations for a benchmark. Useful for controlling long-running benchmarks.
  • time_budget_ns: Define a time budget for the benchmark in nanoseconds. Helps in limiting the total execution time of the benchmark.
  • hooks: Set before_all, after_all, before_each, and after_each hooks to function pointers.
  • track_allocations: Boolean to enable or disable tracking memory allocations during the benchmark.

Compatibility Notes

Zig is in active development and the APIs can change frequently, making it challenging to support every dev build. This project currently aims to be compatible with stable, non-development builds to provide a consistent experience for the users.

Supported Version: As of now, zBench is tested and supported on Zig version 0.12.0.

Benchmark Functions

Benchmark functions have the following signature:

fn(allocator: std.mem.Allocator) void

The function body contains the code you wish to benchmark.

Reporting Benchmarks

zBench provides a comprehensive report for each benchmark run. It includes the total operations performed, the average, min, and max durations of operations, and the percentile distribution (p75, p99, p995) of operation durations.

benchmark              runs     time (avg ยฑ ฯƒ)         (min ... max)                p75        p99        p995
---------------------------------------------------------------------------------------------------------------
benchmarkMyFunction    1000     1200ms ยฑ 10ms          (100ms ... 2000ms)           1100ms     1900ms     1950ms

This example report indicates that the benchmark "benchmarkMyFunction" ran with an average of 1200 ms per execution and a standard deviation of 10 ms. The minimum and maximum execution times were 100 ms and 2000 ms, respectively. The 75th, 99th and 99.5th percentiles of execution times were 1100 ms, 1900 ms, and 1950 ms, respectively.

Running zBench Examples

You can run all example tests with the following command:

zig build test_examples

Troubleshooting

Contributing

The main purpose of this repository is to continue to evolve zBench, making it faster and more efficient. We are grateful to the community for contributing bugfixes and improvements. Read below to learn how you help improve zBench.

Contributing Guide

Read our contributing guide to learn about our development process, how to propose bugfixes and improvements, and how to build and test your changes to zBench.

License

zBench is MIT licensed.

zbench's People

Contributors

bens avatar bryysen avatar fobersteiner avatar hendriknielaender avatar xvzls avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.