|
| 1 | +package hdrhistogram_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "fmt" |
| 6 | + hdrhistogram "github.com/HdrHistogram/hdrhistogram-go" |
| 7 | + "io/ioutil" |
| 8 | +) |
| 9 | + |
| 10 | +// The log format encodes into a single file, multiple histograms with optional shared meta data. |
| 11 | +// The following example showcases reading a log file into a slice of histograms |
| 12 | +// nolint |
| 13 | +func ExampleNewHistogramLogReader() { |
| 14 | + dat, _ := ioutil.ReadFile("./test/tagged-Log.logV2.hlog") |
| 15 | + r := bytes.NewReader(dat) |
| 16 | + |
| 17 | + // Create a histogram log reader |
| 18 | + reader := hdrhistogram.NewHistogramLogReader(r) |
| 19 | + var histograms []*hdrhistogram.Histogram = make([]*hdrhistogram.Histogram, 0) |
| 20 | + |
| 21 | + // Read all histograms in the file |
| 22 | + for hist, err := reader.NextIntervalHistogram(); hist != nil && err == nil; hist, err = reader.NextIntervalHistogram() { |
| 23 | + histograms = append(histograms, hist) |
| 24 | + } |
| 25 | + fmt.Printf("Read a total of %d histograms\n", len(histograms)) |
| 26 | + |
| 27 | + min := reader.RangeObservedMin() |
| 28 | + max := reader.RangeObservedMax() |
| 29 | + sigdigits := 3 |
| 30 | + overallHistogram := hdrhistogram.New(min, max, sigdigits) |
| 31 | + |
| 32 | + //// We can then merge all histograms into one and retrieve overall metrics |
| 33 | + for _, hist := range histograms { |
| 34 | + overallHistogram.Merge(hist) |
| 35 | + } |
| 36 | + fmt.Printf("Overall count: %d samples\n", overallHistogram.TotalCount()) |
| 37 | + fmt.Printf("Overall Percentile 50: %d\n", overallHistogram.ValueAtQuantile(50.0)) |
| 38 | + |
| 39 | + // Output: |
| 40 | + // Read a total of 42 histograms |
| 41 | + // Overall count: 32290 samples |
| 42 | + // Overall Percentile 50: 344319 |
| 43 | + |
| 44 | +} |
| 45 | + |
| 46 | +// The log format encodes into a single file, multiple histograms with optional shared meta data. |
| 47 | +// The following example showcases writing multiple histograms into a log file and then |
| 48 | +// processing them again to confirm a proper encode-decode flow |
| 49 | +// nolint |
| 50 | +func ExampleNewHistogramLogWriter() { |
| 51 | + var buff bytes.Buffer |
| 52 | + |
| 53 | + // Create a histogram log writer to write to a bytes.Buffer |
| 54 | + writer := hdrhistogram.NewHistogramLogWriter(&buff) |
| 55 | + |
| 56 | + writer.OutputLogFormatVersion() |
| 57 | + writer.OutputStartTime(0) |
| 58 | + writer.OutputLegend() |
| 59 | + |
| 60 | + // Lets create 3 distinct histograms to exemply the logwriter features |
| 61 | + // each one with a time-frame of 60 secs ( 60000 ms ) |
| 62 | + hist1 := hdrhistogram.New(1, 30000000, 3) |
| 63 | + hist1.SetStartTimeMs(0) |
| 64 | + hist1.SetEndTimeMs(60000) |
| 65 | + for _, sample := range []int64{10, 20, 30, 40} { |
| 66 | + hist1.RecordValue(sample) |
| 67 | + } |
| 68 | + hist2 := hdrhistogram.New(1, 3000, 3) |
| 69 | + hist1.SetStartTimeMs(60001) |
| 70 | + hist1.SetEndTimeMs(120000) |
| 71 | + for _, sample := range []int64{50, 70, 80, 60} { |
| 72 | + hist2.RecordValue(sample) |
| 73 | + } |
| 74 | + hist3 := hdrhistogram.New(1, 30000, 3) |
| 75 | + hist1.SetStartTimeMs(120001) |
| 76 | + hist1.SetEndTimeMs(180000) |
| 77 | + for _, sample := range []int64{90, 100} { |
| 78 | + hist3.RecordValue(sample) |
| 79 | + } |
| 80 | + writer.OutputIntervalHistogram(hist1) |
| 81 | + writer.OutputIntervalHistogram(hist2) |
| 82 | + writer.OutputIntervalHistogram(hist3) |
| 83 | + |
| 84 | + ioutil.WriteFile("example.logV2.hlog", buff.Bytes(), 0644) |
| 85 | + |
| 86 | + // read check |
| 87 | + // Lets read all again and confirm that the total sample count is 10 |
| 88 | + dat, _ := ioutil.ReadFile("example.logV2.hlog") |
| 89 | + r := bytes.NewReader(dat) |
| 90 | + |
| 91 | + // Create a histogram log reader |
| 92 | + reader := hdrhistogram.NewHistogramLogReader(r) |
| 93 | + var histograms []*hdrhistogram.Histogram = make([]*hdrhistogram.Histogram, 0) |
| 94 | + |
| 95 | + // Read all histograms in the file |
| 96 | + for hist, err := reader.NextIntervalHistogram(); hist != nil && err == nil; hist, err = reader.NextIntervalHistogram() { |
| 97 | + histograms = append(histograms, hist) |
| 98 | + } |
| 99 | + fmt.Printf("Read a total of %d histograms\n", len(histograms)) |
| 100 | + |
| 101 | + min := reader.RangeObservedMin() |
| 102 | + max := reader.RangeObservedMax() |
| 103 | + sigdigits := 3 |
| 104 | + overallHistogram := hdrhistogram.New(min, max, sigdigits) |
| 105 | + |
| 106 | + //// We can then merge all histograms into one and retrieve overall metrics |
| 107 | + for _, hist := range histograms { |
| 108 | + overallHistogram.Merge(hist) |
| 109 | + } |
| 110 | + fmt.Printf("Overall count: %d samples\n", overallHistogram.TotalCount()) |
| 111 | + // Output: |
| 112 | + // Read a total of 3 histograms |
| 113 | + // Overall count: 10 samples |
| 114 | +} |
0 commit comments