From 556ae07857bb896491a045195ac96e79e9d7047c Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Fri, 19 Nov 2021 10:41:37 -0800 Subject: [PATCH] simplify the reader for speedtest (#13682) additionally count only success operations, truncated incomplete calls don't need to be counted. --- cmd/peer-rest-server.go | 46 +++++++++++------------------------------ go.mod | 2 +- go.sum | 3 ++- 3 files changed, 15 insertions(+), 36 deletions(-) diff --git a/cmd/peer-rest-server.go b/cmd/peer-rest-server.go index 48df0eb8c..4bce86337 100644 --- a/cmd/peer-rest-server.go +++ b/cmd/peer-rest-server.go @@ -19,7 +19,6 @@ package cmd import ( "context" - "crypto/rand" "encoding/gob" "errors" "fmt" @@ -40,6 +39,7 @@ import ( "github.com/minio/minio/internal/event" "github.com/minio/minio/internal/hash" "github.com/minio/minio/internal/logger" + "github.com/minio/pkg/randreader" "github.com/tinylib/msgp/msgp" ) @@ -1125,31 +1125,8 @@ type SpeedtestResult struct { Error string } -// SpeedtestObject implements "random-read" object reader -type SpeedtestObject struct { - buf []byte - remaining int - totalBytesWritten *uint64 -} - -func (bo *SpeedtestObject) Read(b []byte) (int, error) { - if bo.remaining == 0 { - return 0, io.EOF - } - if len(b) == 0 { - return 0, nil - } - if len(b) > len(bo.buf) { - b = b[:len(bo.buf)] - } - if len(b) > bo.remaining { - b = b[:bo.remaining] - } - copy(b, bo.buf) - bo.remaining -= len(b) - - atomic.AddUint64(bo.totalBytesWritten, uint64(len(b))) - return len(b), nil +func newRandomReader(size int) io.Reader { + return io.LimitReader(randreader.New(), int64(size)) } // Runs the speedtest on local MinIO process. @@ -1162,10 +1139,6 @@ func selfSpeedtest(ctx context.Context, size, concurrent int, duration time.Dura var retError string bucket := minioMetaSpeedTestBucket - - buf := make([]byte, humanize.MiByte) - rand.Read(buf) - objCountPerThread := make([]uint64, concurrent) uploadsStopped := false @@ -1187,7 +1160,7 @@ func selfSpeedtest(ctx context.Context, size, concurrent int, duration time.Dura go func(i int) { defer wg.Done() for { - hashReader, err := hash.NewReader(&SpeedtestObject{buf, size, &totalBytesWritten}, + hashReader, err := hash.NewReader(newRandomReader(size), int64(size), "", "", int64(size)) if err != nil { retError = err.Error() @@ -1195,7 +1168,7 @@ func selfSpeedtest(ctx context.Context, size, concurrent int, duration time.Dura break } reader := NewPutObjReader(hashReader) - _, err = objAPI.PutObject(uploadsCtx, bucket, fmt.Sprintf("%s.%d.%d", + objInfo, err := objAPI.PutObject(uploadsCtx, bucket, fmt.Sprintf("%s.%d.%d", objNamePrefix, i, objCountPerThread[i]), reader, ObjectOptions{}) if err != nil && !uploadsStopped { retError = err.Error() @@ -1204,6 +1177,7 @@ func selfSpeedtest(ctx context.Context, size, concurrent int, duration time.Dura if err != nil { break } + atomic.AddUint64(&totalBytesWritten, uint64(objInfo.Size)) objCountPerThread[i]++ } }(i) @@ -1243,8 +1217,12 @@ func selfSpeedtest(ctx context.Context, size, concurrent int, duration time.Dura } n, err := io.Copy(ioutil.Discard, r) r.Close() - - atomic.AddUint64(&totalBytesRead, uint64(n)) + if err == nil { + // Only capture success criteria - do not + // have to capture failed reads, truncated + // reads etc. + atomic.AddUint64(&totalBytesRead, uint64(n)) + } if err != nil && !downloadsStopped { retError = err.Error() logger.LogIf(ctx, err) diff --git a/go.mod b/go.mod index d7dc13b0c..0121183ee 100644 --- a/go.mod +++ b/go.mod @@ -52,7 +52,7 @@ require ( github.com/minio/mc v0.0.0-20211115052100-7fd441ec6c5b // indirect github.com/minio/minio-go/v7 v7.0.16-0.20211108161804-a7a36ee131df github.com/minio/parquet-go v1.1.0 - github.com/minio/pkg v1.1.7 + github.com/minio/pkg v1.1.8 github.com/minio/selfupdate v0.3.1 github.com/minio/sha256-simd v1.0.0 github.com/minio/simdjson-go v0.2.1 diff --git a/go.sum b/go.sum index ed747054f..bc1461d69 100644 --- a/go.sum +++ b/go.sum @@ -1111,8 +1111,9 @@ github.com/minio/pkg v1.0.3/go.mod h1:obU54TZ9QlMv0TRaDgQ/JTzf11ZSXxnSfLrm4tMtBP github.com/minio/pkg v1.0.4/go.mod h1:obU54TZ9QlMv0TRaDgQ/JTzf11ZSXxnSfLrm4tMtBP8= github.com/minio/pkg v1.0.11/go.mod h1:32x/3OmGB0EOi1N+3ggnp+B5VFkSBBB9svPMVfpnf14= github.com/minio/pkg v1.1.3/go.mod h1:32x/3OmGB0EOi1N+3ggnp+B5VFkSBBB9svPMVfpnf14= -github.com/minio/pkg v1.1.7 h1:v+2/ol/h1Sl0iJdOFN1Srk4CzksMIDsfugXCZYb5L7Y= github.com/minio/pkg v1.1.7/go.mod h1:32x/3OmGB0EOi1N+3ggnp+B5VFkSBBB9svPMVfpnf14= +github.com/minio/pkg v1.1.8 h1:m/yKFtUCmLKp6kOO6Wf8C2SRUIIhtkIf6+rGlrB5RVk= +github.com/minio/pkg v1.1.8/go.mod h1:32x/3OmGB0EOi1N+3ggnp+B5VFkSBBB9svPMVfpnf14= github.com/minio/selfupdate v0.3.1 h1:BWEFSNnrZVMUWXbXIgLDNDjbejkmpAmZvy/nCz1HlEs= github.com/minio/selfupdate v0.3.1/go.mod h1:b8ThJzzH7u2MkF6PcIra7KaXO9Khf6alWPvMSyTDCFM= github.com/minio/sha256-simd v0.1.1/go.mod h1:B5e1o+1/KgNmWrSQK08Y6Z1Vb5pwIktudl0J58iy0KM=