github.com/network-quality/goresponsiveness@v0.0.0-20240129151524-343954285090/rpm/parameters.go (about)

     1  /*
     2   * This file is part of Go Responsiveness.
     3   *
     4   * Go Responsiveness is free software: you can redistribute it and/or modify it under
     5   * the terms of the GNU General Public License as published by the Free Software Foundation,
     6   * either version 2 of the License, or (at your option) any later version.
     7   * Go Responsiveness is distributed in the hope that it will be useful, but WITHOUT ANY
     8   * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
     9   * PARTICULAR PURPOSE. See the GNU General Public License for more details.
    10   *
    11   * You should have received a copy of the GNU General Public License along
    12   * with Go Responsiveness. If not, see <https://www.gnu.org/licenses/>.
    13   */
    14  
    15  package rpm
    16  
    17  import (
    18  	"fmt"
    19  	"time"
    20  
    21  	"github.com/network-quality/goresponsiveness/executor"
    22  	"github.com/network-quality/goresponsiveness/utilities"
    23  )
    24  
    25  type SpecParameters struct {
    26  	TestTimeout      time.Duration // Total test time.
    27  	MovingAvgDist    int
    28  	EvalInterval     time.Duration // How often to reevaluate network conditions.
    29  	TrimmedMeanPct   uint
    30  	StdDevTolerance  float64
    31  	MaxParallelConns int
    32  	ProbeInterval    time.Duration
    33  	ProbeCapacityPct float64
    34  	Percentile       uint
    35  	ExecutionPolicy  executor.ExecutionMethod
    36  }
    37  
    38  func SpecParametersFromArguments(timeout int, mad int, id int, tmp uint, sdt float64, mnp int,
    39  	mps int, ptc float64, p int, executionPolicy executor.ExecutionMethod,
    40  ) (*SpecParameters, error) {
    41  	if timeout <= 0 {
    42  		return nil, fmt.Errorf("cannot specify a 0 or negative timeout for the test")
    43  	}
    44  	if mad <= 0 {
    45  		return nil, fmt.Errorf("cannot specify a 0 or negative moving-average distance for the test")
    46  	}
    47  	if id <= 0 {
    48  		return nil, fmt.Errorf("cannot specify a 0 or negative reevaluation interval for the test")
    49  	}
    50  	if tmp < 0 {
    51  		return nil, fmt.Errorf("cannot specify a negative trimming percentage for the test")
    52  	}
    53  	if sdt < 0 {
    54  		return nil, fmt.Errorf("cannot specify a negative standard-deviation tolerance for the test")
    55  	}
    56  	if mnp <= 0 {
    57  		return nil, fmt.Errorf("cannot specify a 0 or negative maximum number of parallel connections for the test")
    58  	}
    59  	if mps <= 0 {
    60  		return nil, fmt.Errorf("cannot specify a 0 or negative probing interval for the test")
    61  	}
    62  	if ptc <= 0 {
    63  		return nil, fmt.Errorf("cannot specify a 0 or negative probe capacity for the test")
    64  	}
    65  	if p < 1 || p >= 100 {
    66  		return nil, fmt.Errorf("percentile for statistical calculations (%v) is invalid", p)
    67  	}
    68  	testTimeout := time.Second * time.Duration(timeout)
    69  	evalInterval := time.Second * time.Duration(id)
    70  	probeInterval := utilities.PerSecondToInterval(int64(mps))
    71  
    72  	params := SpecParameters{
    73  		TestTimeout: testTimeout, MovingAvgDist: mad,
    74  		EvalInterval: evalInterval, TrimmedMeanPct: tmp, StdDevTolerance: sdt,
    75  		MaxParallelConns: mnp, ProbeInterval: probeInterval, ProbeCapacityPct: ptc,
    76  		Percentile: uint(p), ExecutionPolicy: executionPolicy,
    77  	}
    78  	return &params, nil
    79  }
    80  
    81  func (parameters *SpecParameters) ToString() string {
    82  	return fmt.Sprintf(
    83  		`Timeout:                                     %v,
    84  Moving-Average Distance:                     %v,
    85  Interval Duration:                           %v,
    86  Trimmed-Mean Percentage:                     %v,
    87  Standard-Deviation Tolerance:                %v,
    88  Maximum number of parallel connections:      %v,
    89  Probe Interval:                              %v (derived from given maximum-probes-per-second parameter),
    90  Maximum Percentage Of Throughput For Probes: %v
    91  Execution Policy:                            %v`,
    92  		parameters.TestTimeout, parameters.MovingAvgDist, parameters.EvalInterval, parameters.TrimmedMeanPct,
    93  		parameters.StdDevTolerance, parameters.MaxParallelConns, parameters.ProbeInterval,
    94  		parameters.ProbeCapacityPct, parameters.ExecutionPolicy.ToString(),
    95  	)
    96  }