refactor: add performance logging for bao
This commit is contained in:
parent
0d0ec43125
commit
c984d72cfd
|
@ -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 {
|
||||
|
|
15
bao/bao.go
15
bao/bao.go
|
@ -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,
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue