// Copyright 2023 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. package traceviewer import ( "fmt" "html/template" "math" "strings" "time" ) // TimeHistogram is an high-dynamic-range histogram for durations. type TimeHistogram struct { Count int Buckets []int MinBucket, MaxBucket int } // Five buckets for every power of 10. var logDiv = math.Log(math.Pow(10, 1.0/5)) // Add adds a single sample to the histogram. func (h *TimeHistogram) Add(d time.Duration) { var bucket int if d > 0 { bucket = int(math.Log(float64(d)) / logDiv) } if len(h.Buckets) <= bucket { h.Buckets = append(h.Buckets, make([]int, bucket-len(h.Buckets)+1)...) h.Buckets = h.Buckets[:cap(h.Buckets)] } h.Buckets[bucket]++ if bucket < h.MinBucket || h.MaxBucket == 0 { h.MinBucket = bucket } if bucket > h.MaxBucket { h.MaxBucket = bucket } h.Count++ } // BucketMin returns the minimum duration value for a provided bucket. func (h *TimeHistogram) BucketMin(bucket int) time.Duration { return time.Duration(math.Exp(float64(bucket) * logDiv)) } // ToHTML renders the histogram as HTML. func (h *TimeHistogram) ToHTML(urlmaker func(min, max time.Duration) string) template.HTML { if h == nil || h.Count == 0 { return template.HTML("") } const barWidth = 400 maxCount := 0 for _, count := range h.Buckets { if count > maxCount { maxCount = count } } w := new(strings.Builder) fmt.Fprintf(w, `
%s | `, urlmaker(h.BucketMin(i), h.BucketMin(i+1)), h.BucketMin(i)) } else { fmt.Fprintf(w, `||
%s | `, h.BucketMin(i)) } // Bucket bar. width := h.Buckets[i] * barWidth / maxCount fmt.Fprintf(w, `%d | `, h.Buckets[i])
fmt.Fprintf(w, "|
%s |