github.com/hanwen/go-fuse@v1.0.0/benchmark/latencymap.go (about) 1 // Copyright 2016 the Go-FUSE 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 benchmark 6 7 import ( 8 "sync" 9 "time" 10 ) 11 12 type latencyMapEntry struct { 13 count int 14 dur time.Duration 15 } 16 17 type LatencyMap struct { 18 sync.Mutex 19 stats map[string]*latencyMapEntry 20 } 21 22 func NewLatencyMap() *LatencyMap { 23 m := &LatencyMap{} 24 m.stats = make(map[string]*latencyMapEntry) 25 return m 26 } 27 28 func (m *LatencyMap) Get(name string) (count int, dt time.Duration) { 29 m.Mutex.Lock() 30 l := m.stats[name] 31 m.Mutex.Unlock() 32 if l == nil { 33 return 0, 0 34 } 35 36 return l.count, l.dur 37 } 38 39 func (m *LatencyMap) Add(name string, dt time.Duration) { 40 m.Mutex.Lock() 41 e := m.stats[name] 42 if e == nil { 43 e = new(latencyMapEntry) 44 m.stats[name] = e 45 } 46 e.count++ 47 e.dur += dt 48 m.Mutex.Unlock() 49 } 50 51 func (m *LatencyMap) Counts() map[string]int { 52 r := make(map[string]int) 53 m.Mutex.Lock() 54 for k, v := range m.stats { 55 r[k] = v.count 56 } 57 m.Mutex.Unlock() 58 59 return r 60 }