refactor: add timing tracking for reading
This commit is contained in:
parent
dd9fdabf47
commit
68d82390ed
13
bao/bao.go
13
bao/bao.go
|
@ -11,6 +11,8 @@ import (
|
|||
"os/exec"
|
||||
"time"
|
||||
|
||||
"github.com/samber/lo"
|
||||
|
||||
"go.uber.org/zap"
|
||||
|
||||
"github.com/docker/go-units"
|
||||
|
@ -35,6 +37,7 @@ type Verifier struct {
|
|||
read uint64
|
||||
buffer *bytes.Buffer
|
||||
logger *zap.Logger
|
||||
readTime []time.Duration
|
||||
verifyTime time.Duration
|
||||
}
|
||||
|
||||
|
@ -52,11 +55,21 @@ func (v *Verifier) Read(p []byte) (int, error) {
|
|||
buf := make([]byte, VERIFY_CHUNK_SIZE)
|
||||
// Continue reading from the source and verifying until we have enough data or hit an error
|
||||
for v.buffer.Len() < len(p)-n {
|
||||
readStart := time.Now()
|
||||
bytesRead, err := io.ReadFull(v.r, buf)
|
||||
if err != nil && err != io.EOF && err != io.ErrUnexpectedEOF {
|
||||
return n, err // Return any read error immediately
|
||||
}
|
||||
|
||||
readEnd := time.Now()
|
||||
|
||||
v.readTime = append(v.readTime, readEnd.Sub(readStart))
|
||||
averageReadTime := lo.Reduce(v.readTime, func(acc time.Duration, cur time.Duration, _ int) time.Duration {
|
||||
return acc + cur
|
||||
}, time.Duration(0)) / time.Duration(len(v.readTime))
|
||||
|
||||
v.logger.Debug("Read time", zap.Duration("average", averageReadTime))
|
||||
|
||||
timeStart := time.Now()
|
||||
|
||||
if status, err := bao.Verify(buf[:bytesRead], v.read, v.proof.Proof, v.proof.Hash); err != nil || !status {
|
||||
|
|
Loading…
Reference in New Issue