-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpeak_example_test.go
60 lines (49 loc) · 1.63 KB
/
peak_example_test.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
// Copyright (c) 2022 Hirotsuna Mizuno. All rights reserved.
// Use of this source code is governed by the MIT license that can be found in
// the LICENSE file.
package peak_test
import (
"fmt"
"time"
"github.com/tunabay/go-peak"
)
func Example_usage() {
// Value that tracks the maximum/minimum in last 1 sec, with the initial
// value 1000.
v := peak.New[uint32](time.Second, 1000)
// Add, Sub, and Set change the value.
time.Sleep(time.Millisecond * 250) // [0.25s]
v.Add(300) // [0.25s] 1300
time.Sleep(time.Millisecond * 250) // [0.50s]
v.Sub(1100) // [0.50s] 200
time.Sleep(time.Millisecond * 250) // [0.75s]
v.Set(900) // [0.75s] 900
time.Sleep(time.Millisecond * 740) // [1.49s]
// Get the current value and maximum/minimum values in last 1 sec.
cur, min, max := v.Get()
fmt.Printf("cur: %3v\n", cur)
fmt.Printf("min: %3v\n", min)
fmt.Printf("max: %3v\n", max)
// Output:
// cur: 900
// min: 200
// max: 900
}
func ExampleValue() {
v := peak.New[uint16](time.Millisecond*500, 100)
v.Sub(50) // 0.0s: 100 -> 50
time.Sleep(time.Millisecond * 200) // 0.0s -> 0.2s
v.Add(30) // 0.2s: 50 -> 80
time.Sleep(time.Millisecond * 200) // 0.2s -> 0.4s
v.Set(250) // 0.4s: 80 -> 250
v.Sub(50) // 0.4s: 250 -> 200
time.Sleep(time.Millisecond * 200) // 0.4s -> 0.6s
cur, min, max := v.Get() // min/max within 0.1s .. 0.5s
fmt.Printf("cur: %3v\n", cur)
fmt.Printf("min: %3v\n", min)
fmt.Printf("max: %3v\n", max)
// Output:
// cur: 200
// min: 80
// max: 250
}