forked from grafana/k6
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.go
81 lines (72 loc) · 1.93 KB
/
util.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/*
*
* k6 - a next-generation load testing tool
* Copyright (C) 2016 Load Impact
*
* 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/>.
*
*/
package lib
import (
"strings"
"github.com/loadimpact/k6/lib/types"
)
// Returns the total sum of time taken by the given set of stages.
func SumStages(stages []Stage) (d types.NullDuration) {
for _, stage := range stages {
d.Valid = stage.Duration.Valid
if stage.Duration.Valid {
d.Duration += stage.Duration.Duration
}
}
return d
}
// Splits a string in the form "key=value".
func SplitKV(s string) (key, value string) {
parts := strings.SplitN(s, "=", 2)
if len(parts) == 1 {
return parts[0], ""
}
return parts[0], parts[1]
}
// Lerp is a linear interpolation between two values x and y, returning the value at the point t,
// where t is a fraction in the range [0.0 - 1.0].
func Lerp(x, y int64, t float64) int64 {
return x + int64(t*float64(y-x))
}
// Clampf returns the given value, "clamped" to the range [min, max].
func Clampf(val, min, max float64) float64 {
switch {
case val < min:
return min
case val > max:
return max
default:
return val
}
}
// Returns the maximum value of a and b.
func Max(a, b int64) int64 {
if a > b {
return a
}
return b
}
// Returns the minimum value of a and b.
func Min(a, b int64) int64 {
if a < b {
return a
}
return b
}