-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
V2 HdrHistogram Log format (v1.3) (#35)
* [add] V2 HdrHistogram Log format (v1.3) * [fix] Added nolint to the examples * [fix] Fixed error message typo on minimum length on bytes for 7,8,and 9 bytelen * [fix] Fixed ExampleHistogram_PercentilesPrint
- Loading branch information
1 parent
aada4ab
commit 20a94f2
Showing
15 changed files
with
892 additions
and
127 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
package hdrhistogram_test | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
hdrhistogram "github.com/HdrHistogram/hdrhistogram-go" | ||
"io/ioutil" | ||
) | ||
|
||
// The log format encodes into a single file, multiple histograms with optional shared meta data. | ||
// The following example showcases reading a log file into a slice of histograms | ||
// nolint | ||
func ExampleNewHistogramLogReader() { | ||
dat, _ := ioutil.ReadFile("./test/tagged-Log.logV2.hlog") | ||
r := bytes.NewReader(dat) | ||
|
||
// Create a histogram log reader | ||
reader := hdrhistogram.NewHistogramLogReader(r) | ||
var histograms []*hdrhistogram.Histogram = make([]*hdrhistogram.Histogram, 0) | ||
|
||
// Read all histograms in the file | ||
for hist, err := reader.NextIntervalHistogram(); hist != nil && err == nil; hist, err = reader.NextIntervalHistogram() { | ||
histograms = append(histograms, hist) | ||
} | ||
fmt.Printf("Read a total of %d histograms\n", len(histograms)) | ||
|
||
min := reader.RangeObservedMin() | ||
max := reader.RangeObservedMax() | ||
sigdigits := 3 | ||
overallHistogram := hdrhistogram.New(min, max, sigdigits) | ||
|
||
//// We can then merge all histograms into one and retrieve overall metrics | ||
for _, hist := range histograms { | ||
overallHistogram.Merge(hist) | ||
} | ||
fmt.Printf("Overall count: %d samples\n", overallHistogram.TotalCount()) | ||
fmt.Printf("Overall Percentile 50: %d\n", overallHistogram.ValueAtQuantile(50.0)) | ||
|
||
// Output: | ||
// Read a total of 42 histograms | ||
// Overall count: 32290 samples | ||
// Overall Percentile 50: 344319 | ||
|
||
} | ||
|
||
// The log format encodes into a single file, multiple histograms with optional shared meta data. | ||
// The following example showcases writing multiple histograms into a log file and then | ||
// processing them again to confirm a proper encode-decode flow | ||
// nolint | ||
func ExampleNewHistogramLogWriter() { | ||
var buff bytes.Buffer | ||
|
||
// Create a histogram log writer to write to a bytes.Buffer | ||
writer := hdrhistogram.NewHistogramLogWriter(&buff) | ||
|
||
writer.OutputLogFormatVersion() | ||
writer.OutputStartTime(0) | ||
writer.OutputLegend() | ||
|
||
// Lets create 3 distinct histograms to exemply the logwriter features | ||
// each one with a time-frame of 60 secs ( 60000 ms ) | ||
hist1 := hdrhistogram.New(1, 30000000, 3) | ||
hist1.SetStartTimeMs(0) | ||
hist1.SetEndTimeMs(60000) | ||
for _, sample := range []int64{10, 20, 30, 40} { | ||
hist1.RecordValue(sample) | ||
} | ||
hist2 := hdrhistogram.New(1, 3000, 3) | ||
hist1.SetStartTimeMs(60001) | ||
hist1.SetEndTimeMs(120000) | ||
for _, sample := range []int64{50, 70, 80, 60} { | ||
hist2.RecordValue(sample) | ||
} | ||
hist3 := hdrhistogram.New(1, 30000, 3) | ||
hist1.SetStartTimeMs(120001) | ||
hist1.SetEndTimeMs(180000) | ||
for _, sample := range []int64{90, 100} { | ||
hist3.RecordValue(sample) | ||
} | ||
writer.OutputIntervalHistogram(hist1) | ||
writer.OutputIntervalHistogram(hist2) | ||
writer.OutputIntervalHistogram(hist3) | ||
|
||
ioutil.WriteFile("example.logV2.hlog", buff.Bytes(), 0644) | ||
|
||
// read check | ||
// Lets read all again and confirm that the total sample count is 10 | ||
dat, _ := ioutil.ReadFile("example.logV2.hlog") | ||
r := bytes.NewReader(dat) | ||
|
||
// Create a histogram log reader | ||
reader := hdrhistogram.NewHistogramLogReader(r) | ||
var histograms []*hdrhistogram.Histogram = make([]*hdrhistogram.Histogram, 0) | ||
|
||
// Read all histograms in the file | ||
for hist, err := reader.NextIntervalHistogram(); hist != nil && err == nil; hist, err = reader.NextIntervalHistogram() { | ||
histograms = append(histograms, hist) | ||
} | ||
fmt.Printf("Read a total of %d histograms\n", len(histograms)) | ||
|
||
min := reader.RangeObservedMin() | ||
max := reader.RangeObservedMax() | ||
sigdigits := 3 | ||
overallHistogram := hdrhistogram.New(min, max, sigdigits) | ||
|
||
//// We can then merge all histograms into one and retrieve overall metrics | ||
for _, hist := range histograms { | ||
overallHistogram.Merge(hist) | ||
} | ||
fmt.Printf("Overall count: %d samples\n", overallHistogram.TotalCount()) | ||
// Output: | ||
// Read a total of 3 histograms | ||
// Overall count: 10 samples | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.