refactor: add timing tracking for reading

This commit is contained in:
Derrick Hammer 2024-02-28 12:56:39 -05:00
parent dd9fdabf47
commit 68d82390ed
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
1 changed files with 13 additions and 0 deletions

View File

@ -11,6 +11,8 @@ import (
"os/exec" "os/exec"
"time" "time"
"github.com/samber/lo"
"go.uber.org/zap" "go.uber.org/zap"
"github.com/docker/go-units" "github.com/docker/go-units"
@ -35,6 +37,7 @@ type Verifier struct {
read uint64 read uint64
buffer *bytes.Buffer buffer *bytes.Buffer
logger *zap.Logger logger *zap.Logger
readTime []time.Duration
verifyTime time.Duration verifyTime time.Duration
} }
@ -52,11 +55,21 @@ func (v *Verifier) Read(p []byte) (int, error) {
buf := make([]byte, VERIFY_CHUNK_SIZE) buf := make([]byte, VERIFY_CHUNK_SIZE)
// Continue reading from the source and verifying until we have enough data or hit an error // Continue reading from the source and verifying until we have enough data or hit an error
for v.buffer.Len() < len(p)-n { for v.buffer.Len() < len(p)-n {
readStart := time.Now()
bytesRead, err := io.ReadFull(v.r, buf) bytesRead, err := io.ReadFull(v.r, buf)
if err != nil && err != io.EOF && err != io.ErrUnexpectedEOF { if err != nil && err != io.EOF && err != io.ErrUnexpectedEOF {
return n, err // Return any read error immediately 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() timeStart := time.Now()
if status, err := bao.Verify(buf[:bytesRead], v.read, v.proof.Proof, v.proof.Hash); err != nil || !status { if status, err := bao.Verify(buf[:bytesRead], v.read, v.proof.Proof, v.proof.Hash); err != nil || !status {