func (t *Timings) Time() int64 {
	return t.totalTime.Get()
}
Time returns the total time elapsed for all values.
package stats
import (
	"encoding/json"
	"fmt"
	"strings"
	"sync"
	"time"
	"xbase/sync2"
)
type Timings struct {
	totalCount sync2.AtomicInt64
	totalTime  sync2.AtomicInt64
	mu         sync.RWMutex
	histograms map[string]*Histogram
	hook       func(string, time.Duration)
}
func NewTimings(name string, categories ...string) *Timings {
	t := &Timings{histograms: make(map[string]*Histogram)}
	for _, cat := range categories {
		t.histograms[cat] = NewGenericHistogram("", bucketCutoffs, bucketLabels, "Count", "Time")
	}
	if name != "" {
		publish(name, t)
	}
	return t
}
func (t *Timings) Add(name string, elapsed time.Duration) {
	t.mu.RLock()
	hist, ok := t.histograms[name]
	hook := t.hook
	t.mu.RUnlock()
	if !ok {
		t.mu.Lock()
		hist, ok = t.histograms[name]
		if !ok {
			hist = NewGenericHistogram("", bucketCutoffs, bucketLabels, "Count", "Time")
			t.histograms[name] = hist
		}
		t.mu.Unlock()
	}
	elapsedNs := int64(elapsed)
	hist.Add(elapsedNs)
	t.totalCount.Add(1)
	t.totalTime.Add(elapsedNs)
	if hook != nil {
		hook(name, elapsed)
	}
}
func (t *Timings) Record(name string, startTime time.Time) {
	t.Add(name, time.Since(startTime))
}
func (t *Timings) String() string {
	t.mu.RLock()
	defer t.mu.RUnlock()
	tm := struct {
		TotalCount int64
		TotalTime  int64
		Histograms map[string]*Histogram
	}{
		t.totalCount.Get(),
		t.totalTime.Get(),
		t.histograms,
	}
	data, err := json.Marshal(tm)
	if err != nil {
		data, _ = json.Marshal(err.Error())
	}
	return string(data)
}
func (t *Timings) Histograms() (h map[string]*Histogram) {
	t.mu.RLock()
	defer t.mu.RUnlock()
	h = make(map[string]*Histogram, len(t.histograms))
	for k, v := range t.histograms {
		h[k] = v
	}
	return
}
func (t *Timings) Count() int64 {
	return t.totalCount.Get()
}