github.com/golang/dep@v0.5.4/gps/metrics.go (about) 1 // Copyright 2017 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package gps 6 7 import ( 8 "bytes" 9 "fmt" 10 "log" 11 "sort" 12 "text/tabwriter" 13 "time" 14 ) 15 16 type metrics struct { 17 stack []string 18 times map[string]time.Duration 19 last time.Time 20 } 21 22 func newMetrics() *metrics { 23 return &metrics{ 24 stack: []string{"other"}, 25 times: map[string]time.Duration{ 26 "other": 0, 27 }, 28 last: time.Now(), 29 } 30 } 31 32 func (m *metrics) push(name string) { 33 cn := m.stack[len(m.stack)-1] 34 m.times[cn] += time.Since(m.last) 35 36 m.stack = append(m.stack, name) 37 m.last = time.Now() 38 } 39 40 func (m *metrics) pop() { 41 on := m.stack[len(m.stack)-1] 42 m.times[on] += time.Since(m.last) 43 44 m.stack = m.stack[:len(m.stack)-1] 45 m.last = time.Now() 46 } 47 48 func (m *metrics) dump(l *log.Logger) { 49 s := make(ndpairs, len(m.times)) 50 k := 0 51 for n, d := range m.times { 52 s[k] = ndpair{ 53 n: n, 54 d: d, 55 } 56 k++ 57 } 58 59 sort.Sort(sort.Reverse(s)) 60 61 var tot time.Duration 62 var buf bytes.Buffer 63 w := tabwriter.NewWriter(&buf, 0, 0, 1, ' ', tabwriter.AlignRight) 64 for _, nd := range s { 65 tot += nd.d 66 fmt.Fprintf(w, "\t%s:\t%v\t\n", nd.n, nd.d) 67 } 68 fmt.Fprintf(w, "\n\tTOTAL:\t%v\t\n", tot) 69 w.Flush() 70 71 l.Println("\nSolver wall times by segment:") 72 l.Println((&buf).String()) 73 } 74 75 type ndpair struct { 76 n string 77 d time.Duration 78 } 79 80 type ndpairs []ndpair 81 82 func (s ndpairs) Less(i, j int) bool { return s[i].d < s[j].d } 83 func (s ndpairs) Swap(i, j int) { s[i], s[j] = s[j], s[i] } 84 func (s ndpairs) Len() int { return len(s) }