storj.io/minio@v0.0.0-20230509071714-0cbc90f649b1/pkg/net/health.go (about)

     1  /*
     2   * MinIO Cloud Storage, (C) 2020 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  
    18  package net
    19  
    20  import (
    21  	"github.com/montanaflynn/stats"
    22  )
    23  
    24  // Latency holds latency information for read/write operations to the drive
    25  type Latency struct {
    26  	Avg          float64 `json:"avg_secs,omitempty"`
    27  	Percentile50 float64 `json:"percentile50_secs,omitempty"`
    28  	Percentile90 float64 `json:"percentile90_secs,omitempty"`
    29  	Percentile99 float64 `json:"percentile99_secs,omitempty"`
    30  	Min          float64 `json:"min_secs,omitempty"`
    31  	Max          float64 `json:"max_secs,omitempty"`
    32  }
    33  
    34  // Throughput holds throughput information for read/write operations to the drive
    35  type Throughput struct {
    36  	Avg          float64 `json:"avg_bytes_per_sec,omitempty"`
    37  	Percentile50 float64 `json:"percentile50_bytes_per_sec,omitempty"`
    38  	Percentile90 float64 `json:"percentile90_bytes_per_sec,omitempty"`
    39  	Percentile99 float64 `json:"percentile99_bytes_per_sec,omitempty"`
    40  	Min          float64 `json:"min_bytes_per_sec,omitempty"`
    41  	Max          float64 `json:"max_bytes_per_sec,omitempty"`
    42  }
    43  
    44  // ComputePerfStats takes arrays of Latency & Throughput to compute Statistics
    45  func ComputePerfStats(latencies, throughputs []float64) (Latency, Throughput, error) {
    46  	var avgLatency float64
    47  	var percentile50Latency float64
    48  	var percentile90Latency float64
    49  	var percentile99Latency float64
    50  	var minLatency float64
    51  	var maxLatency float64
    52  
    53  	var avgThroughput float64
    54  	var percentile50Throughput float64
    55  	var percentile90Throughput float64
    56  	var percentile99Throughput float64
    57  	var minThroughput float64
    58  	var maxThroughput float64
    59  	var err error
    60  
    61  	if avgLatency, err = stats.Mean(latencies); err != nil {
    62  		return Latency{}, Throughput{}, err
    63  	}
    64  	if percentile50Latency, err = stats.Percentile(latencies, 50); err != nil {
    65  		return Latency{}, Throughput{}, err
    66  	}
    67  	if percentile90Latency, err = stats.Percentile(latencies, 90); err != nil {
    68  		return Latency{}, Throughput{}, err
    69  	}
    70  	if percentile99Latency, err = stats.Percentile(latencies, 99); err != nil {
    71  		return Latency{}, Throughput{}, err
    72  	}
    73  	if maxLatency, err = stats.Max(latencies); err != nil {
    74  		return Latency{}, Throughput{}, err
    75  	}
    76  	if minLatency, err = stats.Min(latencies); err != nil {
    77  		return Latency{}, Throughput{}, err
    78  	}
    79  	l := Latency{
    80  		Avg:          avgLatency,
    81  		Percentile50: percentile50Latency,
    82  		Percentile90: percentile90Latency,
    83  		Percentile99: percentile99Latency,
    84  		Min:          minLatency,
    85  		Max:          maxLatency,
    86  	}
    87  
    88  	if avgThroughput, err = stats.Mean(throughputs); err != nil {
    89  		return Latency{}, Throughput{}, err
    90  	}
    91  	if percentile50Throughput, err = stats.Percentile(throughputs, 50); err != nil {
    92  		return Latency{}, Throughput{}, err
    93  	}
    94  	if percentile90Throughput, err = stats.Percentile(throughputs, 90); err != nil {
    95  		return Latency{}, Throughput{}, err
    96  	}
    97  	if percentile99Throughput, err = stats.Percentile(throughputs, 99); err != nil {
    98  		return Latency{}, Throughput{}, err
    99  	}
   100  	if maxThroughput, err = stats.Max(throughputs); err != nil {
   101  		return Latency{}, Throughput{}, err
   102  	}
   103  	if minThroughput, err = stats.Min(throughputs); err != nil {
   104  		return Latency{}, Throughput{}, err
   105  	}
   106  	t := Throughput{
   107  		Avg:          avgThroughput,
   108  		Percentile50: percentile50Throughput,
   109  		Percentile90: percentile90Throughput,
   110  		Percentile99: percentile99Throughput,
   111  		Min:          minThroughput,
   112  		Max:          maxThroughput,
   113  	}
   114  
   115  	return l, t, nil
   116  }