moon-hlog

    Interoperate with HdrHistogram interval logs and compare merged latency distributions.

    hdrhistogram
    hlog
    latency
    benchmark
    Download zip
    Version
    0.3.0
    License
    MIT
    Last updated
    2 hours ago
    Downloads
    4

    #MoonHlog

    MoonHlog is a MoonBit library and CLI for HdrHistogram interval logs. It merges recorded bucket counts before computing percentiles, so a comparison reflects the whole selected distribution.

    The sample logs each have 1,100 read measurements. P50 stays at 10,007 ns while P99 rises from 10,007 ns to 180,095 ns.

    #Install the CLI

    Download the archive for Linux x86-64 or macOS Apple Silicon from Release v0.3.0. Extract it and run ./hlog --help. The release includes sample logs and checksums. On a machine with the supported MoonBit toolchain, you can also install the CLI from a checkout with moon install ./cmd/hlog.

    #Run the example

    Use MoonBit 0.10.11+6ff76a5f9, the version used for verification. Clone this repository and run the following from its root.

    moon update moon build --target native --release hlog=_build/native/release/build/cmd/hlog/hlog.exe "$hlog" summary tests/fixtures/baseline.hlog --tag read --json "$hlog" compare tests/fixtures/baseline.hlog tests/fixtures/candidate.hlog \ --tag read --from 1 --until 2 --max-p99-increase-pct 5 "$hlog" report tests/fixtures/baseline.hlog tests/fixtures/candidate.hlog \ --window 1 --max-p99-increase-pct 5 --json "$hlog" rewrite tests/fixtures/candidate.hlog /tmp/moonhlog-candidate.hlog

    The comparison and window report return 1 for this deliberately regressed fixture. Run commands separately when using a shell that stops on nonzero exit. Exit 0 means the command succeeded and any configured threshold passed. Exit 2 means the input or options prevented the command from completing.

    • Add --json to summary, compare or report for structured output. p999 denotes P99.9.
    • report joins both logs by tag and relative-time window. With a threshold, the gate fails if any paired window exceeds it or if one or both sides lack samples. A missing histogram is null in the report.
    • Time windows select interval starts relative to each log's StartTime, with an inclusive --from and exclusive --until. Selected intervals contribute all their samples.
    • Histogram values stay in the producer's units. Compare runs written with the same unit and a comparable workload. Relative P99 increase is undefined when the baseline P99 is zero. With a threshold set, any positive candidate P99 in that case returns 1.

    #Sample-count gating

    Pass --min-samples COUNT to compare or report. The count is a positive integer and applies to both sides. compare checks the selected aggregate, and report checks each selected tag and time window.

    "$hlog" report tests/fixtures/baseline.hlog tests/fixtures/candidate.hlog \ --window 1 --tag write --min-samples 100 --max-p99-increase-pct 5 --json

    The example returns 1. The report row has outcome: "insufficient_data", reason: "below_min_samples", and 30 samples on each side. Percentiles and the relative change remain visible as descriptive values. The P99 threshold is evaluated only when both sides meet the sample minimum.

    • JSON includes the configured min_samples, the gate_failed flag, and the machine-readable outcome and reason. Reports also include an outcome and reason for each row. If a report has both a confirmed regression and insufficient data, its overall outcome is regression.
    • pass means all configured conditions were met. With no gate configured, a comparable result is not_evaluated. A reported P99 threshold breach is regression.
    • With --min-samples enabled, missing samples and empty selections block the gate with exit 1. Invalid arguments or malformed inputs still return 2. The minimum is a policy setting for sample coverage.

    See external log and GitHub Actions integration for a recorded jHiccup log, the real capture command, and a CI example that retains the full JSON report.

    #Unit declarations and streaming reads

    For comparisons of logs with known latency units, declare both --baseline-unit UNIT and --candidate-unit UNIT. Accepted values are ns, us, ms and s. Conflicting declarations return exit 2 and JSON outcome: "incomparable", reason: "unit_mismatch", before comparing any numerical values. Omit both flags for the original producer-unit behavior.

    "$hlog" report tests/fixtures/baseline.hlog tests/fixtures/candidate.hlog \ --window 1 --from 1 --until 2 --baseline-unit ns --candidate-unit ns \ --selected-only --max-p99-increase-pct 5 --json

    summary, compare and report read the logs one line at a time and accumulate selected histograms. By default all payloads are validated. --selected-only still parses every line header and explicitly skips compressed payloads outside the selection. The JSON validation object records checked and skipped payload counts, and text output reports these counts too.

    The library exposes LogParser for incremental input and WindowAccumulator for per-tag/window aggregation. See the benchmark for the measured performance of the streaming reader.

    #Use the library

    The root package is guo-dong-123/moon-hlog. The repository includes an independent consumer module. Run it against this checkout with a temporary workspace:

    moon package scripts/verify-consumer.sh _build/publish/guo-dong-123-moon-hlog-0.3.0.zip

    The verifier imports the public API from the unpacked archive on all four targets, then installs and runs the native CLI. The library exposes Log::windows for one log and Log::compare_windows for paired results.

    fn read_p99(text : StringView) -> Int64? raise @hlog.HlogError {
    let log = @hlog.parse_log(text)
    let selection : @hlog.Selection = {
    tag: Some("read"),
    from: Some(0.0),
    until: Some(2.0),
    }
    match log.aggregate(selection) {
    Some(histogram) => histogram.percentile(9900)
    None => None
    }
    }

    Percentile arguments are basis points of the sample count, so 9,900 selects P99. Empty selections return None; empty histograms have no percentile.

    The format scope is zlib-compressed HdrHistogram V2 integer payloads with unit scale and zero normalization offset, in logs with one time origin. Aggregation requires identical bucket precision and unit magnitude. rewrite preserves interval fields, tags and counts, and normalizes the file header. The decoder permits up to 8 MiB of inflated data per frame. The library is tested on Native, JavaScript, Wasm and Wasm GC. The file-based CLI uses Native.

    #Verification

    moon check --target all --deny-warn moon test --target all --deny-warn moon fmt --check moon build --target native --release --deny-warn scripts/verify-interop.sh _build/native/release/build/cmd/hlog/hlog.exe

    The interop check uses Java, Python 3 and the checksum-pinned HdrHistogram 2.2.2 JAR. It compares eight summaries and the two sides of each of three report rows against Java, then verifies that Java reads every recorded bucket, tag and timestamp from the two rewritten logs. It also verifies a jHiccup capture and the CI summary example. Run the reviewer walkthrough for the offline demo.

    #License

    MoonHlog is MIT-licensed. The interchange format is based on HdrHistogram, available under CC0-1.0 or BSD-2-Clause. The Java files under scripts/ form an independent verification harness. MoonBit's flate, x and async dependencies use Apache-2.0.

    HlogError

    pub(all) suberror HlogError {
    Invalid(String)
    Unsupported(String)
    Incompatible(String)
    } derive(
    Debug
    )

    Errors describing malformed data, unsupported formats, or invalid operations.

    Histogram

    pub struct Histogram {
    // private fields
    }

    An integer HdrHistogram with sorted, sparse logical buckets.

    Histogram::count

    fn Histogram::count(self : Histogram) -> Int64

    Returns the number of samples represented by the histogram.

    Histogram::encode

    fn Histogram::encode(self : Histogram) -> String raise HlogError

    Encodes a histogram as a compressed integer frame accepted by HdrHistogram.

    Histogram::merge

    fn Histogram::merge(self : Histogram, other : Histogram) -> Histogram raise HlogError

    Merges sample counts for compatible integer bucket layouts.

    Histogram::percentile

    fn Histogram::percentile(self : Histogram, basis_points : Int) -> Int64?

    Returns a percentile in the histogram's native units. The 0 to 10,000 basis-point scale represents 0% to 100%.

    Interval

    pub struct Interval {
    tag : String?
    relative_start : Double
    absolute_start : Double
    duration : Double
    histogram : Histogram
    // private fields
    }

    A recorded interval. Times are in seconds and the histogram uses the producer's units.

    Log

    pub struct Log {
    start_time : Double
    base_time : Double
    // private fields
    }

    A sequence of recorded intervals with a shared time origin.

    Log::aggregate

    fn Log::aggregate(self : Log, selection : Selection) -> Histogram? raise HlogError

    Selects interval starts relative to the log's start and merges their sample counts.

    Log::compare_windows

    fn Log::compare_windows(self : Log, other : Log, selection : Selection, seconds : Double) -> Array[WindowComparison] raise HlogError

    Joins the union of selected tag/window keys from two logs.

    Log::encode

    fn Log::encode(self : Log) -> String raise HlogError

    Writes intervals in HdrHistogram log format, preserving the producer’s peak field.

    Log::intervals

    fn Log::intervals(self : Log) -> Array[Interval]

    Returns a shallow copy of the immutable interval records in this log.

    Log::windows

    fn Log::windows(self : Log, selection : Selection, seconds : Double) -> Array[WindowHistogram] raise HlogError

    Groups selected interval starts into [index * seconds, (index + 1) * seconds). Groups are sorted by tag and window index. Each selected interval contributes all its samples to exactly one window.

    LogParser

    pub struct LogParser {
    // private fields
    }

    Incremental parser for interval logs. The parser owns the time origin and line counter.

    LogParser::LogParser

    fn LogParser::LogParser(selection? : Selection, selected_only? : Bool) -> LogParser raise HlogError

    Creates an incremental parser. Rows outside the selection are validated unless selected_only explicitly permits skipping their compressed payloads.

    LogParser::checked_payloads

    fn LogParser::checked_payloads(self : LogParser) -> Int64

    Number of successfully decoded compressed payloads.

    LogParser::push

    fn LogParser::push(self : LogParser, raw : StringView) -> Interval? raise HlogError

    Processes one complete input line, yielding a selected interval if present.

    LogParser::skipped_payloads

    fn LogParser::skipped_payloads(self : LogParser) -> Int64

    Number of compressed payloads skipped by an explicitly selective read.

    Selection

    pub(all) struct Selection {
    tag : String?
    from : Double?
    until : Double?
    } derive(Default)

    Limits a query to recorded interval starts in [from, until).

    WindowAccumulator

    pub struct WindowAccumulator {
    // private fields
    }

    Accumulates one merged histogram per tag and relative-time window.

    WindowAccumulator::WindowAccumulator

    fn WindowAccumulator::WindowAccumulator(selection : Selection, seconds : Double) -> WindowAccumulator raise HlogError

    Creates a window accumulator with validated bounds.

    WindowAccumulator::add

    fn WindowAccumulator::add(self : WindowAccumulator, entry : Interval) -> Unit raise HlogError

    Merges a selected interval into its corresponding window.

    WindowAccumulator::compare

    Compares the sorted results of two accumulators with the same window size.

    WindowAccumulator::windows

    Returns the merged histograms in tag and window order.

    WindowComparison

    pub struct WindowComparison {
    tag : String?
    index : Int64
    baseline : Histogram?
    candidate : Histogram?
    }

    A paired window. Missing tags or windows remain explicit.

    WindowHistogram

    pub struct WindowHistogram {
    tag : String?
    index : Int64
    histogram : Histogram
    }

    A histogram for a single tag and relative-time window.

    decode_histogram

    fn decode_histogram(payload : StringView) -> Histogram raise HlogError

    Decodes one base64-encoded, zlib-compressed HdrHistogram V2 integer frame.

    parse_log

    fn parse_log(text : StringView) -> Log raise HlogError

    Reads an HdrHistogram interval log with V2 compressed integer payloads.