batch: add remote query profiler

This commit is contained in:
c9s 2022-06-02 16:51:44 +08:00
parent 02a8bf4c8c
commit 824951c3d5
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54
4 changed files with 29 additions and 8 deletions

View File

@ -8,6 +8,8 @@ import (
"github.com/sirupsen/logrus"
"golang.org/x/time/rate"
"github.com/c9s/bbgo/pkg/util"
)
var log = logrus.WithField("component", "batch")
@ -52,6 +54,8 @@ func (q *AsyncTimeRangedBatchQuery) Query(ctx context.Context, ch interface{}, s
log.Debugf("batch querying %T: %v <=> %v", q.Type, startTime, endTime)
queryProfiler := util.StartTimeProfile("remoteQuery")
sliceInf, err := q.Q(startTime, endTime)
if err != nil {
errC <- err
@ -60,9 +64,10 @@ func (q *AsyncTimeRangedBatchQuery) Query(ctx context.Context, ch interface{}, s
listRef := reflect.ValueOf(sliceInf)
listLen := listRef.Len()
log.Debugf("batch querying %T: %d remote records", q.Type, listLen)
queryProfiler.StopAndLog(log.Debugf)
if listLen == 0 {
if q.JumpIfEmpty > 0 {
startTime = startTime.Add(q.JumpIfEmpty)

View File

@ -4,8 +4,6 @@ import (
"context"
"time"
"golang.org/x/time/rate"
"github.com/c9s/bbgo/pkg/types"
)

View File

@ -153,5 +153,3 @@ func buildIdMap(sel SyncTask, recordSliceRef reflect.Value) map[string]struct{}
return ids
}

View File

@ -1,14 +1,21 @@
package util
import "time"
import (
"time"
)
type TimeProfile struct {
Name string
StartTime, EndTime time.Time
Duration time.Duration
}
func StartTimeProfile() TimeProfile {
return TimeProfile{StartTime: time.Now()}
func StartTimeProfile(args ...string) TimeProfile {
name := ""
if len(args) > 0 {
name = args[0]
}
return TimeProfile{StartTime: time.Now(), Name: name}
}
func (p *TimeProfile) TilNow() time.Duration {
@ -20,3 +27,16 @@ func (p *TimeProfile) Stop() time.Duration {
p.Duration = p.EndTime.Sub(p.StartTime)
return p.Duration
}
type logFunction func(format string, args ...interface{})
func (p *TimeProfile) StopAndLog(f logFunction) {
duration := p.Stop()
s := "[profile] "
if len(p.Name) > 0 {
s += p.Name
}
s += " " + duration.String()
f(s)
}