-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcodec.go
70 lines (57 loc) · 1.58 KB
/
codec.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
package stopwatch
import (
"errors"
"fmt"
"time"
)
type StopWatchLabels []string
const maxSupportedDuration = ((1 << 26) - 1)
func Encode(labels StopWatchLabels, results *Results) ([]uint32, error) {
labelMap := make(map[string]int)
if len(labels) > 64 {
return nil, errors.New("only up to 64 labels supported")
}
for i := 0; i < len(labels); i++ {
labelMap[labels[i]] = i
}
out := make([]uint32, len(results.Steps))
for i := 0; i < len(results.Steps); i++ {
step := results.Steps[i]
labelIndex, found := labelMap[step.Label]
if !found {
return nil, fmt.Errorf("label %s not found in encode label list", step.Label)
}
dur := step.Duration / time.Microsecond
if dur > maxSupportedDuration {
return nil, fmt.Errorf("label %s duration of %dus exceeds the max support duration %dus", step.Label, dur, maxSupportedDuration)
}
val := uint32(uint32(labelIndex<<26) + uint32(dur))
out[i] = val
}
return out, nil
}
func Decode(labels StopWatchLabels, data []uint32) (*Results, error) {
labelMap := make(map[int]string)
if len(labels) > 64 {
return nil, errors.New("only up to 64 labels supported")
}
for i := 0; i < len(labels); i++ {
labelMap[i] = labels[i]
}
out := &Results{
Steps: make([]Step, len(data)),
}
for i := 0; i < len(data); i++ {
dur := int64(data[i]&maxSupportedDuration) * int64(time.Microsecond)
index := int(data[i] >> 26)
label, found := labelMap[index]
if !found {
return nil, fmt.Errorf("label for index %d not found", index)
}
out.Steps[i] = Step{
Label: label,
Duration: time.Duration(dur),
}
}
return out, nil
}