-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimulator.go
75 lines (64 loc) · 1.29 KB
/
simulator.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
package ddbrew
import (
"bufio"
"encoding/json"
"errors"
"io"
"strings"
)
type SimulateOpt struct {
Reader io.Reader
Mode DDBMode
}
type SimulateResult struct {
ConsumeRRU *int
ConsumeWRU *int
ConsumeRCU *int
ConsumeWCU *int
TotalItemSize int
}
func Simulate(opt *SimulateOpt) (*SimulateResult, error) {
reader := bufio.NewReader(opt.Reader)
var totalItemSize float64
rusum, wusum := 0, 0
for {
l, err := reader.ReadString('\n')
if err != nil {
if err == io.EOF {
break
}
}
tl := strings.TrimSpace(l)
var pjl map[string]interface{}
err = json.Unmarshal([]byte(tl), &pjl)
if err != nil {
return nil, err
}
itemResult, err := GetItemSizeByJSON(pjl)
if err != nil {
return nil, err
}
if itemResult.Size > WRITE_LIMIT_BYTE_SIZE {
return nil, err
}
totalItemSize = totalItemSize + float64(itemResult.Size)
rusum += itemResult.ReadUnit
wusum += itemResult.WriteUnit
}
switch opt.Mode {
case OnDemand:
return &SimulateResult{
ConsumeWRU: &wusum,
ConsumeRRU: &rusum,
TotalItemSize: int(totalItemSize),
}, nil
case Provisioned:
return &SimulateResult{
ConsumeWCU: &wusum,
ConsumeRCU: &rusum,
TotalItemSize: int(totalItemSize),
}, nil
default:
return nil, errors.New("invalid DynamoDB mode")
}
}