2020-02-23 09:08:47 +00:00
|
|
|
package counters
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"math"
|
2020-02-27 01:45:07 +00:00
|
|
|
"time"
|
2020-02-23 09:08:47 +00:00
|
|
|
|
|
|
|
c "github.com/Azareal/Gosora/common"
|
|
|
|
qgen "github.com/Azareal/Gosora/query_gen"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
var PerfCounter *DefaultPerfCounter
|
|
|
|
|
|
|
|
type PerfCounterBucket struct {
|
2020-02-27 01:45:07 +00:00
|
|
|
low *MutexCounter64Bucket
|
2020-02-23 09:08:47 +00:00
|
|
|
high *MutexCounter64Bucket
|
2020-02-27 01:45:07 +00:00
|
|
|
avg *MutexCounter64Bucket
|
2020-02-23 09:08:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Track perf on a per route basis
|
|
|
|
type DefaultPerfCounter struct {
|
|
|
|
buckets []PerfCounterBucket
|
|
|
|
|
|
|
|
insert *sql.Stmt
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewDefaultPerfCounter(acc *qgen.Accumulator) (*DefaultPerfCounter, error) {
|
|
|
|
co := &DefaultPerfCounter{
|
2020-02-27 01:45:07 +00:00
|
|
|
buckets: []PerfCounterBucket{
|
2020-02-23 09:08:47 +00:00
|
|
|
PerfCounterBucket{
|
2020-02-27 01:45:07 +00:00
|
|
|
low: &MutexCounter64Bucket{counter: math.MaxInt64},
|
2020-02-23 09:08:47 +00:00
|
|
|
high: &MutexCounter64Bucket{counter: 0},
|
2020-02-27 01:45:07 +00:00
|
|
|
avg: &MutexCounter64Bucket{counter: 0},
|
2020-02-23 09:08:47 +00:00
|
|
|
},
|
|
|
|
},
|
2020-02-27 01:45:07 +00:00
|
|
|
insert: acc.Insert("perfchunks").Columns("low,high,avg,createdAt").Fields("?,?,?,UTC_TIMESTAMP()").Prepare(),
|
2020-02-23 09:08:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
c.AddScheduledFifteenMinuteTask(co.Tick)
|
|
|
|
//c.AddScheduledSecondTask(co.Tick)
|
|
|
|
c.AddShutdownTask(co.Tick)
|
|
|
|
return co, acc.FirstError()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (co *DefaultPerfCounter) Tick() error {
|
2020-02-27 01:45:07 +00:00
|
|
|
getCounter := func(b *MutexCounter64Bucket) (c int64) {
|
|
|
|
b.Lock()
|
|
|
|
c = b.counter
|
|
|
|
b.counter = 0
|
|
|
|
b.Unlock()
|
|
|
|
return c
|
2020-02-23 09:08:47 +00:00
|
|
|
}
|
|
|
|
for _, b := range co.buckets {
|
2020-02-27 01:45:07 +00:00
|
|
|
var low int64
|
|
|
|
b.low.Lock()
|
|
|
|
low = b.low.counter
|
|
|
|
b.low.counter = math.MaxInt64
|
|
|
|
b.low.Unlock()
|
2020-02-23 09:08:47 +00:00
|
|
|
if low == math.MaxInt64 {
|
|
|
|
low = 0
|
|
|
|
}
|
|
|
|
high := getCounter(b.high)
|
|
|
|
avg := getCounter(b.avg)
|
|
|
|
|
|
|
|
err := co.insertChunk(low, high, avg) // TODO: Bulk insert for speed?
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(errors.WithStack(err), "perf counter")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (co *DefaultPerfCounter) insertChunk(low, high, avg int64) error {
|
|
|
|
if low == 0 && high == 0 && avg == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
c.DebugLogf("Inserting a pchunk with low %d, high %d, avg %d", low, high, avg)
|
|
|
|
_, err := co.insert.Exec(low, high, avg)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-02-27 23:21:38 +00:00
|
|
|
func (co *DefaultPerfCounter) Push(dur time.Duration /*,_ bool*/) {
|
2020-02-23 09:08:47 +00:00
|
|
|
id := 0
|
|
|
|
b := co.buckets[id]
|
2020-02-27 01:45:07 +00:00
|
|
|
//c.DebugDetail("buckets[", id, "]: ", b)
|
2020-02-23 09:08:47 +00:00
|
|
|
micro := dur.Microseconds()
|
2020-02-28 01:41:16 +00:00
|
|
|
if micro >= math.MaxInt32 {
|
|
|
|
c.LogWarning(errors.New("dur should not be int32 max or higher"))
|
2020-02-27 23:21:38 +00:00
|
|
|
}
|
2020-02-23 09:08:47 +00:00
|
|
|
|
|
|
|
low := b.low
|
2020-02-27 01:45:07 +00:00
|
|
|
low.Lock()
|
2020-02-23 09:08:47 +00:00
|
|
|
if micro < low.counter {
|
2020-02-27 01:45:07 +00:00
|
|
|
low.counter = micro
|
2020-02-23 09:08:47 +00:00
|
|
|
}
|
2020-02-27 01:45:07 +00:00
|
|
|
low.Unlock()
|
2020-02-23 09:08:47 +00:00
|
|
|
|
|
|
|
high := b.high
|
2020-02-27 01:45:07 +00:00
|
|
|
high.Lock()
|
2020-02-23 09:08:47 +00:00
|
|
|
if micro > high.counter {
|
2020-02-27 01:45:07 +00:00
|
|
|
high.counter = micro
|
2020-02-23 09:08:47 +00:00
|
|
|
}
|
2020-02-27 01:45:07 +00:00
|
|
|
high.Unlock()
|
2020-02-23 09:08:47 +00:00
|
|
|
|
|
|
|
avg := b.avg
|
2020-02-27 01:45:07 +00:00
|
|
|
avg.Lock()
|
2020-02-23 09:08:47 +00:00
|
|
|
if micro != avg.counter {
|
|
|
|
if avg.counter == 0 {
|
2020-02-27 01:45:07 +00:00
|
|
|
avg.counter = micro
|
|
|
|
} else {
|
|
|
|
avg.counter = (micro + avg.counter) / 2
|
2020-02-23 09:08:47 +00:00
|
|
|
}
|
|
|
|
}
|
2020-02-27 01:45:07 +00:00
|
|
|
avg.Unlock()
|
2020-02-23 09:08:47 +00:00
|
|
|
}
|