mirror of
https://github.com/ceph/ceph-csi.git
synced 2025-06-13 02:33:34 +00:00
vendor files
This commit is contained in:
532
vendor/golang.org/x/net/trace/events.go
generated
vendored
Normal file
532
vendor/golang.org/x/net/trace/events.go
generated
vendored
Normal file
@ -0,0 +1,532 @@
|
||||
// Copyright 2015 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 trace
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"html/template"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"runtime"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"text/tabwriter"
|
||||
"time"
|
||||
)
|
||||
|
||||
const maxEventsPerLog = 100
|
||||
|
||||
type bucket struct {
|
||||
MaxErrAge time.Duration
|
||||
String string
|
||||
}
|
||||
|
||||
var buckets = []bucket{
|
||||
{0, "total"},
|
||||
{10 * time.Second, "errs<10s"},
|
||||
{1 * time.Minute, "errs<1m"},
|
||||
{10 * time.Minute, "errs<10m"},
|
||||
{1 * time.Hour, "errs<1h"},
|
||||
{10 * time.Hour, "errs<10h"},
|
||||
{24000 * time.Hour, "errors"},
|
||||
}
|
||||
|
||||
// RenderEvents renders the HTML page typically served at /debug/events.
|
||||
// It does not do any auth checking. The request may be nil.
|
||||
//
|
||||
// Most users will use the Events handler.
|
||||
func RenderEvents(w http.ResponseWriter, req *http.Request, sensitive bool) {
|
||||
now := time.Now()
|
||||
data := &struct {
|
||||
Families []string // family names
|
||||
Buckets []bucket
|
||||
Counts [][]int // eventLog count per family/bucket
|
||||
|
||||
// Set when a bucket has been selected.
|
||||
Family string
|
||||
Bucket int
|
||||
EventLogs eventLogs
|
||||
Expanded bool
|
||||
}{
|
||||
Buckets: buckets,
|
||||
}
|
||||
|
||||
data.Families = make([]string, 0, len(families))
|
||||
famMu.RLock()
|
||||
for name := range families {
|
||||
data.Families = append(data.Families, name)
|
||||
}
|
||||
famMu.RUnlock()
|
||||
sort.Strings(data.Families)
|
||||
|
||||
// Count the number of eventLogs in each family for each error age.
|
||||
data.Counts = make([][]int, len(data.Families))
|
||||
for i, name := range data.Families {
|
||||
// TODO(sameer): move this loop under the family lock.
|
||||
f := getEventFamily(name)
|
||||
data.Counts[i] = make([]int, len(data.Buckets))
|
||||
for j, b := range data.Buckets {
|
||||
data.Counts[i][j] = f.Count(now, b.MaxErrAge)
|
||||
}
|
||||
}
|
||||
|
||||
if req != nil {
|
||||
var ok bool
|
||||
data.Family, data.Bucket, ok = parseEventsArgs(req)
|
||||
if !ok {
|
||||
// No-op
|
||||
} else {
|
||||
data.EventLogs = getEventFamily(data.Family).Copy(now, buckets[data.Bucket].MaxErrAge)
|
||||
}
|
||||
if data.EventLogs != nil {
|
||||
defer data.EventLogs.Free()
|
||||
sort.Sort(data.EventLogs)
|
||||
}
|
||||
if exp, err := strconv.ParseBool(req.FormValue("exp")); err == nil {
|
||||
data.Expanded = exp
|
||||
}
|
||||
}
|
||||
|
||||
famMu.RLock()
|
||||
defer famMu.RUnlock()
|
||||
if err := eventsTmpl().Execute(w, data); err != nil {
|
||||
log.Printf("net/trace: Failed executing template: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func parseEventsArgs(req *http.Request) (fam string, b int, ok bool) {
|
||||
fam, bStr := req.FormValue("fam"), req.FormValue("b")
|
||||
if fam == "" || bStr == "" {
|
||||
return "", 0, false
|
||||
}
|
||||
b, err := strconv.Atoi(bStr)
|
||||
if err != nil || b < 0 || b >= len(buckets) {
|
||||
return "", 0, false
|
||||
}
|
||||
return fam, b, true
|
||||
}
|
||||
|
||||
// An EventLog provides a log of events associated with a specific object.
|
||||
type EventLog interface {
|
||||
// Printf formats its arguments with fmt.Sprintf and adds the
|
||||
// result to the event log.
|
||||
Printf(format string, a ...interface{})
|
||||
|
||||
// Errorf is like Printf, but it marks this event as an error.
|
||||
Errorf(format string, a ...interface{})
|
||||
|
||||
// Finish declares that this event log is complete.
|
||||
// The event log should not be used after calling this method.
|
||||
Finish()
|
||||
}
|
||||
|
||||
// NewEventLog returns a new EventLog with the specified family name
|
||||
// and title.
|
||||
func NewEventLog(family, title string) EventLog {
|
||||
el := newEventLog()
|
||||
el.ref()
|
||||
el.Family, el.Title = family, title
|
||||
el.Start = time.Now()
|
||||
el.events = make([]logEntry, 0, maxEventsPerLog)
|
||||
el.stack = make([]uintptr, 32)
|
||||
n := runtime.Callers(2, el.stack)
|
||||
el.stack = el.stack[:n]
|
||||
|
||||
getEventFamily(family).add(el)
|
||||
return el
|
||||
}
|
||||
|
||||
func (el *eventLog) Finish() {
|
||||
getEventFamily(el.Family).remove(el)
|
||||
el.unref() // matches ref in New
|
||||
}
|
||||
|
||||
var (
|
||||
famMu sync.RWMutex
|
||||
families = make(map[string]*eventFamily) // family name => family
|
||||
)
|
||||
|
||||
func getEventFamily(fam string) *eventFamily {
|
||||
famMu.Lock()
|
||||
defer famMu.Unlock()
|
||||
f := families[fam]
|
||||
if f == nil {
|
||||
f = &eventFamily{}
|
||||
families[fam] = f
|
||||
}
|
||||
return f
|
||||
}
|
||||
|
||||
type eventFamily struct {
|
||||
mu sync.RWMutex
|
||||
eventLogs eventLogs
|
||||
}
|
||||
|
||||
func (f *eventFamily) add(el *eventLog) {
|
||||
f.mu.Lock()
|
||||
f.eventLogs = append(f.eventLogs, el)
|
||||
f.mu.Unlock()
|
||||
}
|
||||
|
||||
func (f *eventFamily) remove(el *eventLog) {
|
||||
f.mu.Lock()
|
||||
defer f.mu.Unlock()
|
||||
for i, el0 := range f.eventLogs {
|
||||
if el == el0 {
|
||||
copy(f.eventLogs[i:], f.eventLogs[i+1:])
|
||||
f.eventLogs = f.eventLogs[:len(f.eventLogs)-1]
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (f *eventFamily) Count(now time.Time, maxErrAge time.Duration) (n int) {
|
||||
f.mu.RLock()
|
||||
defer f.mu.RUnlock()
|
||||
for _, el := range f.eventLogs {
|
||||
if el.hasRecentError(now, maxErrAge) {
|
||||
n++
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (f *eventFamily) Copy(now time.Time, maxErrAge time.Duration) (els eventLogs) {
|
||||
f.mu.RLock()
|
||||
defer f.mu.RUnlock()
|
||||
els = make(eventLogs, 0, len(f.eventLogs))
|
||||
for _, el := range f.eventLogs {
|
||||
if el.hasRecentError(now, maxErrAge) {
|
||||
el.ref()
|
||||
els = append(els, el)
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
type eventLogs []*eventLog
|
||||
|
||||
// Free calls unref on each element of the list.
|
||||
func (els eventLogs) Free() {
|
||||
for _, el := range els {
|
||||
el.unref()
|
||||
}
|
||||
}
|
||||
|
||||
// eventLogs may be sorted in reverse chronological order.
|
||||
func (els eventLogs) Len() int { return len(els) }
|
||||
func (els eventLogs) Less(i, j int) bool { return els[i].Start.After(els[j].Start) }
|
||||
func (els eventLogs) Swap(i, j int) { els[i], els[j] = els[j], els[i] }
|
||||
|
||||
// A logEntry is a timestamped log entry in an event log.
|
||||
type logEntry struct {
|
||||
When time.Time
|
||||
Elapsed time.Duration // since previous event in log
|
||||
NewDay bool // whether this event is on a different day to the previous event
|
||||
What string
|
||||
IsErr bool
|
||||
}
|
||||
|
||||
// WhenString returns a string representation of the elapsed time of the event.
|
||||
// It will include the date if midnight was crossed.
|
||||
func (e logEntry) WhenString() string {
|
||||
if e.NewDay {
|
||||
return e.When.Format("2006/01/02 15:04:05.000000")
|
||||
}
|
||||
return e.When.Format("15:04:05.000000")
|
||||
}
|
||||
|
||||
// An eventLog represents an active event log.
|
||||
type eventLog struct {
|
||||
// Family is the top-level grouping of event logs to which this belongs.
|
||||
Family string
|
||||
|
||||
// Title is the title of this event log.
|
||||
Title string
|
||||
|
||||
// Timing information.
|
||||
Start time.Time
|
||||
|
||||
// Call stack where this event log was created.
|
||||
stack []uintptr
|
||||
|
||||
// Append-only sequence of events.
|
||||
//
|
||||
// TODO(sameer): change this to a ring buffer to avoid the array copy
|
||||
// when we hit maxEventsPerLog.
|
||||
mu sync.RWMutex
|
||||
events []logEntry
|
||||
LastErrorTime time.Time
|
||||
discarded int
|
||||
|
||||
refs int32 // how many buckets this is in
|
||||
}
|
||||
|
||||
func (el *eventLog) reset() {
|
||||
// Clear all but the mutex. Mutexes may not be copied, even when unlocked.
|
||||
el.Family = ""
|
||||
el.Title = ""
|
||||
el.Start = time.Time{}
|
||||
el.stack = nil
|
||||
el.events = nil
|
||||
el.LastErrorTime = time.Time{}
|
||||
el.discarded = 0
|
||||
el.refs = 0
|
||||
}
|
||||
|
||||
func (el *eventLog) hasRecentError(now time.Time, maxErrAge time.Duration) bool {
|
||||
if maxErrAge == 0 {
|
||||
return true
|
||||
}
|
||||
el.mu.RLock()
|
||||
defer el.mu.RUnlock()
|
||||
return now.Sub(el.LastErrorTime) < maxErrAge
|
||||
}
|
||||
|
||||
// delta returns the elapsed time since the last event or the log start,
|
||||
// and whether it spans midnight.
|
||||
// L >= el.mu
|
||||
func (el *eventLog) delta(t time.Time) (time.Duration, bool) {
|
||||
if len(el.events) == 0 {
|
||||
return t.Sub(el.Start), false
|
||||
}
|
||||
prev := el.events[len(el.events)-1].When
|
||||
return t.Sub(prev), prev.Day() != t.Day()
|
||||
|
||||
}
|
||||
|
||||
func (el *eventLog) Printf(format string, a ...interface{}) {
|
||||
el.printf(false, format, a...)
|
||||
}
|
||||
|
||||
func (el *eventLog) Errorf(format string, a ...interface{}) {
|
||||
el.printf(true, format, a...)
|
||||
}
|
||||
|
||||
func (el *eventLog) printf(isErr bool, format string, a ...interface{}) {
|
||||
e := logEntry{When: time.Now(), IsErr: isErr, What: fmt.Sprintf(format, a...)}
|
||||
el.mu.Lock()
|
||||
e.Elapsed, e.NewDay = el.delta(e.When)
|
||||
if len(el.events) < maxEventsPerLog {
|
||||
el.events = append(el.events, e)
|
||||
} else {
|
||||
// Discard the oldest event.
|
||||
if el.discarded == 0 {
|
||||
// el.discarded starts at two to count for the event it
|
||||
// is replacing, plus the next one that we are about to
|
||||
// drop.
|
||||
el.discarded = 2
|
||||
} else {
|
||||
el.discarded++
|
||||
}
|
||||
// TODO(sameer): if this causes allocations on a critical path,
|
||||
// change eventLog.What to be a fmt.Stringer, as in trace.go.
|
||||
el.events[0].What = fmt.Sprintf("(%d events discarded)", el.discarded)
|
||||
// The timestamp of the discarded meta-event should be
|
||||
// the time of the last event it is representing.
|
||||
el.events[0].When = el.events[1].When
|
||||
copy(el.events[1:], el.events[2:])
|
||||
el.events[maxEventsPerLog-1] = e
|
||||
}
|
||||
if e.IsErr {
|
||||
el.LastErrorTime = e.When
|
||||
}
|
||||
el.mu.Unlock()
|
||||
}
|
||||
|
||||
func (el *eventLog) ref() {
|
||||
atomic.AddInt32(&el.refs, 1)
|
||||
}
|
||||
|
||||
func (el *eventLog) unref() {
|
||||
if atomic.AddInt32(&el.refs, -1) == 0 {
|
||||
freeEventLog(el)
|
||||
}
|
||||
}
|
||||
|
||||
func (el *eventLog) When() string {
|
||||
return el.Start.Format("2006/01/02 15:04:05.000000")
|
||||
}
|
||||
|
||||
func (el *eventLog) ElapsedTime() string {
|
||||
elapsed := time.Since(el.Start)
|
||||
return fmt.Sprintf("%.6f", elapsed.Seconds())
|
||||
}
|
||||
|
||||
func (el *eventLog) Stack() string {
|
||||
buf := new(bytes.Buffer)
|
||||
tw := tabwriter.NewWriter(buf, 1, 8, 1, '\t', 0)
|
||||
printStackRecord(tw, el.stack)
|
||||
tw.Flush()
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
// printStackRecord prints the function + source line information
|
||||
// for a single stack trace.
|
||||
// Adapted from runtime/pprof/pprof.go.
|
||||
func printStackRecord(w io.Writer, stk []uintptr) {
|
||||
for _, pc := range stk {
|
||||
f := runtime.FuncForPC(pc)
|
||||
if f == nil {
|
||||
continue
|
||||
}
|
||||
file, line := f.FileLine(pc)
|
||||
name := f.Name()
|
||||
// Hide runtime.goexit and any runtime functions at the beginning.
|
||||
if strings.HasPrefix(name, "runtime.") {
|
||||
continue
|
||||
}
|
||||
fmt.Fprintf(w, "# %s\t%s:%d\n", name, file, line)
|
||||
}
|
||||
}
|
||||
|
||||
func (el *eventLog) Events() []logEntry {
|
||||
el.mu.RLock()
|
||||
defer el.mu.RUnlock()
|
||||
return el.events
|
||||
}
|
||||
|
||||
// freeEventLogs is a freelist of *eventLog
|
||||
var freeEventLogs = make(chan *eventLog, 1000)
|
||||
|
||||
// newEventLog returns a event log ready to use.
|
||||
func newEventLog() *eventLog {
|
||||
select {
|
||||
case el := <-freeEventLogs:
|
||||
return el
|
||||
default:
|
||||
return new(eventLog)
|
||||
}
|
||||
}
|
||||
|
||||
// freeEventLog adds el to freeEventLogs if there's room.
|
||||
// This is non-blocking.
|
||||
func freeEventLog(el *eventLog) {
|
||||
el.reset()
|
||||
select {
|
||||
case freeEventLogs <- el:
|
||||
default:
|
||||
}
|
||||
}
|
||||
|
||||
var eventsTmplCache *template.Template
|
||||
var eventsTmplOnce sync.Once
|
||||
|
||||
func eventsTmpl() *template.Template {
|
||||
eventsTmplOnce.Do(func() {
|
||||
eventsTmplCache = template.Must(template.New("events").Funcs(template.FuncMap{
|
||||
"elapsed": elapsed,
|
||||
"trimSpace": strings.TrimSpace,
|
||||
}).Parse(eventsHTML))
|
||||
})
|
||||
return eventsTmplCache
|
||||
}
|
||||
|
||||
const eventsHTML = `
|
||||
<html>
|
||||
<head>
|
||||
<title>events</title>
|
||||
</head>
|
||||
<style type="text/css">
|
||||
body {
|
||||
font-family: sans-serif;
|
||||
}
|
||||
table#req-status td.family {
|
||||
padding-right: 2em;
|
||||
}
|
||||
table#req-status td.active {
|
||||
padding-right: 1em;
|
||||
}
|
||||
table#req-status td.empty {
|
||||
color: #aaa;
|
||||
}
|
||||
table#reqs {
|
||||
margin-top: 1em;
|
||||
}
|
||||
table#reqs tr.first {
|
||||
{{if $.Expanded}}font-weight: bold;{{end}}
|
||||
}
|
||||
table#reqs td {
|
||||
font-family: monospace;
|
||||
}
|
||||
table#reqs td.when {
|
||||
text-align: right;
|
||||
white-space: nowrap;
|
||||
}
|
||||
table#reqs td.elapsed {
|
||||
padding: 0 0.5em;
|
||||
text-align: right;
|
||||
white-space: pre;
|
||||
width: 10em;
|
||||
}
|
||||
address {
|
||||
font-size: smaller;
|
||||
margin-top: 5em;
|
||||
}
|
||||
</style>
|
||||
<body>
|
||||
|
||||
<h1>/debug/events</h1>
|
||||
|
||||
<table id="req-status">
|
||||
{{range $i, $fam := .Families}}
|
||||
<tr>
|
||||
<td class="family">{{$fam}}</td>
|
||||
|
||||
{{range $j, $bucket := $.Buckets}}
|
||||
{{$n := index $.Counts $i $j}}
|
||||
<td class="{{if not $bucket.MaxErrAge}}active{{end}}{{if not $n}}empty{{end}}">
|
||||
{{if $n}}<a href="?fam={{$fam}}&b={{$j}}{{if $.Expanded}}&exp=1{{end}}">{{end}}
|
||||
[{{$n}} {{$bucket.String}}]
|
||||
{{if $n}}</a>{{end}}
|
||||
</td>
|
||||
{{end}}
|
||||
|
||||
</tr>{{end}}
|
||||
</table>
|
||||
|
||||
{{if $.EventLogs}}
|
||||
<hr />
|
||||
<h3>Family: {{$.Family}}</h3>
|
||||
|
||||
{{if $.Expanded}}<a href="?fam={{$.Family}}&b={{$.Bucket}}">{{end}}
|
||||
[Summary]{{if $.Expanded}}</a>{{end}}
|
||||
|
||||
{{if not $.Expanded}}<a href="?fam={{$.Family}}&b={{$.Bucket}}&exp=1">{{end}}
|
||||
[Expanded]{{if not $.Expanded}}</a>{{end}}
|
||||
|
||||
<table id="reqs">
|
||||
<tr><th>When</th><th>Elapsed</th></tr>
|
||||
{{range $el := $.EventLogs}}
|
||||
<tr class="first">
|
||||
<td class="when">{{$el.When}}</td>
|
||||
<td class="elapsed">{{$el.ElapsedTime}}</td>
|
||||
<td>{{$el.Title}}
|
||||
</tr>
|
||||
{{if $.Expanded}}
|
||||
<tr>
|
||||
<td class="when"></td>
|
||||
<td class="elapsed"></td>
|
||||
<td><pre>{{$el.Stack|trimSpace}}</pre></td>
|
||||
</tr>
|
||||
{{range $el.Events}}
|
||||
<tr>
|
||||
<td class="when">{{.WhenString}}</td>
|
||||
<td class="elapsed">{{elapsed .Elapsed}}</td>
|
||||
<td>.{{if .IsErr}}E{{else}}.{{end}}. {{.What}}</td>
|
||||
</tr>
|
||||
{{end}}
|
||||
{{end}}
|
||||
{{end}}
|
||||
</table>
|
||||
{{end}}
|
||||
</body>
|
||||
</html>
|
||||
`
|
365
vendor/golang.org/x/net/trace/histogram.go
generated
vendored
Normal file
365
vendor/golang.org/x/net/trace/histogram.go
generated
vendored
Normal file
@ -0,0 +1,365 @@
|
||||
// Copyright 2015 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 trace
|
||||
|
||||
// This file implements histogramming for RPC statistics collection.
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"html/template"
|
||||
"log"
|
||||
"math"
|
||||
"sync"
|
||||
|
||||
"golang.org/x/net/internal/timeseries"
|
||||
)
|
||||
|
||||
const (
|
||||
bucketCount = 38
|
||||
)
|
||||
|
||||
// histogram keeps counts of values in buckets that are spaced
|
||||
// out in powers of 2: 0-1, 2-3, 4-7...
|
||||
// histogram implements timeseries.Observable
|
||||
type histogram struct {
|
||||
sum int64 // running total of measurements
|
||||
sumOfSquares float64 // square of running total
|
||||
buckets []int64 // bucketed values for histogram
|
||||
value int // holds a single value as an optimization
|
||||
valueCount int64 // number of values recorded for single value
|
||||
}
|
||||
|
||||
// AddMeasurement records a value measurement observation to the histogram.
|
||||
func (h *histogram) addMeasurement(value int64) {
|
||||
// TODO: assert invariant
|
||||
h.sum += value
|
||||
h.sumOfSquares += float64(value) * float64(value)
|
||||
|
||||
bucketIndex := getBucket(value)
|
||||
|
||||
if h.valueCount == 0 || (h.valueCount > 0 && h.value == bucketIndex) {
|
||||
h.value = bucketIndex
|
||||
h.valueCount++
|
||||
} else {
|
||||
h.allocateBuckets()
|
||||
h.buckets[bucketIndex]++
|
||||
}
|
||||
}
|
||||
|
||||
func (h *histogram) allocateBuckets() {
|
||||
if h.buckets == nil {
|
||||
h.buckets = make([]int64, bucketCount)
|
||||
h.buckets[h.value] = h.valueCount
|
||||
h.value = 0
|
||||
h.valueCount = -1
|
||||
}
|
||||
}
|
||||
|
||||
func log2(i int64) int {
|
||||
n := 0
|
||||
for ; i >= 0x100; i >>= 8 {
|
||||
n += 8
|
||||
}
|
||||
for ; i > 0; i >>= 1 {
|
||||
n += 1
|
||||
}
|
||||
return n
|
||||
}
|
||||
|
||||
func getBucket(i int64) (index int) {
|
||||
index = log2(i) - 1
|
||||
if index < 0 {
|
||||
index = 0
|
||||
}
|
||||
if index >= bucketCount {
|
||||
index = bucketCount - 1
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// Total returns the number of recorded observations.
|
||||
func (h *histogram) total() (total int64) {
|
||||
if h.valueCount >= 0 {
|
||||
total = h.valueCount
|
||||
}
|
||||
for _, val := range h.buckets {
|
||||
total += int64(val)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// Average returns the average value of recorded observations.
|
||||
func (h *histogram) average() float64 {
|
||||
t := h.total()
|
||||
if t == 0 {
|
||||
return 0
|
||||
}
|
||||
return float64(h.sum) / float64(t)
|
||||
}
|
||||
|
||||
// Variance returns the variance of recorded observations.
|
||||
func (h *histogram) variance() float64 {
|
||||
t := float64(h.total())
|
||||
if t == 0 {
|
||||
return 0
|
||||
}
|
||||
s := float64(h.sum) / t
|
||||
return h.sumOfSquares/t - s*s
|
||||
}
|
||||
|
||||
// StandardDeviation returns the standard deviation of recorded observations.
|
||||
func (h *histogram) standardDeviation() float64 {
|
||||
return math.Sqrt(h.variance())
|
||||
}
|
||||
|
||||
// PercentileBoundary estimates the value that the given fraction of recorded
|
||||
// observations are less than.
|
||||
func (h *histogram) percentileBoundary(percentile float64) int64 {
|
||||
total := h.total()
|
||||
|
||||
// Corner cases (make sure result is strictly less than Total())
|
||||
if total == 0 {
|
||||
return 0
|
||||
} else if total == 1 {
|
||||
return int64(h.average())
|
||||
}
|
||||
|
||||
percentOfTotal := round(float64(total) * percentile)
|
||||
var runningTotal int64
|
||||
|
||||
for i := range h.buckets {
|
||||
value := h.buckets[i]
|
||||
runningTotal += value
|
||||
if runningTotal == percentOfTotal {
|
||||
// We hit an exact bucket boundary. If the next bucket has data, it is a
|
||||
// good estimate of the value. If the bucket is empty, we interpolate the
|
||||
// midpoint between the next bucket's boundary and the next non-zero
|
||||
// bucket. If the remaining buckets are all empty, then we use the
|
||||
// boundary for the next bucket as the estimate.
|
||||
j := uint8(i + 1)
|
||||
min := bucketBoundary(j)
|
||||
if runningTotal < total {
|
||||
for h.buckets[j] == 0 {
|
||||
j++
|
||||
}
|
||||
}
|
||||
max := bucketBoundary(j)
|
||||
return min + round(float64(max-min)/2)
|
||||
} else if runningTotal > percentOfTotal {
|
||||
// The value is in this bucket. Interpolate the value.
|
||||
delta := runningTotal - percentOfTotal
|
||||
percentBucket := float64(value-delta) / float64(value)
|
||||
bucketMin := bucketBoundary(uint8(i))
|
||||
nextBucketMin := bucketBoundary(uint8(i + 1))
|
||||
bucketSize := nextBucketMin - bucketMin
|
||||
return bucketMin + round(percentBucket*float64(bucketSize))
|
||||
}
|
||||
}
|
||||
return bucketBoundary(bucketCount - 1)
|
||||
}
|
||||
|
||||
// Median returns the estimated median of the observed values.
|
||||
func (h *histogram) median() int64 {
|
||||
return h.percentileBoundary(0.5)
|
||||
}
|
||||
|
||||
// Add adds other to h.
|
||||
func (h *histogram) Add(other timeseries.Observable) {
|
||||
o := other.(*histogram)
|
||||
if o.valueCount == 0 {
|
||||
// Other histogram is empty
|
||||
} else if h.valueCount >= 0 && o.valueCount > 0 && h.value == o.value {
|
||||
// Both have a single bucketed value, aggregate them
|
||||
h.valueCount += o.valueCount
|
||||
} else {
|
||||
// Two different values necessitate buckets in this histogram
|
||||
h.allocateBuckets()
|
||||
if o.valueCount >= 0 {
|
||||
h.buckets[o.value] += o.valueCount
|
||||
} else {
|
||||
for i := range h.buckets {
|
||||
h.buckets[i] += o.buckets[i]
|
||||
}
|
||||
}
|
||||
}
|
||||
h.sumOfSquares += o.sumOfSquares
|
||||
h.sum += o.sum
|
||||
}
|
||||
|
||||
// Clear resets the histogram to an empty state, removing all observed values.
|
||||
func (h *histogram) Clear() {
|
||||
h.buckets = nil
|
||||
h.value = 0
|
||||
h.valueCount = 0
|
||||
h.sum = 0
|
||||
h.sumOfSquares = 0
|
||||
}
|
||||
|
||||
// CopyFrom copies from other, which must be a *histogram, into h.
|
||||
func (h *histogram) CopyFrom(other timeseries.Observable) {
|
||||
o := other.(*histogram)
|
||||
if o.valueCount == -1 {
|
||||
h.allocateBuckets()
|
||||
copy(h.buckets, o.buckets)
|
||||
}
|
||||
h.sum = o.sum
|
||||
h.sumOfSquares = o.sumOfSquares
|
||||
h.value = o.value
|
||||
h.valueCount = o.valueCount
|
||||
}
|
||||
|
||||
// Multiply scales the histogram by the specified ratio.
|
||||
func (h *histogram) Multiply(ratio float64) {
|
||||
if h.valueCount == -1 {
|
||||
for i := range h.buckets {
|
||||
h.buckets[i] = int64(float64(h.buckets[i]) * ratio)
|
||||
}
|
||||
} else {
|
||||
h.valueCount = int64(float64(h.valueCount) * ratio)
|
||||
}
|
||||
h.sum = int64(float64(h.sum) * ratio)
|
||||
h.sumOfSquares = h.sumOfSquares * ratio
|
||||
}
|
||||
|
||||
// New creates a new histogram.
|
||||
func (h *histogram) New() timeseries.Observable {
|
||||
r := new(histogram)
|
||||
r.Clear()
|
||||
return r
|
||||
}
|
||||
|
||||
func (h *histogram) String() string {
|
||||
return fmt.Sprintf("%d, %f, %d, %d, %v",
|
||||
h.sum, h.sumOfSquares, h.value, h.valueCount, h.buckets)
|
||||
}
|
||||
|
||||
// round returns the closest int64 to the argument
|
||||
func round(in float64) int64 {
|
||||
return int64(math.Floor(in + 0.5))
|
||||
}
|
||||
|
||||
// bucketBoundary returns the first value in the bucket.
|
||||
func bucketBoundary(bucket uint8) int64 {
|
||||
if bucket == 0 {
|
||||
return 0
|
||||
}
|
||||
return 1 << bucket
|
||||
}
|
||||
|
||||
// bucketData holds data about a specific bucket for use in distTmpl.
|
||||
type bucketData struct {
|
||||
Lower, Upper int64
|
||||
N int64
|
||||
Pct, CumulativePct float64
|
||||
GraphWidth int
|
||||
}
|
||||
|
||||
// data holds data about a Distribution for use in distTmpl.
|
||||
type data struct {
|
||||
Buckets []*bucketData
|
||||
Count, Median int64
|
||||
Mean, StandardDeviation float64
|
||||
}
|
||||
|
||||
// maxHTMLBarWidth is the maximum width of the HTML bar for visualizing buckets.
|
||||
const maxHTMLBarWidth = 350.0
|
||||
|
||||
// newData returns data representing h for use in distTmpl.
|
||||
func (h *histogram) newData() *data {
|
||||
// Force the allocation of buckets to simplify the rendering implementation
|
||||
h.allocateBuckets()
|
||||
// We scale the bars on the right so that the largest bar is
|
||||
// maxHTMLBarWidth pixels in width.
|
||||
maxBucket := int64(0)
|
||||
for _, n := range h.buckets {
|
||||
if n > maxBucket {
|
||||
maxBucket = n
|
||||
}
|
||||
}
|
||||
total := h.total()
|
||||
barsizeMult := maxHTMLBarWidth / float64(maxBucket)
|
||||
var pctMult float64
|
||||
if total == 0 {
|
||||
pctMult = 1.0
|
||||
} else {
|
||||
pctMult = 100.0 / float64(total)
|
||||
}
|
||||
|
||||
buckets := make([]*bucketData, len(h.buckets))
|
||||
runningTotal := int64(0)
|
||||
for i, n := range h.buckets {
|
||||
if n == 0 {
|
||||
continue
|
||||
}
|
||||
runningTotal += n
|
||||
var upperBound int64
|
||||
if i < bucketCount-1 {
|
||||
upperBound = bucketBoundary(uint8(i + 1))
|
||||
} else {
|
||||
upperBound = math.MaxInt64
|
||||
}
|
||||
buckets[i] = &bucketData{
|
||||
Lower: bucketBoundary(uint8(i)),
|
||||
Upper: upperBound,
|
||||
N: n,
|
||||
Pct: float64(n) * pctMult,
|
||||
CumulativePct: float64(runningTotal) * pctMult,
|
||||
GraphWidth: int(float64(n) * barsizeMult),
|
||||
}
|
||||
}
|
||||
return &data{
|
||||
Buckets: buckets,
|
||||
Count: total,
|
||||
Median: h.median(),
|
||||
Mean: h.average(),
|
||||
StandardDeviation: h.standardDeviation(),
|
||||
}
|
||||
}
|
||||
|
||||
func (h *histogram) html() template.HTML {
|
||||
buf := new(bytes.Buffer)
|
||||
if err := distTmpl().Execute(buf, h.newData()); err != nil {
|
||||
buf.Reset()
|
||||
log.Printf("net/trace: couldn't execute template: %v", err)
|
||||
}
|
||||
return template.HTML(buf.String())
|
||||
}
|
||||
|
||||
var distTmplCache *template.Template
|
||||
var distTmplOnce sync.Once
|
||||
|
||||
func distTmpl() *template.Template {
|
||||
distTmplOnce.Do(func() {
|
||||
// Input: data
|
||||
distTmplCache = template.Must(template.New("distTmpl").Parse(`
|
||||
<table>
|
||||
<tr>
|
||||
<td style="padding:0.25em">Count: {{.Count}}</td>
|
||||
<td style="padding:0.25em">Mean: {{printf "%.0f" .Mean}}</td>
|
||||
<td style="padding:0.25em">StdDev: {{printf "%.0f" .StandardDeviation}}</td>
|
||||
<td style="padding:0.25em">Median: {{.Median}}</td>
|
||||
</tr>
|
||||
</table>
|
||||
<hr>
|
||||
<table>
|
||||
{{range $b := .Buckets}}
|
||||
{{if $b}}
|
||||
<tr>
|
||||
<td style="padding:0 0 0 0.25em">[</td>
|
||||
<td style="text-align:right;padding:0 0.25em">{{.Lower}},</td>
|
||||
<td style="text-align:right;padding:0 0.25em">{{.Upper}})</td>
|
||||
<td style="text-align:right;padding:0 0.25em">{{.N}}</td>
|
||||
<td style="text-align:right;padding:0 0.25em">{{printf "%#.3f" .Pct}}%</td>
|
||||
<td style="text-align:right;padding:0 0.25em">{{printf "%#.3f" .CumulativePct}}%</td>
|
||||
<td><div style="background-color: blue; height: 1em; width: {{.GraphWidth}};"></div></td>
|
||||
</tr>
|
||||
{{end}}
|
||||
{{end}}
|
||||
</table>
|
||||
`))
|
||||
})
|
||||
return distTmplCache
|
||||
}
|
325
vendor/golang.org/x/net/trace/histogram_test.go
generated
vendored
Normal file
325
vendor/golang.org/x/net/trace/histogram_test.go
generated
vendored
Normal file
@ -0,0 +1,325 @@
|
||||
// Copyright 2015 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 trace
|
||||
|
||||
import (
|
||||
"math"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type sumTest struct {
|
||||
value int64
|
||||
sum int64
|
||||
sumOfSquares float64
|
||||
total int64
|
||||
}
|
||||
|
||||
var sumTests = []sumTest{
|
||||
{100, 100, 10000, 1},
|
||||
{50, 150, 12500, 2},
|
||||
{50, 200, 15000, 3},
|
||||
{50, 250, 17500, 4},
|
||||
}
|
||||
|
||||
type bucketingTest struct {
|
||||
in int64
|
||||
log int
|
||||
bucket int
|
||||
}
|
||||
|
||||
var bucketingTests = []bucketingTest{
|
||||
{0, 0, 0},
|
||||
{1, 1, 0},
|
||||
{2, 2, 1},
|
||||
{3, 2, 1},
|
||||
{4, 3, 2},
|
||||
{1000, 10, 9},
|
||||
{1023, 10, 9},
|
||||
{1024, 11, 10},
|
||||
{1000000, 20, 19},
|
||||
}
|
||||
|
||||
type multiplyTest struct {
|
||||
in int64
|
||||
ratio float64
|
||||
expectedSum int64
|
||||
expectedTotal int64
|
||||
expectedSumOfSquares float64
|
||||
}
|
||||
|
||||
var multiplyTests = []multiplyTest{
|
||||
{15, 2.5, 37, 2, 562.5},
|
||||
{128, 4.6, 758, 13, 77953.9},
|
||||
}
|
||||
|
||||
type percentileTest struct {
|
||||
fraction float64
|
||||
expected int64
|
||||
}
|
||||
|
||||
var percentileTests = []percentileTest{
|
||||
{0.25, 48},
|
||||
{0.5, 96},
|
||||
{0.6, 109},
|
||||
{0.75, 128},
|
||||
{0.90, 205},
|
||||
{0.95, 230},
|
||||
{0.99, 256},
|
||||
}
|
||||
|
||||
func TestSum(t *testing.T) {
|
||||
var h histogram
|
||||
|
||||
for _, test := range sumTests {
|
||||
h.addMeasurement(test.value)
|
||||
sum := h.sum
|
||||
if sum != test.sum {
|
||||
t.Errorf("h.Sum = %v WANT: %v", sum, test.sum)
|
||||
}
|
||||
|
||||
sumOfSquares := h.sumOfSquares
|
||||
if sumOfSquares != test.sumOfSquares {
|
||||
t.Errorf("h.SumOfSquares = %v WANT: %v", sumOfSquares, test.sumOfSquares)
|
||||
}
|
||||
|
||||
total := h.total()
|
||||
if total != test.total {
|
||||
t.Errorf("h.Total = %v WANT: %v", total, test.total)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestMultiply(t *testing.T) {
|
||||
var h histogram
|
||||
for i, test := range multiplyTests {
|
||||
h.addMeasurement(test.in)
|
||||
h.Multiply(test.ratio)
|
||||
if h.sum != test.expectedSum {
|
||||
t.Errorf("#%v: h.sum = %v WANT: %v", i, h.sum, test.expectedSum)
|
||||
}
|
||||
if h.total() != test.expectedTotal {
|
||||
t.Errorf("#%v: h.total = %v WANT: %v", i, h.total(), test.expectedTotal)
|
||||
}
|
||||
if h.sumOfSquares != test.expectedSumOfSquares {
|
||||
t.Errorf("#%v: h.SumOfSquares = %v WANT: %v", i, test.expectedSumOfSquares, h.sumOfSquares)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestBucketingFunctions(t *testing.T) {
|
||||
for _, test := range bucketingTests {
|
||||
log := log2(test.in)
|
||||
if log != test.log {
|
||||
t.Errorf("log2 = %v WANT: %v", log, test.log)
|
||||
}
|
||||
|
||||
bucket := getBucket(test.in)
|
||||
if bucket != test.bucket {
|
||||
t.Errorf("getBucket = %v WANT: %v", bucket, test.bucket)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestAverage(t *testing.T) {
|
||||
a := new(histogram)
|
||||
average := a.average()
|
||||
if average != 0 {
|
||||
t.Errorf("Average of empty histogram was %v WANT: 0", average)
|
||||
}
|
||||
|
||||
a.addMeasurement(1)
|
||||
a.addMeasurement(1)
|
||||
a.addMeasurement(3)
|
||||
const expected = float64(5) / float64(3)
|
||||
average = a.average()
|
||||
|
||||
if !isApproximate(average, expected) {
|
||||
t.Errorf("Average = %g WANT: %v", average, expected)
|
||||
}
|
||||
}
|
||||
|
||||
func TestStandardDeviation(t *testing.T) {
|
||||
a := new(histogram)
|
||||
add(a, 10, 1<<4)
|
||||
add(a, 10, 1<<5)
|
||||
add(a, 10, 1<<6)
|
||||
stdDev := a.standardDeviation()
|
||||
const expected = 19.95
|
||||
|
||||
if !isApproximate(stdDev, expected) {
|
||||
t.Errorf("StandardDeviation = %v WANT: %v", stdDev, expected)
|
||||
}
|
||||
|
||||
// No values
|
||||
a = new(histogram)
|
||||
stdDev = a.standardDeviation()
|
||||
|
||||
if !isApproximate(stdDev, 0) {
|
||||
t.Errorf("StandardDeviation = %v WANT: 0", stdDev)
|
||||
}
|
||||
|
||||
add(a, 1, 1<<4)
|
||||
if !isApproximate(stdDev, 0) {
|
||||
t.Errorf("StandardDeviation = %v WANT: 0", stdDev)
|
||||
}
|
||||
|
||||
add(a, 10, 1<<4)
|
||||
if !isApproximate(stdDev, 0) {
|
||||
t.Errorf("StandardDeviation = %v WANT: 0", stdDev)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPercentileBoundary(t *testing.T) {
|
||||
a := new(histogram)
|
||||
add(a, 5, 1<<4)
|
||||
add(a, 10, 1<<6)
|
||||
add(a, 5, 1<<7)
|
||||
|
||||
for _, test := range percentileTests {
|
||||
percentile := a.percentileBoundary(test.fraction)
|
||||
if percentile != test.expected {
|
||||
t.Errorf("h.PercentileBoundary (fraction=%v) = %v WANT: %v", test.fraction, percentile, test.expected)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestCopyFrom(t *testing.T) {
|
||||
a := histogram{5, 25, []int64{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}, 4, -1}
|
||||
b := histogram{6, 36, []int64{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}, 5, -1}
|
||||
|
||||
a.CopyFrom(&b)
|
||||
|
||||
if a.String() != b.String() {
|
||||
t.Errorf("a.String = %s WANT: %s", a.String(), b.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestClear(t *testing.T) {
|
||||
a := histogram{5, 25, []int64{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}, 4, -1}
|
||||
|
||||
a.Clear()
|
||||
|
||||
expected := "0, 0.000000, 0, 0, []"
|
||||
if a.String() != expected {
|
||||
t.Errorf("a.String = %s WANT %s", a.String(), expected)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNew(t *testing.T) {
|
||||
a := histogram{5, 25, []int64{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}, 4, -1}
|
||||
b := a.New()
|
||||
|
||||
expected := "0, 0.000000, 0, 0, []"
|
||||
if b.(*histogram).String() != expected {
|
||||
t.Errorf("b.(*histogram).String = %s WANT: %s", b.(*histogram).String(), expected)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAdd(t *testing.T) {
|
||||
// The tests here depend on the associativity of addMeasurement and Add.
|
||||
// Add empty observation
|
||||
a := histogram{5, 25, []int64{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}, 4, -1}
|
||||
b := a.New()
|
||||
|
||||
expected := a.String()
|
||||
a.Add(b)
|
||||
if a.String() != expected {
|
||||
t.Errorf("a.String = %s WANT: %s", a.String(), expected)
|
||||
}
|
||||
|
||||
// Add same bucketed value, no new buckets
|
||||
c := new(histogram)
|
||||
d := new(histogram)
|
||||
e := new(histogram)
|
||||
c.addMeasurement(12)
|
||||
d.addMeasurement(11)
|
||||
e.addMeasurement(12)
|
||||
e.addMeasurement(11)
|
||||
c.Add(d)
|
||||
if c.String() != e.String() {
|
||||
t.Errorf("c.String = %s WANT: %s", c.String(), e.String())
|
||||
}
|
||||
|
||||
// Add bucketed values
|
||||
f := new(histogram)
|
||||
g := new(histogram)
|
||||
h := new(histogram)
|
||||
f.addMeasurement(4)
|
||||
f.addMeasurement(12)
|
||||
f.addMeasurement(100)
|
||||
g.addMeasurement(18)
|
||||
g.addMeasurement(36)
|
||||
g.addMeasurement(255)
|
||||
h.addMeasurement(4)
|
||||
h.addMeasurement(12)
|
||||
h.addMeasurement(100)
|
||||
h.addMeasurement(18)
|
||||
h.addMeasurement(36)
|
||||
h.addMeasurement(255)
|
||||
f.Add(g)
|
||||
if f.String() != h.String() {
|
||||
t.Errorf("f.String = %q WANT: %q", f.String(), h.String())
|
||||
}
|
||||
|
||||
// add buckets to no buckets
|
||||
i := new(histogram)
|
||||
j := new(histogram)
|
||||
k := new(histogram)
|
||||
j.addMeasurement(18)
|
||||
j.addMeasurement(36)
|
||||
j.addMeasurement(255)
|
||||
k.addMeasurement(18)
|
||||
k.addMeasurement(36)
|
||||
k.addMeasurement(255)
|
||||
i.Add(j)
|
||||
if i.String() != k.String() {
|
||||
t.Errorf("i.String = %q WANT: %q", i.String(), k.String())
|
||||
}
|
||||
|
||||
// add buckets to single value (no overlap)
|
||||
l := new(histogram)
|
||||
m := new(histogram)
|
||||
n := new(histogram)
|
||||
l.addMeasurement(0)
|
||||
m.addMeasurement(18)
|
||||
m.addMeasurement(36)
|
||||
m.addMeasurement(255)
|
||||
n.addMeasurement(0)
|
||||
n.addMeasurement(18)
|
||||
n.addMeasurement(36)
|
||||
n.addMeasurement(255)
|
||||
l.Add(m)
|
||||
if l.String() != n.String() {
|
||||
t.Errorf("l.String = %q WANT: %q", l.String(), n.String())
|
||||
}
|
||||
|
||||
// mixed order
|
||||
o := new(histogram)
|
||||
p := new(histogram)
|
||||
o.addMeasurement(0)
|
||||
o.addMeasurement(2)
|
||||
o.addMeasurement(0)
|
||||
p.addMeasurement(0)
|
||||
p.addMeasurement(0)
|
||||
p.addMeasurement(2)
|
||||
if o.String() != p.String() {
|
||||
t.Errorf("o.String = %q WANT: %q", o.String(), p.String())
|
||||
}
|
||||
}
|
||||
|
||||
func add(h *histogram, times int, val int64) {
|
||||
for i := 0; i < times; i++ {
|
||||
h.addMeasurement(val)
|
||||
}
|
||||
}
|
||||
|
||||
func isApproximate(x, y float64) bool {
|
||||
return math.Abs(x-y) < 1e-2
|
||||
}
|
1082
vendor/golang.org/x/net/trace/trace.go
generated
vendored
Normal file
1082
vendor/golang.org/x/net/trace/trace.go
generated
vendored
Normal file
File diff suppressed because it is too large
Load Diff
21
vendor/golang.org/x/net/trace/trace_go16.go
generated
vendored
Normal file
21
vendor/golang.org/x/net/trace/trace_go16.go
generated
vendored
Normal file
@ -0,0 +1,21 @@
|
||||
// Copyright 2017 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.
|
||||
|
||||
// +build !go1.7
|
||||
|
||||
package trace
|
||||
|
||||
import "golang.org/x/net/context"
|
||||
|
||||
// NewContext returns a copy of the parent context
|
||||
// and associates it with a Trace.
|
||||
func NewContext(ctx context.Context, tr Trace) context.Context {
|
||||
return context.WithValue(ctx, contextKey, tr)
|
||||
}
|
||||
|
||||
// FromContext returns the Trace bound to the context, if any.
|
||||
func FromContext(ctx context.Context) (tr Trace, ok bool) {
|
||||
tr, ok = ctx.Value(contextKey).(Trace)
|
||||
return
|
||||
}
|
21
vendor/golang.org/x/net/trace/trace_go17.go
generated
vendored
Normal file
21
vendor/golang.org/x/net/trace/trace_go17.go
generated
vendored
Normal file
@ -0,0 +1,21 @@
|
||||
// Copyright 2017 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.
|
||||
|
||||
// +build go1.7
|
||||
|
||||
package trace
|
||||
|
||||
import "context"
|
||||
|
||||
// NewContext returns a copy of the parent context
|
||||
// and associates it with a Trace.
|
||||
func NewContext(ctx context.Context, tr Trace) context.Context {
|
||||
return context.WithValue(ctx, contextKey, tr)
|
||||
}
|
||||
|
||||
// FromContext returns the Trace bound to the context, if any.
|
||||
func FromContext(ctx context.Context) (tr Trace, ok bool) {
|
||||
tr, ok = ctx.Value(contextKey).(Trace)
|
||||
return
|
||||
}
|
178
vendor/golang.org/x/net/trace/trace_test.go
generated
vendored
Normal file
178
vendor/golang.org/x/net/trace/trace_test.go
generated
vendored
Normal file
@ -0,0 +1,178 @@
|
||||
// Copyright 2015 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 trace
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type s struct{}
|
||||
|
||||
func (s) String() string { return "lazy string" }
|
||||
|
||||
// TestReset checks whether all the fields are zeroed after reset.
|
||||
func TestReset(t *testing.T) {
|
||||
tr := New("foo", "bar")
|
||||
tr.LazyLog(s{}, false)
|
||||
tr.LazyPrintf("%d", 1)
|
||||
tr.SetRecycler(func(_ interface{}) {})
|
||||
tr.SetTraceInfo(3, 4)
|
||||
tr.SetMaxEvents(100)
|
||||
tr.SetError()
|
||||
tr.Finish()
|
||||
|
||||
tr.(*trace).reset()
|
||||
|
||||
if !reflect.DeepEqual(tr, new(trace)) {
|
||||
t.Errorf("reset didn't clear all fields: %+v", tr)
|
||||
}
|
||||
}
|
||||
|
||||
// TestResetLog checks whether all the fields are zeroed after reset.
|
||||
func TestResetLog(t *testing.T) {
|
||||
el := NewEventLog("foo", "bar")
|
||||
el.Printf("message")
|
||||
el.Errorf("error")
|
||||
el.Finish()
|
||||
|
||||
el.(*eventLog).reset()
|
||||
|
||||
if !reflect.DeepEqual(el, new(eventLog)) {
|
||||
t.Errorf("reset didn't clear all fields: %+v", el)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAuthRequest(t *testing.T) {
|
||||
testCases := []struct {
|
||||
host string
|
||||
want bool
|
||||
}{
|
||||
{host: "192.168.23.1", want: false},
|
||||
{host: "192.168.23.1:8080", want: false},
|
||||
{host: "malformed remote addr", want: false},
|
||||
{host: "localhost", want: true},
|
||||
{host: "localhost:8080", want: true},
|
||||
{host: "127.0.0.1", want: true},
|
||||
{host: "127.0.0.1:8080", want: true},
|
||||
{host: "::1", want: true},
|
||||
{host: "[::1]:8080", want: true},
|
||||
}
|
||||
for _, tt := range testCases {
|
||||
req := &http.Request{RemoteAddr: tt.host}
|
||||
any, sensitive := AuthRequest(req)
|
||||
if any != tt.want || sensitive != tt.want {
|
||||
t.Errorf("AuthRequest(%q) = %t, %t; want %t, %t", tt.host, any, sensitive, tt.want, tt.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestParseTemplate checks that all templates used by this package are valid
|
||||
// as they are parsed on first usage
|
||||
func TestParseTemplate(t *testing.T) {
|
||||
if tmpl := distTmpl(); tmpl == nil {
|
||||
t.Error("invalid template returned from distTmpl()")
|
||||
}
|
||||
if tmpl := pageTmpl(); tmpl == nil {
|
||||
t.Error("invalid template returned from pageTmpl()")
|
||||
}
|
||||
if tmpl := eventsTmpl(); tmpl == nil {
|
||||
t.Error("invalid template returned from eventsTmpl()")
|
||||
}
|
||||
}
|
||||
|
||||
func benchmarkTrace(b *testing.B, maxEvents, numEvents int) {
|
||||
numSpans := (b.N + numEvents + 1) / numEvents
|
||||
|
||||
for i := 0; i < numSpans; i++ {
|
||||
tr := New("test", "test")
|
||||
tr.SetMaxEvents(maxEvents)
|
||||
for j := 0; j < numEvents; j++ {
|
||||
tr.LazyPrintf("%d", j)
|
||||
}
|
||||
tr.Finish()
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkTrace_Default_2(b *testing.B) {
|
||||
benchmarkTrace(b, 0, 2)
|
||||
}
|
||||
|
||||
func BenchmarkTrace_Default_10(b *testing.B) {
|
||||
benchmarkTrace(b, 0, 10)
|
||||
}
|
||||
|
||||
func BenchmarkTrace_Default_100(b *testing.B) {
|
||||
benchmarkTrace(b, 0, 100)
|
||||
}
|
||||
|
||||
func BenchmarkTrace_Default_1000(b *testing.B) {
|
||||
benchmarkTrace(b, 0, 1000)
|
||||
}
|
||||
|
||||
func BenchmarkTrace_Default_10000(b *testing.B) {
|
||||
benchmarkTrace(b, 0, 10000)
|
||||
}
|
||||
|
||||
func BenchmarkTrace_10_2(b *testing.B) {
|
||||
benchmarkTrace(b, 10, 2)
|
||||
}
|
||||
|
||||
func BenchmarkTrace_10_10(b *testing.B) {
|
||||
benchmarkTrace(b, 10, 10)
|
||||
}
|
||||
|
||||
func BenchmarkTrace_10_100(b *testing.B) {
|
||||
benchmarkTrace(b, 10, 100)
|
||||
}
|
||||
|
||||
func BenchmarkTrace_10_1000(b *testing.B) {
|
||||
benchmarkTrace(b, 10, 1000)
|
||||
}
|
||||
|
||||
func BenchmarkTrace_10_10000(b *testing.B) {
|
||||
benchmarkTrace(b, 10, 10000)
|
||||
}
|
||||
|
||||
func BenchmarkTrace_100_2(b *testing.B) {
|
||||
benchmarkTrace(b, 100, 2)
|
||||
}
|
||||
|
||||
func BenchmarkTrace_100_10(b *testing.B) {
|
||||
benchmarkTrace(b, 100, 10)
|
||||
}
|
||||
|
||||
func BenchmarkTrace_100_100(b *testing.B) {
|
||||
benchmarkTrace(b, 100, 100)
|
||||
}
|
||||
|
||||
func BenchmarkTrace_100_1000(b *testing.B) {
|
||||
benchmarkTrace(b, 100, 1000)
|
||||
}
|
||||
|
||||
func BenchmarkTrace_100_10000(b *testing.B) {
|
||||
benchmarkTrace(b, 100, 10000)
|
||||
}
|
||||
|
||||
func BenchmarkTrace_1000_2(b *testing.B) {
|
||||
benchmarkTrace(b, 1000, 2)
|
||||
}
|
||||
|
||||
func BenchmarkTrace_1000_10(b *testing.B) {
|
||||
benchmarkTrace(b, 1000, 10)
|
||||
}
|
||||
|
||||
func BenchmarkTrace_1000_100(b *testing.B) {
|
||||
benchmarkTrace(b, 1000, 100)
|
||||
}
|
||||
|
||||
func BenchmarkTrace_1000_1000(b *testing.B) {
|
||||
benchmarkTrace(b, 1000, 1000)
|
||||
}
|
||||
|
||||
func BenchmarkTrace_1000_10000(b *testing.B) {
|
||||
benchmarkTrace(b, 1000, 10000)
|
||||
}
|
Reference in New Issue
Block a user