Skip to content

Concurrent Reads and Writes

Compare
Choose a tag to compare
@LeeCampbell LeeCampbell released this 02 Sep 07:21
· 53 commits to master since this release

This release features support for thread safe reading and writing of histogram values.

The concurrent implementations of the histograms (LongConcurrentHistogram & IntConcurrentHistogram) allow writing to a single histogram from multiple threads. This comes at a performance penalty of about 4x cost of a write.

The Recorder allows thread safe reading by allowing a consumer to rotate the underlying histogram and reading from a snapshot. This is useful for serializing histograms to disk in real time. The Recorder adds about a 2x cost to a write.

If performance of a write is of a concern, then do ensure that you choose the best Histogram implementation for your purposes. Your platform choice (x86 vs x64), and the JIT you use (Legacy vs RyuJit) can tend to favour certain implementations.Also dedicating a histogram per thread can be a useful way to avoid the extra cost of the concurrent implementations.

To help with choosing the best solution for you, the HistogramFactory is a new feature that offers a fluent syntax to create a Histogram implementation or Recorder instance. e.g. Creating an IntHistogram with default values can now be done with the following code.

var histogram = HistogramFactory
                .With32BitBucketSize()                  //IntHistogram
                .Create();

Alternatively if you wanted thread safe reads and write and wanted to fully define the settings for a LongHistogram you could use the following code.

var recorder = HistogramFactory
                .With64BitBucketSize()                  //LongHistogram
                .WithValuesFrom(1)                      //Default value
                .WithValuesUpTo(TimeStamp.Minutes(10))  //Default value
                .WithPrecisionOf(3)                     //Default value
                .WithThreadSafeWrites()                 //Switches internal imp to concurrent version i.e. LongConcurrentHistogram
                .WithThreadSafeReads()                  //returns a Recorder that wraps the LongConcurrentHistogram
                .Create();