criterion-polyglot 0.1.0

An extension trait for criterion providing benchmark methods for various non-Rust programming languages
Documentation
mod helpers;
mod macros;

use criterion_polyglot::{BenchSpec as Spec, CriterionPolyglotExt};

cases! {
    simple {
        python => Spec::from("pass"),
        ruby => Spec::from(""),
        c => Spec::from(""),
        zig => Spec::from(""),
        go => Spec::from("")
    },
    init {
        python => Spec::from("defined_in_init")
            .with_global_init(r#"
                try:
                    defined_in_init
                    # exit with error if already defined
                    exit(1)
                except NameError:
                    # continue with no error if not defined
                    defined_in_init = True
            "#),
        ruby => Spec::from(r#"
                raise NameError.new(msg = "`defined_in_init` not in scope for 'timed' code", name = :defined_in_init) \
                    unless defined? defined_in_init
            "#)
            .with_global_init(r#"
                raise NameError.new(msg = "init ran more than once", name = :defined_in_init) if defined? defined_in_init
                defined_in_init = true
            "#),
        c => Spec::from(r#"
                if (defined_in_init != 1)
                    exit(1);
            "#)
            .with_imports(r#"
                #include <stdlib.h>
            "#)
            .with_declarations(r#"
                int defined_in_init = 0;
            "#)
            .with_global_init(r#"
                defined_in_init += 1;
            "#),
        zig => Spec::from(r#"
                if (defined_in_init != 1) {
                    exit(1);
                }
            "#)
            .with_global_init(r#"
                var defined_in_init: u32 = 0;
                defined_in_init += 1;
            "#)
            .with_imports(r#"const exit = @import("std").os.exit;"#),
        go => Spec::from(r#"
                if defined_in_init != 1 {
                    os.Exit(1)
                }
            "#)
            .with_global_init(r#"
                defined_in_init := 0
                defined_in_init += 1
            "#)
            .with_imports(r#"
                import "os"
            "#)
    },
    setup {
        python => Spec::from(r#"
                if current != prev + 1:
                    raise RuntimeError("setup code didn't run")
            "#)
            .with_sample_init(r#"
                global prev
                prev = 0 if prev is None else prev + 1
                current = prev + 1
            "#)
            .with_global_init(r#"prev = None"#),
        ruby => Spec::from(r#"
                if current != prev + 1
                    abort("setup code didn't run")
                end
            "#)
            .with_sample_init(r#"
                if prev == nil
                    prev = 0
                else
                    prev += 1
                end
                current = prev + 1
             "#)
             .with_global_init(r#"prev = nil"#),
        c => Spec::from(r#"if (current != *prev + 1) { exit(1); }"#)
            .with_sample_init(r#"
                 if (prev == NULL) {
                     prev = malloc(sizeof(int));
                     *prev = 0;
                 } else {
                     *prev = *prev + 1;
                 }
                 current = *prev + 1;
            "#)
            .with_declarations(r#"int* prev = NULL; int current = 0;"#),
        zig => Spec::from(r#"
                if (current != prev.? + 1) {
                    exit(1);
                }
            "#)
            .with_sample_init(r#"
                prev = (prev orelse 0) + 1;
                current = prev.? + 1;
            "#)
            .with_global_init(r#"var prev: ?usize = null; var current: usize = 0;"#)
            .with_imports(r#"const exit = @import("std").os.exit;"#),
        go => Spec::from(r#"
                if (current != *prev + 1) {
                    os.Exit(1)
                }
            "#)
            .with_sample_init(r#"
                if prev == nil {
                    prev = new(int)
                } else {
                    *prev += 1
                }
                current = *prev + 1
            "#)
            .with_global_init(r#"
                var prev *int
                current := 0
            "#)
            .with_imports(r#"
                import "os"
            "#)
    }
}