Categorygithub.com/gotomgo/safehdrhistogram
modulepackage
0.4.0
Repository: https://github.com/gotomgo/safehdrhistogram.git
Documentation: pkg.go.dev

# README

Concurrency Safe HDR Historgram

safehdrhistogram is a GO (Golang) package that wraps https://github.com/HdrHistogram/histogram-go with safe, concurrent read and write access to a hdrhistogram.Histogram, or a collection of named instances of hdrhistogram.Histogram (see HistogramMap)

Installation

$ go get -u github.com/gotomgo/safhdrhistogram

import

import "github.com/gotomgo/safehdrhistogram"

optional import

When you need or want to access the underlying hdrhistogram package directly:

import "github.com/HdrHistogram/hdrhistogram-go"

Quick Start

Create Histogram

// Create a concurrency safe version of HdrHistogram using the standard parameters 
hist := safehdrhistogram.NewHistogram(1, 30000000, 3)

Create a Histogram using a configuration

// Create a concurrency safe version of HdrHistogram using configuration values 
hist := safehdrhistogram.NewHistogramFromConfig(
		safehdrhistogram.HistogramConfig{
			LowestDiscernibleValue:         lowestDiscernibleValue,
			HighestTrackableValue:          highestTrackableValue,
			NumberOfSignificantValueDigits: numberOfSignificantValueDigits,
			CommandBufferSize:              32,
		})

Record Value

startTime := time.Now()
// ... do some work here
hist.RecordValue(time.Since(startTime).Microseconds())

Get Snapshot

// get a snapshot of the histogram (no reset)
snapshot := hist.Snapshot(false)

// create an hdrhistogram.Histogram from a snapshot
histCopy := hist.Snapshot(false).ToHistogram()

Go Routine to ship Percentiles

func shipPercentiles(server string,hist *safehdrhistorgram,done <-chan bool) {
    for {
        select {
            case <-done:
                return
            case <-time.After(time.Duration(60) * time.Second):
                // send to server (code not shown)
                sendPercentilesToServer(server,hist.Percentiles(false))
        }
    }
}

Go Routine to ship Percentiles (fully parameterized)

func shipPercentiles(
	server string,
	hist *safehdrhistorgram,
	interval time.Duration,
	autoReset bool,
	done <-chan bool) {
    for {
        select {
            case <-done:
                return
            case <-time.After(interval):
            	// send to server (code not shown)
                sendPercentilesToServer(server,hist.Percentiles(autoReset))
        }
    }
}

Go Routine to ship Snapshots

func shipSnapshot(server string,hist *safehdrhistorgram,done <-chan bool) {
    for {
        select {
            case <-done:
                return
            case <-time.After(time.Duration(60) * time.Second):
                // send to server (code not shown)
                sendSnapshotToServer(server,hist.Snapshot(false))
        }
    }
}

Reset a Histogram

// reset the state of a histogram
hist.Reset()

// reset the state of a histogram after a snapshot
snapshot := hist.Snapshot(true)

// reset the state of a histogram after a Percentiles snapshot
snapshot := hist.Percentiles(true)

HistogramMap

A HistogramMap manages a collection of histograms that are referenced by name. It allows for dynamic creation of histograms, based on usage, and allows a large number of histograms to be managed by a single command channel that is processed by a single go routine.

The functionality of HistogramMap is more or less the same as Histogram but for operations on individual histograms the name of the histogram must be provided.

// recording to a histogram directly
hist.RecordValue(latency)

// recording to a histograms via a HistogramMap
hist.RecordValue(latency, "get-user")

// we can also record a single value to multiple histograms with one call
hist.RecordValue(latency, "get-user","user-api","api")

Because HistogramMap is a collection of histograms, bulk operations (such as the record example shown above) are supported.

  • Record
  • RequestSnapshot
  • SnapshotAll
  • RequestPercentiles
  • PercentilesAll
  • RequestReset
  • ResetAll

The Request* variants allow multiple names to be specified and are non-blocking. The *All variants operate on all histogram instances in the collection and wait for the operation to complete before returning.

In addition, HistogramMap provides the Names() function to return the names of all the histograms being managed

Examples

About HdrHistogram

# Packages

No description provided by the author

# Functions

CreatePercentiles creates an instance of Percentiles from a hdrhistogram.Histogram.
CreateSnapshot creates an instance of Snapshot from a hdrhistogram.Histogram.
NewHistogram creates an instance of hdrhistogram.Histogram that is safe for concurrent use.
NewHistogramFromConfig creates an instance of hdrhistogram.Histogram that is safe for concurrent use based on values from a HistogramConfig.
NewHistogramFromSnapshot re-creates a Histogram from a snapshot.
NewHistogramMap creates a collection to manage named instances of hdrhistogram.Histogram, that are safe for concurrent use Notes Named histograms are created dynamically, on first reference.
NewHistogramMapFromConfig creates an instance of hdrhistogram.Histogram that is safe for concurrent use based on values from a HistogramConfig Notes The histogram cannot record values (or anything else) until Start() is called.

# Constants

DefaultCommandBufferSize is the default size of the command buffer used to process commands, such as recording a value to a histogram.

# Structs

Histogram is a wrapper around hdrhistogram.Histogram that uses a command channel to safely manipulate the histogram when there are multiple readers and writers.
HistogramConfig represents the values used to construct a Histogram and is designed for use in yaml or JSON configuration files.
HistogramMap is a collection of named hdrhistogram.Histogram instances.
Percentile represents a percentile, it's value, and cumulative count.
Percentiles represents a percentiles snapshot of a hdrhistogram.Histogram Notes Percentiles are ordered lowest percentile to highest .
Snapshot represents a snapshot of a hdrhistogram.Histogram.