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)
|
defer closeBody(res.Body)
|
||||||
|
|
||||||
verifier := bao.NewVerifier(res.Body, baoProof)
|
verifier := bao.NewVerifier(res.Body, baoProof, s.logger)
|
||||||
defer func(Body io.ReadCloser) {
|
defer func(Body io.ReadCloser) {
|
||||||
err := Body.Close()
|
err := Body.Close()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
23
bao/bao.go
23
bao/bao.go
|
@ -9,6 +9,9 @@ import (
|
||||||
"math"
|
"math"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"go.uber.org/zap"
|
||||||
|
|
||||||
"github.com/docker/go-units"
|
"github.com/docker/go-units"
|
||||||
"github.com/hashicorp/go-plugin"
|
"github.com/hashicorp/go-plugin"
|
||||||
|
@ -27,10 +30,12 @@ var _ io.ReadCloser = (*Verifier)(nil)
|
||||||
var ErrVerifyFailed = errors.New("verification failed")
|
var ErrVerifyFailed = errors.New("verification failed")
|
||||||
|
|
||||||
type Verifier struct {
|
type Verifier struct {
|
||||||
r io.ReadCloser
|
r io.ReadCloser
|
||||||
proof Result
|
proof Result
|
||||||
read uint64
|
read uint64
|
||||||
buffer *bytes.Buffer
|
buffer *bytes.Buffer
|
||||||
|
logger *zap.Logger
|
||||||
|
verifyTime time.Duration
|
||||||
}
|
}
|
||||||
|
|
||||||
func (v *Verifier) Read(p []byte) (int, error) {
|
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
|
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 {
|
if status, err := bao.Verify(buf[:bytesRead], v.read, v.proof.Proof, v.proof.Hash); err != nil || !status {
|
||||||
return n, errors.Join(ErrVerifyFailed, err)
|
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.read += uint64(bytesRead)
|
||||||
v.buffer.Write(buf[:bytesRead]) // Append new data to the buffer
|
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
|
return &result, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewVerifier(r io.ReadCloser, proof Result) *Verifier {
|
func NewVerifier(r io.ReadCloser, proof Result, logger *zap.Logger) *Verifier {
|
||||||
return &Verifier{
|
return &Verifier{
|
||||||
r: r,
|
r: r,
|
||||||
proof: proof,
|
proof: proof,
|
||||||
buffer: new(bytes.Buffer),
|
buffer: new(bytes.Buffer),
|
||||||
|
logger: logger,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue