forked from HdrHistogram/hdrhistogram-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_log_writer_test.go
114 lines (96 loc) · 3.73 KB
/
example_log_writer_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
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
}