-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser_test.go
155 lines (148 loc) · 3.61 KB
/
parser_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
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package tdat
import (
"bytes"
"encoding/json"
"fmt"
"github.com/cvilsmeier/tdat/assert"
"io/ioutil"
"strconv"
"strings"
"testing"
)
func TestParse(t *testing.T) {
fis, err := ioutil.ReadDir("testdata")
assert.Truef(t, err == nil, "err was %s", err)
for _, fi := range fis {
fname := fi.Name()
if !strings.HasPrefix(fname, "parser_") {
break
}
t.Run(fname, func(t *testing.T) {
buf, err := ioutil.ReadFile("testdata/" + fname)
assert.Truef(t, err == nil, "err was %s", err)
all := string(buf)
i := strings.Index(all, "---EOF---")
assert.Truef(t, i >= 0, "i was %d", i)
input := all[:i]
exp := strings.TrimSpace(all[i+9:])
// parse input
model, err := ParseFromString(input)
// stringify parse result
act := ""
if model != nil {
for _, table := range model.Tables {
act += stringifyTable(table)
}
}
if err != nil {
act += err.Error()
}
act = strings.TrimSpace(act)
assert.EqStr(t, exp, act)
})
}
}
func stringifyTable(table *Table) string {
str := fmt.Sprintf("table %q\n", table.Name)
for _, col := range table.Columns {
str += fmt.Sprintf(" col %q(%c)\n", col.Name, col.Type)
}
for rowIndex, row := range table.Rows {
str += fmt.Sprintf("row %d\n", rowIndex+1)
for _, val := range row.Values {
if val.Null {
str += fmt.Sprintf(" val null(%c)\n", val.Type)
} else {
valText := ""
switch val.Type {
case IntValue:
valText = fmt.Sprintf("%d", val.AsInt)
case FloatValue:
valText = fmt.Sprintf("%f", val.AsFloat)
case BoolValue:
valText = fmt.Sprintf("%t", val.AsBool)
case StringValue:
valText = fmt.Sprintf("%s", val.AsString)
case TimeValue:
valText = fmt.Sprintf("%s", val.AsTime)
default:
panic("wrong type")
}
str += fmt.Sprintf(" val %s(%c)\n", valText, val.Type)
}
}
}
return str
}
func BenchmarkAllocateModel(b *testing.B) {
b.Skip()
rowCount := 200 * 1000
rows := make([]*Row, 0)
for i := 0; i < rowCount; i++ {
id, _ := strconv.ParseInt("1", 10, 64)
idValue := &Value{Type: IntValue, AsInt: id}
rate, _ := strconv.ParseFloat("13000.12", 64)
rateValue := &Value{Type: FloatValue, AsFloat: rate}
flag, _ := strconv.ParseBool("true")
flagValue := &Value{Type: BoolValue, AsBool: flag}
nameValue := &Value{Type: StringValue, AsString: "joe"}
row := &Row{[]*Value{idValue, rateValue, flagValue, nameValue}}
rows = append(rows, row)
}
}
func BenchmarkParseValue(b *testing.B) {
b.Skip()
p := &parser{}
rowCount := 100 * 1000
for i := 0; i < rowCount; i++ {
p.parseValue(IntValue, "13")
p.parseValue(FloatValue, "1300.13")
p.parseValue(BoolValue, "true")
p.parseValue(StringValue, "lorem ipsum")
p.parseValue(TimeValue, "2017-12-12T10:00:00.113")
}
}
func BenchmarkParseTdat(t *testing.B) {
rowCount := 100 * 1000
var input string
{
b := &bytes.Buffer{}
b.WriteString("persons\n")
b.WriteString("|id:i|rate:f|flag:b|name:s\n")
for i := 0; i < rowCount; i++ {
b.WriteString("|1|13000.00|true|\"joe\"\n")
}
input = string(b.Bytes())
}
t.ResetTimer()
m, err := ParseFromString(input)
if err != nil {
panic(err)
}
if m == nil {
panic("no model")
}
}
func BenchmarkParseJson(t *testing.B) {
rowCount := 100 * 1000
var input []byte
{
b := &bytes.Buffer{}
b.WriteString("{\"persons\":[")
for i := 0; i < rowCount; i++ {
if i > 0 {
b.WriteString(",")
}
b.WriteString("{\"id\":1,\"rate\":13000.00,\"flag\":true,\"name\":\"joe\"}")
}
b.WriteString("]}")
input = b.Bytes()
}
t.ResetTimer()
model := map[string]interface{}{}
t.ResetTimer()
err := json.Unmarshal(input, &model)
if err != nil {
panic(err)
}
}