refactor: add performance logging for bao

This commit is contained in:
Derrick Hammer 2024-02-28 12:33:13 -05:00
parent 0d0ec43125
commit c984d72cfd
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
2 changed files with 19 additions and 6 deletions

View File

@ -1684,7 +1684,7 @@ func (s *S5API) pinImportCronJob(cid string, url string, proofUrl string, userId
}
defer closeBody(res.Body)
verifier := bao.NewVerifier(res.Body, baoProof)
verifier := bao.NewVerifier(res.Body, baoProof, s.logger)
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {

View File

@ -9,6 +9,9 @@ import (
"math"
"os"
"os/exec"
"time"
"go.uber.org/zap"
"github.com/docker/go-units"
"github.com/hashicorp/go-plugin"
@ -31,6 +34,8 @@ type Verifier struct {
proof Result
read uint64
buffer *bytes.Buffer
logger *zap.Logger
verifyTime time.Duration
}
func (v *Verifier) Read(p []byte) (int, error) {
@ -52,10 +57,17 @@ func (v *Verifier) Read(p []byte) (int, error) {
return n, err // Return any read error immediately
}
timeStart := time.Now()
if status, err := bao.Verify(buf[:bytesRead], v.read, v.proof.Proof, v.proof.Hash); err != nil || !status {
return n, errors.Join(ErrVerifyFailed, err)
}
timeEnd := time.Now()
v.verifyTime += timeEnd.Sub(timeStart)
averageVerifyTime := v.verifyTime / time.Duration(v.read/VERIFY_CHUNK_SIZE)
v.logger.Debug("Verification time", zap.Duration("duration", timeEnd.Sub(timeStart)), zap.Duration("average", averageVerifyTime))
v.read += uint64(bytesRead)
v.buffer.Write(buf[:bytesRead]) // Append new data to the buffer
@ -163,10 +175,11 @@ func Hash(r io.Reader) (*Result, error) {
return &result, nil
}
func NewVerifier(r io.ReadCloser, proof Result) *Verifier {
func NewVerifier(r io.ReadCloser, proof Result, logger *zap.Logger) *Verifier {
return &Verifier{
r: r,
proof: proof,
buffer: new(bytes.Buffer),
logger: logger,
}
}