-
Notifications
You must be signed in to change notification settings - Fork 0
/
performance.go
106 lines (88 loc) · 3.02 KB
/
performance.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package performance
import (
"strconv"
"time"
)
type Browser struct {
hasDecimal bool
}
type Performance struct {
start int64
offset int64
hasDecimal bool
Mode LengthMode
}
var (
// Chromium is the Browser value corresponding to Chromium browsers, and try to mimic
// their Performance implementation.
Chromium = NewBrowser(true)
// Gecko is the Browser value corresponding to the firefox browser, and try to mimic
// its Performance implementation. Firefox's performance doesn't return floats with
// decimal precision.
Gecko = NewBrowser(false)
)
// NewBrowser creates a new instance of the Browser struct with the specified decimal precision flag.
func NewBrowser(hasDecimal bool) *Browser {
return &Browser{hasDecimal: hasDecimal}
}
// TimeOrigin returns a string representation of the current time in
// milliseconds, with or without decimal precision depending on the associated Browser instance.
func (b *Browser) TimeOrigin() float64 {
t := time.Now().UnixMilli()
if !b.hasDecimal {
return float64(t)
}
return float64(t) + modes[SingleDigitMode].float()
}
// TimeOriginString returns a string representation of the current time in
// milliseconds, with or without decimal precision depending on the associated Browser instance.
func (b *Browser) TimeOriginString() string {
t := time.Now().UnixMilli()
if !b.hasDecimal {
return strconv.FormatInt(t, 10)
}
f := float64(t) + modes[SingleDigitMode].float()
return strconv.FormatFloat(f, 'f', SingleDigitMode.Index(), 64)
}
// NewPerformance creates a new instance of the Performance struct with the
// specified length mode and the starting time set to the current time.
func (b *Browser) NewPerformance(mode LengthMode) *Performance {
return &Performance{
start: time.Now().UnixMilli(),
hasDecimal: b.hasDecimal,
Mode: mode,
}
}
// Now returns the elapsed time since the start of the Performance instance,
// with or without decimal precision depending on the Browser.
func (p *Performance) Now() float64 {
t := time.Now().UnixMilli()
if !p.hasDecimal {
return float64(t - p.start)
}
return float64(t-p.start) + modes[p.Mode].float()
}
// Add adds the specified offset value to the underlying offset.
func (p *Performance) Add(offset int64) {
p.offset += offset
}
// Min subtracts the specified offset value from the underlying offset.
func (p *Performance) Min(offset int64) {
p.offset -= offset
}
// Current returns a float of the current offset with the random decimal value added to it, to match the Browser implementation.
func (p *Performance) Current() float64 {
if !p.hasDecimal {
return float64(p.offset)
}
return float64(p.offset) + modes[p.Mode].float()
}
// CurrentString returns a string representation of the current offset value,
// including the random decimal value generated to match the Browser implementation.
func (p *Performance) CurrentString() string {
if !p.hasDecimal {
return strconv.FormatInt(p.offset, 10)
}
f := float64(p.offset) + modes[p.Mode].float()
return strconv.FormatFloat(f, 'f', p.Mode.Index(), 64)
}