-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtimer.go
More file actions
76 lines (64 loc) · 1.86 KB
/
Copy pathtimer.go
File metadata and controls
76 lines (64 loc) · 1.86 KB
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
package search
import (
"fmt"
"time"
"go.k6.io/k6/js/common"
)
// Timer manages scenario timing for sequential benchmark phases.
// Create with search.timer({ duration: "30s", gap: "2s" }).
type Timer struct {
durationSec int
gapSec int
phaseStart int
nextPhase int
started bool
}
// newTimer creates a Timer from a JS config object.
func (m *ModuleInstance) newTimer(config map[string]interface{}) *Timer {
durationStr, _ := config["duration"].(string)
gapStr, _ := config["gap"].(string)
duration, err := time.ParseDuration(durationStr)
if err != nil || duration <= 0 {
common.Throw(m.vu.Runtime(), fmt.Errorf("timer: invalid duration %q", durationStr))
return nil
}
gap, _ := time.ParseDuration(gapStr)
if gap < 0 {
gap = 0
}
return &Timer{
durationSec: int(duration.Seconds()),
gapSec: int(gap.Seconds()),
}
}
// AdvanceAndGet advances to the next phase and returns the startTime string.
func (t *Timer) AdvanceAndGet() string {
t.phaseStart = t.nextPhase
t.nextPhase = t.phaseStart + t.durationSec + t.gapSec
t.started = true
return fmt.Sprintf("%ds", t.phaseStart)
}
// Next is an alias for AdvanceAndGet.
func (t *Timer) Next() string {
return t.AdvanceAndGet()
}
// Get returns the current phase startTime without advancing.
// Use for parallel scenarios that share a phase with the preceding advanceAndGet.
// Auto-advances on first call if no phase has been started.
func (t *Timer) Get() string {
if !t.started {
return t.AdvanceAndGet()
}
return fmt.Sprintf("%ds", t.phaseStart)
}
// Duration returns the phase duration as a string (e.g. "30s").
func (t *Timer) Duration() string {
return fmt.Sprintf("%ds", t.durationSec)
}
// TotalDuration returns the total covering duration as a string (e.g. "70s").
func (t *Timer) TotalDuration() string {
if !t.started {
return "0s"
}
return fmt.Sprintf("%ds", t.nextPhase)
}