mirror of
https://github.com/minio/minio.git
synced 2025-11-10 05:59:43 -05:00
Add ability to test drive speeds on a MinIO setup (#7664)
- Extends existing Admin API to measure disk performance
This commit is contained in:
committed by
kannappanr
parent
e7b3f39064
commit
6ba323b009
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* MinIO Cloud Storage, (C) 2018 MinIO, Inc.
|
||||
* MinIO Cloud Storage, (C) 2018-2019 MinIO, Inc.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -17,25 +17,9 @@
|
||||
package disk
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/rand"
|
||||
"errors"
|
||||
"os"
|
||||
"path"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
humanize "github.com/dustin/go-humanize"
|
||||
)
|
||||
|
||||
// file size for performance read and write checks
|
||||
const (
|
||||
randBufSize = 1 * humanize.KiByte
|
||||
randParts = 1024
|
||||
fileSize = randParts * randBufSize
|
||||
|
||||
// Total count of read / write iteration for performance measurement
|
||||
iterations = 10
|
||||
"github.com/ncw/directio"
|
||||
)
|
||||
|
||||
// Info stat fs struct is container which holds following values
|
||||
@@ -64,9 +48,9 @@ type Performance struct {
|
||||
}
|
||||
|
||||
// GetPerformance returns given disk's read and write performance
|
||||
func GetPerformance(path string) Performance {
|
||||
func GetPerformance(path string, size int64) Performance {
|
||||
perf := Performance{}
|
||||
write, read, err := doPerfMeasure(path)
|
||||
write, read, err := doPerfMeasure(path, size)
|
||||
if err != nil {
|
||||
perf.Error = err.Error()
|
||||
return perf
|
||||
@@ -78,63 +62,36 @@ func GetPerformance(path string) Performance {
|
||||
|
||||
// Calculate the write and read performance - write and read 10 tmp (1 MiB)
|
||||
// files and find the average time taken (Bytes / Sec)
|
||||
func doPerfMeasure(fsPath string) (write, read float64, err error) {
|
||||
var count int
|
||||
var totalWriteElapsed time.Duration
|
||||
var totalReadElapsed time.Duration
|
||||
|
||||
func doPerfMeasure(fsPath string, size int64) (writeSpeed, readSpeed float64, err error) {
|
||||
// Remove the file created for speed test purposes
|
||||
defer os.RemoveAll(fsPath)
|
||||
|
||||
randBuf := make([]byte, randBufSize)
|
||||
rand.Read(randBuf)
|
||||
buf := bytes.Repeat(randBuf, randParts)
|
||||
|
||||
// create the enclosing directory
|
||||
err = os.MkdirAll(fsPath, 0777)
|
||||
// Create a file with O_DIRECT flag
|
||||
w, err := OpenFileDirectIO(fsPath, os.O_CREATE|os.O_WRONLY|os.O_EXCL, 0666)
|
||||
if err != nil {
|
||||
return 0, 0, err
|
||||
}
|
||||
|
||||
for count = 1; count <= iterations; count++ {
|
||||
fsTempObjPath := path.Join(fsPath, strconv.Itoa(count))
|
||||
// Fetch aligned buf for direct-io
|
||||
buf := directio.AlignedBlock(speedTestBlockSize)
|
||||
|
||||
// Write performance calculation
|
||||
writeStart := time.Now()
|
||||
n, err := writeFile(fsTempObjPath, buf)
|
||||
|
||||
if err != nil {
|
||||
return 0, 0, err
|
||||
}
|
||||
if n != fileSize {
|
||||
return 0, 0, errors.New("Could not write temporary data to disk")
|
||||
}
|
||||
|
||||
writeElapsed := time.Since(writeStart)
|
||||
totalWriteElapsed += writeElapsed
|
||||
|
||||
// Read performance calculation
|
||||
readStart := time.Now()
|
||||
n, err = readFile(fsTempObjPath, buf)
|
||||
|
||||
if err != nil {
|
||||
return 0, 0, err
|
||||
}
|
||||
if n != fileSize {
|
||||
return 0, 0, errors.New("Could not read temporary data from disk")
|
||||
}
|
||||
|
||||
readElapsed := time.Since(readStart)
|
||||
totalReadElapsed += readElapsed
|
||||
writeSpeed, err = speedTestWrite(w, buf, size)
|
||||
w.Close()
|
||||
if err != nil {
|
||||
return 0, 0, err
|
||||
}
|
||||
// Average time spent = total time elapsed / number of writes
|
||||
avgWriteTime := totalWriteElapsed.Seconds() / float64(count)
|
||||
// Write perf = fileSize (in Bytes) / average time spent writing (in seconds)
|
||||
write = fileSize / avgWriteTime
|
||||
|
||||
// Average time spent = total time elapsed / number of writes
|
||||
avgReadTime := totalReadElapsed.Seconds() / float64(count)
|
||||
// read perf = fileSize (in Bytes) / average time spent reading (in seconds)
|
||||
read = fileSize / avgReadTime
|
||||
// Open file to compute read speed
|
||||
r, err := OpenFileDirectIO(fsPath, os.O_RDONLY, 0666)
|
||||
if err != nil {
|
||||
return 0, 0, err
|
||||
}
|
||||
defer r.Close()
|
||||
|
||||
return write, read, nil
|
||||
readSpeed, err = speedTestRead(r, buf, size)
|
||||
if err != nil {
|
||||
return 0, 0, err
|
||||
}
|
||||
|
||||
return writeSpeed, readSpeed, nil
|
||||
}
|
||||
|
||||
@@ -1,52 +0,0 @@
|
||||
/*
|
||||
* MinIO Cloud Storage, (C) 2018 MinIO, Inc.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package disk
|
||||
|
||||
import (
|
||||
"os"
|
||||
)
|
||||
|
||||
func readFile(path string, buf []byte) (int, error) {
|
||||
f, err := os.Open(path)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
n, err := f.Read(buf)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
return n, nil
|
||||
}
|
||||
|
||||
func writeFile(path string, data []byte) (int, error) {
|
||||
f, err := os.Create(path)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
n, err := f.Write(data)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
f.Sync()
|
||||
return n, nil
|
||||
}
|
||||
97
pkg/disk/speedtest.go
Normal file
97
pkg/disk/speedtest.go
Normal file
@@ -0,0 +1,97 @@
|
||||
/*
|
||||
* MinIO Cloud Storage, (C) 2019 MinIO, Inc.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package disk
|
||||
|
||||
import (
|
||||
"io"
|
||||
"math"
|
||||
"time"
|
||||
|
||||
humanize "github.com/dustin/go-humanize"
|
||||
)
|
||||
|
||||
var speedTestBlockSize = 4 * humanize.MiByte
|
||||
|
||||
// speedTestWrite computes the write speed by writing
|
||||
// `speedTestFileSize` bytes of data to `w` in 4MiB direct-aligned
|
||||
// blocks present in `buf`
|
||||
func speedTestWrite(w io.Writer, buf []byte, size int64) (float64, error) {
|
||||
// Write speedTestFileSize of data and record write speed
|
||||
startTime := time.Now()
|
||||
remaining := size
|
||||
for remaining > 0 {
|
||||
var toWrite int
|
||||
// there's more remaining to write than the buffer can hold
|
||||
if int64(len(buf)) < remaining {
|
||||
toWrite = len(buf)
|
||||
} else { // buffer can hold all there is to write
|
||||
toWrite = int(remaining)
|
||||
}
|
||||
|
||||
written, err := w.Write(buf[:toWrite])
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
remaining = remaining - int64(written)
|
||||
}
|
||||
|
||||
elapsedTime := time.Since(startTime).Seconds()
|
||||
totalWriteMBs := float64(size) / humanize.MiByte
|
||||
writeSpeed := totalWriteMBs / elapsedTime
|
||||
|
||||
return roundToTwoDecimals(writeSpeed), nil
|
||||
}
|
||||
|
||||
// speedTestRead computes the read speed by reading
|
||||
// `speedTestFileSize` bytes from the reader `r` using 4MiB size `buf`
|
||||
func speedTestRead(r io.Reader, buf []byte, size int64) (float64, error) {
|
||||
// Read speedTestFileSize and record read speed
|
||||
startTime := time.Now()
|
||||
remaining := size
|
||||
for remaining > 0 {
|
||||
// reads `speedTestBlockSize` on every read
|
||||
n, err := io.ReadFull(r, buf)
|
||||
if err == io.ErrUnexpectedEOF || err == nil {
|
||||
remaining = remaining - int64(n)
|
||||
continue
|
||||
}
|
||||
|
||||
// Nothing more left to read from the Reader
|
||||
if err == io.EOF {
|
||||
break
|
||||
}
|
||||
// Error while reading from the underlying Reader
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
}
|
||||
|
||||
if remaining > 0 {
|
||||
return 0, io.ErrUnexpectedEOF
|
||||
}
|
||||
|
||||
elapsedTime := time.Since(startTime).Seconds()
|
||||
totalReadMBs := float64(size) / humanize.MiByte
|
||||
readSpeed := totalReadMBs / elapsedTime
|
||||
|
||||
return roundToTwoDecimals(readSpeed), nil
|
||||
}
|
||||
|
||||
func roundToTwoDecimals(num float64) float64 {
|
||||
return math.Round(num*100) / 100
|
||||
}
|
||||
@@ -36,7 +36,7 @@ func main() {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
||||
st, err := madmClnt.ServerDrivesPerfInfo()
|
||||
st, err := madmClnt.ServerDrivesPerfInfo(madmin.DefaultDrivePerfSize)
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
||||
@@ -35,6 +35,8 @@ import (
|
||||
const (
|
||||
// DefaultNetPerfSize - default payload size used for network performance.
|
||||
DefaultNetPerfSize = 100 * humanize.MiByte
|
||||
// DefaultDrivePerfSize - default file size for testing drive performance
|
||||
DefaultDrivePerfSize = 100 * humanize.MiByte
|
||||
)
|
||||
|
||||
// BackendType - represents different backend types.
|
||||
@@ -172,12 +174,16 @@ type ServerDrivesPerfInfo struct {
|
||||
Addr string `json:"addr"`
|
||||
Error string `json:"error,omitempty"`
|
||||
Perf []disk.Performance `json:"perf"`
|
||||
Size int64 `json:"size,omitempty"`
|
||||
}
|
||||
|
||||
// ServerDrivesPerfInfo - Returns drive's read and write performance information
|
||||
func (adm *AdminClient) ServerDrivesPerfInfo() ([]ServerDrivesPerfInfo, error) {
|
||||
func (adm *AdminClient) ServerDrivesPerfInfo(size int64) ([]ServerDrivesPerfInfo, error) {
|
||||
v := url.Values{}
|
||||
v.Set("perfType", string("drive"))
|
||||
|
||||
v.Set("size", strconv.FormatInt(size, 10))
|
||||
|
||||
resp, err := adm.executeMethod("GET", requestData{
|
||||
relPath: "/v1/performance",
|
||||
queryValues: v,
|
||||
|
||||
Reference in New Issue
Block a user