2021-04-18 15:41:13 -04:00
|
|
|
// Copyright (c) 2015-2021 MinIO, Inc.
|
|
|
|
//
|
|
|
|
// This file is part of MinIO Object Storage stack
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2016-06-25 23:22:04 -04:00
|
|
|
|
2016-08-18 19:23:42 -04:00
|
|
|
package cmd
|
2016-06-25 23:22:04 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2018-03-15 16:27:16 -04:00
|
|
|
"context"
|
2016-07-08 17:28:06 -04:00
|
|
|
"math"
|
2016-06-25 23:22:04 -04:00
|
|
|
"math/rand"
|
|
|
|
"strconv"
|
|
|
|
"testing"
|
2016-11-22 21:18:22 -05:00
|
|
|
|
2022-08-29 19:57:16 -04:00
|
|
|
"github.com/dustin/go-humanize"
|
2016-06-25 23:22:04 -04:00
|
|
|
)
|
|
|
|
|
2016-06-29 01:12:36 -04:00
|
|
|
// Benchmark utility functions for ObjectLayer.PutObject().
|
|
|
|
// Creates Object layer setup ( MakeBucket ) and then runs the PutObject benchmark.
|
|
|
|
func runPutObjectBenchmark(b *testing.B, obj ObjectLayer, objSize int) {
|
|
|
|
var err error
|
|
|
|
// obtains random bucket name.
|
|
|
|
bucket := getRandomBucketName()
|
|
|
|
// create bucket.
|
2022-07-25 20:51:32 -04:00
|
|
|
err = obj.MakeBucketWithLocation(context.Background(), bucket, MakeBucketOptions{})
|
2016-06-29 01:12:36 -04:00
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// get text data generated for number of bytes equal to object size.
|
|
|
|
textData := generateBytesData(objSize)
|
|
|
|
// generate md5sum for the generated data.
|
|
|
|
// md5sum of the data to written is required as input for PutObject.
|
2017-10-22 01:30:34 -04:00
|
|
|
|
|
|
|
md5hex := getMD5Hash(textData)
|
|
|
|
sha256hex := ""
|
|
|
|
|
2016-06-29 01:12:36 -04:00
|
|
|
// benchmark utility which helps obtain number of allocations and bytes allocated per ops.
|
|
|
|
b.ReportAllocs()
|
|
|
|
// the actual benchmark for PutObject starts here. Reset the benchmark timer.
|
|
|
|
b.ResetTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
// insert the object.
|
2018-03-15 16:27:16 -04:00
|
|
|
objInfo, err := obj.PutObject(context.Background(), bucket, "object"+strconv.Itoa(i),
|
2020-12-27 01:58:06 -05:00
|
|
|
mustGetPutObjReader(b, bytes.NewReader(textData), int64(len(textData)), md5hex, sha256hex), ObjectOptions{})
|
2016-06-29 01:12:36 -04:00
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
2017-10-22 01:30:34 -04:00
|
|
|
if objInfo.ETag != md5hex {
|
|
|
|
b.Fatalf("Write no: %d: Md5Sum mismatch during object write into the bucket: Expected %s, got %s", i+1, objInfo.ETag, md5hex)
|
2016-06-29 01:12:36 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Benchmark ends here. Stop timer.
|
|
|
|
b.StopTimer()
|
|
|
|
}
|
|
|
|
|
2016-07-08 17:28:06 -04:00
|
|
|
// Benchmark utility functions for ObjectLayer.PutObjectPart().
|
|
|
|
// Creates Object layer setup ( MakeBucket ) and then runs the PutObjectPart benchmark.
|
|
|
|
func runPutObjectPartBenchmark(b *testing.B, obj ObjectLayer, partSize int) {
|
|
|
|
var err error
|
|
|
|
// obtains random bucket name.
|
|
|
|
bucket := getRandomBucketName()
|
|
|
|
object := getRandomObjectName()
|
|
|
|
|
|
|
|
// create bucket.
|
2022-07-25 20:51:32 -04:00
|
|
|
err = obj.MakeBucketWithLocation(context.Background(), bucket, MakeBucketOptions{})
|
2016-07-08 17:28:06 -04:00
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2016-11-22 21:18:22 -05:00
|
|
|
objSize := 128 * humanize.MiByte
|
2016-07-08 17:28:06 -04:00
|
|
|
|
2017-05-14 15:05:51 -04:00
|
|
|
// PutObjectPart returns etag of the object inserted.
|
|
|
|
// etag variable is assigned with that value.
|
2022-08-29 19:57:16 -04:00
|
|
|
var etag string
|
2016-07-08 17:28:06 -04:00
|
|
|
// get text data generated for number of bytes equal to object size.
|
|
|
|
textData := generateBytesData(objSize)
|
|
|
|
// generate md5sum for the generated data.
|
|
|
|
// md5sum of the data to written is required as input for NewMultipartUpload.
|
2022-08-29 19:57:16 -04:00
|
|
|
res, err := obj.NewMultipartUpload(context.Background(), bucket, object, ObjectOptions{})
|
2016-07-08 17:28:06 -04:00
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-10-22 01:30:34 -04:00
|
|
|
sha256hex := ""
|
|
|
|
|
2016-07-08 17:28:06 -04:00
|
|
|
var textPartData []byte
|
|
|
|
// benchmark utility which helps obtain number of allocations and bytes allocated per ops.
|
|
|
|
b.ReportAllocs()
|
|
|
|
// the actual benchmark for PutObjectPart starts here. Reset the benchmark timer.
|
|
|
|
b.ResetTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
// insert the object.
|
|
|
|
totalPartsNR := int(math.Ceil(float64(objSize) / float64(partSize)))
|
|
|
|
for j := 0; j < totalPartsNR; j++ {
|
|
|
|
if j < totalPartsNR-1 {
|
|
|
|
textPartData = textData[j*partSize : (j+1)*partSize-1]
|
|
|
|
} else {
|
|
|
|
textPartData = textData[j*partSize:]
|
|
|
|
}
|
2021-05-24 12:28:19 -04:00
|
|
|
md5hex := getMD5Hash(textPartData)
|
2017-01-31 12:38:34 -05:00
|
|
|
var partInfo PartInfo
|
2022-08-29 19:57:16 -04:00
|
|
|
partInfo, err = obj.PutObjectPart(context.Background(), bucket, object, res.UploadID, j,
|
2020-12-27 01:58:06 -05:00
|
|
|
mustGetPutObjReader(b, bytes.NewReader(textPartData), int64(len(textPartData)), md5hex, sha256hex), ObjectOptions{})
|
2016-07-08 17:28:06 -04:00
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
2017-10-22 01:30:34 -04:00
|
|
|
if partInfo.ETag != md5hex {
|
|
|
|
b.Fatalf("Write no: %d: Md5Sum mismatch during object write into the bucket: Expected %s, got %s", i+1, etag, md5hex)
|
2016-07-08 17:28:06 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Benchmark ends here. Stop timer.
|
|
|
|
b.StopTimer()
|
|
|
|
}
|
|
|
|
|
2020-06-12 23:04:01 -04:00
|
|
|
// creates Erasure/FS backend setup, obtains the object layer and calls the runPutObjectPartBenchmark function.
|
2016-07-21 14:17:28 -04:00
|
|
|
func benchmarkPutObjectPart(b *testing.B, instanceType string, objSize int) {
|
2020-06-12 23:04:01 -04:00
|
|
|
// create a temp Erasure/FS backend.
|
2020-04-14 20:52:38 -04:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
objLayer, disks, err := prepareTestBackend(ctx, instanceType)
|
2016-07-08 17:28:06 -04:00
|
|
|
if err != nil {
|
|
|
|
b.Fatalf("Failed obtaining Temp Backend: <ERROR> %s", err)
|
|
|
|
}
|
|
|
|
// cleaning up the backend by removing all the directories and files created on function return.
|
|
|
|
defer removeRoots(disks)
|
2018-08-15 00:41:47 -04:00
|
|
|
|
2016-07-21 14:17:28 -04:00
|
|
|
// uses *testing.B and the object Layer to run the benchmark.
|
|
|
|
runPutObjectPartBenchmark(b, objLayer, objSize)
|
2016-07-08 17:28:06 -04:00
|
|
|
}
|
|
|
|
|
2020-06-12 23:04:01 -04:00
|
|
|
// creates Erasure/FS backend setup, obtains the object layer and calls the runPutObjectBenchmark function.
|
2016-07-21 14:17:28 -04:00
|
|
|
func benchmarkPutObject(b *testing.B, instanceType string, objSize int) {
|
2020-06-12 23:04:01 -04:00
|
|
|
// create a temp Erasure/FS backend.
|
2020-04-14 20:52:38 -04:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
objLayer, disks, err := prepareTestBackend(ctx, instanceType)
|
2016-06-29 01:12:36 -04:00
|
|
|
if err != nil {
|
|
|
|
b.Fatalf("Failed obtaining Temp Backend: <ERROR> %s", err)
|
|
|
|
}
|
|
|
|
// cleaning up the backend by removing all the directories and files created on function return.
|
|
|
|
defer removeRoots(disks)
|
2018-08-15 00:41:47 -04:00
|
|
|
|
2016-07-21 14:17:28 -04:00
|
|
|
// uses *testing.B and the object Layer to run the benchmark.
|
|
|
|
runPutObjectBenchmark(b, objLayer, objSize)
|
2016-06-29 01:12:36 -04:00
|
|
|
}
|
|
|
|
|
2020-06-12 23:04:01 -04:00
|
|
|
// creates Erasure/FS backend setup, obtains the object layer and runs parallel benchmark for put object.
|
2016-07-21 14:17:28 -04:00
|
|
|
func benchmarkPutObjectParallel(b *testing.B, instanceType string, objSize int) {
|
2020-06-12 23:04:01 -04:00
|
|
|
// create a temp Erasure/FS backend.
|
2020-04-14 20:52:38 -04:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
objLayer, disks, err := prepareTestBackend(ctx, instanceType)
|
2016-07-21 14:17:28 -04:00
|
|
|
if err != nil {
|
|
|
|
b.Fatalf("Failed obtaining Temp Backend: <ERROR> %s", err)
|
2016-06-29 01:12:36 -04:00
|
|
|
}
|
2016-07-21 14:17:28 -04:00
|
|
|
// cleaning up the backend by removing all the directories and files created on function return.
|
|
|
|
defer removeRoots(disks)
|
2018-08-15 00:41:47 -04:00
|
|
|
|
2016-07-21 14:17:28 -04:00
|
|
|
// uses *testing.B and the object Layer to run the benchmark.
|
|
|
|
runPutObjectBenchmarkParallel(b, objLayer, objSize)
|
2016-06-29 01:12:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// randomly picks a character and returns its equivalent byte array.
|
|
|
|
func getRandomByte() []byte {
|
|
|
|
const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
|
|
|
// seeding the random number generator.
|
2017-03-18 14:28:41 -04:00
|
|
|
rand.Seed(UTCNow().UnixNano())
|
2016-06-29 01:12:36 -04:00
|
|
|
// pick a character randomly.
|
2019-02-13 07:59:36 -05:00
|
|
|
return []byte{letterBytes[rand.Intn(len(letterBytes))]}
|
2016-06-29 01:12:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// picks a random byte and repeats it to size bytes.
|
2016-06-25 23:22:04 -04:00
|
|
|
func generateBytesData(size int) []byte {
|
2016-06-27 13:01:09 -04:00
|
|
|
// repeat the random character chosen size
|
2016-06-25 23:22:04 -04:00
|
|
|
return bytes.Repeat(getRandomByte(), size)
|
|
|
|
}
|
|
|
|
|
2016-07-10 14:08:45 -04:00
|
|
|
// Parallel benchmark utility functions for ObjectLayer.PutObject().
|
|
|
|
// Creates Object layer setup ( MakeBucket ) and then runs the PutObject benchmark.
|
|
|
|
func runPutObjectBenchmarkParallel(b *testing.B, obj ObjectLayer, objSize int) {
|
|
|
|
// obtains random bucket name.
|
|
|
|
bucket := getRandomBucketName()
|
|
|
|
// create bucket.
|
2022-07-25 20:51:32 -04:00
|
|
|
err := obj.MakeBucketWithLocation(context.Background(), bucket, MakeBucketOptions{})
|
2016-07-10 14:08:45 -04:00
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// get text data generated for number of bytes equal to object size.
|
|
|
|
textData := generateBytesData(objSize)
|
|
|
|
// generate md5sum for the generated data.
|
|
|
|
// md5sum of the data to written is required as input for PutObject.
|
2017-10-22 01:30:34 -04:00
|
|
|
|
2021-05-24 12:28:19 -04:00
|
|
|
md5hex := getMD5Hash(textData)
|
2017-10-22 01:30:34 -04:00
|
|
|
sha256hex := ""
|
|
|
|
|
2016-07-10 14:08:45 -04:00
|
|
|
// benchmark utility which helps obtain number of allocations and bytes allocated per ops.
|
|
|
|
b.ReportAllocs()
|
|
|
|
// the actual benchmark for PutObject starts here. Reset the benchmark timer.
|
|
|
|
b.ResetTimer()
|
|
|
|
|
|
|
|
b.RunParallel(func(pb *testing.PB) {
|
|
|
|
i := 0
|
|
|
|
for pb.Next() {
|
|
|
|
// insert the object.
|
2018-03-15 16:27:16 -04:00
|
|
|
objInfo, err := obj.PutObject(context.Background(), bucket, "object"+strconv.Itoa(i),
|
2020-12-27 01:58:06 -05:00
|
|
|
mustGetPutObjReader(b, bytes.NewReader(textData), int64(len(textData)), md5hex, sha256hex), ObjectOptions{})
|
2016-07-10 14:08:45 -04:00
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
2017-10-22 01:30:34 -04:00
|
|
|
if objInfo.ETag != md5hex {
|
|
|
|
b.Fatalf("Write no: Md5Sum mismatch during object write into the bucket: Expected %s, got %s", objInfo.ETag, md5hex)
|
2016-07-10 14:08:45 -04:00
|
|
|
}
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
// Benchmark ends here. Stop timer.
|
|
|
|
b.StopTimer()
|
|
|
|
}
|