github.com/minio/madmin-go@v1.7.5/perf-drive.go (about)

     1  //
     2  // MinIO Object Storage (c) 2021 MinIO, Inc.
     3  //
     4  // Licensed under the Apache License, Version 2.0 (the "License");
     5  // you may not use this file except in compliance with the License.
     6  // You may obtain a copy of the License at
     7  //
     8  //      http://www.apache.org/licenses/LICENSE-2.0
     9  //
    10  // Unless required by applicable law or agreed to in writing, software
    11  // distributed under the License is distributed on an "AS IS" BASIS,
    12  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  // See the License for the specific language governing permissions and
    14  // limitations under the License.
    15  //
    16  
    17  package madmin
    18  
    19  import (
    20  	"context"
    21  	"encoding/json"
    22  	"net/http"
    23  	"net/url"
    24  	"strconv"
    25  )
    26  
    27  // DriveSpeedTestResult - result of the drive speed test
    28  type DriveSpeedTestResult struct {
    29  	Version   string      `json:"version"`
    30  	Endpoint  string      `json:"endpoint"`
    31  	DrivePerf []DrivePerf `json:"drivePerf,omitempty"`
    32  
    33  	Error string `json:"string,omitempty"`
    34  }
    35  
    36  // DrivePerf - result of drive speed test on 1 drive mounted at path
    37  type DrivePerf struct {
    38  	Path            string `json:"path"`
    39  	ReadThroughput  uint64 `json:"readThroughput"`
    40  	WriteThroughput uint64 `json:"writeThroughput"`
    41  
    42  	Error string `json:"error,omitempty"`
    43  }
    44  
    45  // DriveSpeedTestOpts provide configurable options for drive speedtest
    46  type DriveSpeedTestOpts struct {
    47  	Serial    bool   // Run speed tests one drive at a time
    48  	BlockSize uint64 // BlockSize for read/write (default 4MiB)
    49  	FileSize  uint64 // Total fileSize to write and read (default 1GiB)
    50  }
    51  
    52  // DriveSpeedtest - perform drive speedtest on the MinIO servers
    53  func (adm *AdminClient) DriveSpeedtest(ctx context.Context, opts DriveSpeedTestOpts) (chan DriveSpeedTestResult, error) {
    54  	queryVals := make(url.Values)
    55  	if opts.Serial {
    56  		queryVals.Set("serial", "true")
    57  	}
    58  	queryVals.Set("blocksize", strconv.FormatUint(opts.BlockSize, 10))
    59  	queryVals.Set("filesize", strconv.FormatUint(opts.FileSize, 10))
    60  	resp, err := adm.executeMethod(ctx,
    61  		http.MethodPost, requestData{
    62  			relPath:     adminAPIPrefix + "/speedtest/drive",
    63  			queryValues: queryVals,
    64  		})
    65  	if err != nil {
    66  		return nil, err
    67  	}
    68  	if resp.StatusCode != http.StatusOK {
    69  		return nil, httpRespToErrorResponse(resp)
    70  	}
    71  	ch := make(chan DriveSpeedTestResult)
    72  	go func() {
    73  		defer closeResponse(resp)
    74  		defer close(ch)
    75  
    76  		dec := json.NewDecoder(resp.Body)
    77  		for {
    78  			var result DriveSpeedTestResult
    79  			if err := dec.Decode(&result); err != nil {
    80  				return
    81  			}
    82  			select {
    83  			case ch <- result:
    84  			case <-ctx.Done():
    85  				return
    86  			}
    87  		}
    88  	}()
    89  	return ch, nil
    90  }