forked from jf-tech/omniparser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson_test.go
72 lines (62 loc) · 1.71 KB
/
json_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
package json
import (
"bytes"
"io"
"io/ioutil"
"testing"
"github.com/bradleyjkemp/cupaloy"
"github.com/jf-tech/go-corelib/jsons"
"github.com/jf-tech/omniparser"
"github.com/jf-tech/omniparser/extensions/omniv21/samples"
"github.com/jf-tech/omniparser/transformctx"
)
func Test1_Single_Object(t *testing.T) {
cupaloy.SnapshotT(t, jsons.BPJ(samples.SampleTestCommon(
t, "./1_single_object.schema.json", "./1_single_object.input.json")))
}
func Test2_Multiple_Objects(t *testing.T) {
cupaloy.SnapshotT(t, jsons.BPJ(samples.SampleTestCommon(t,
"./2_multiple_objects.schema.json", "./2_multiple_objects.input.json")))
}
func Test3_XPathDynamic(t *testing.T) {
cupaloy.SnapshotT(t, jsons.BPJ(samples.SampleTestCommon(t,
"./3_xpathdynamic.schema.json", "./3_xpathdynamic.input.json")))
}
var benchSchemaFile = "./2_multiple_objects.schema.json"
var benchInputFile = "./2_multiple_objects.input.json"
var benchSchema omniparser.Schema
var benchInput []byte
func init() {
schema, err := ioutil.ReadFile(benchSchemaFile)
if err != nil {
panic(err)
}
benchSchema, err = omniparser.NewSchema("bench", bytes.NewReader(schema))
if err != nil {
panic(err)
}
benchInput, err = ioutil.ReadFile(benchInputFile)
if err != nil {
panic(err)
}
}
// go test -bench=. -benchmem -benchtime=30s
// Benchmark2_Multiple_Objects-8 177285 203819 ns/op 67450 B/op 1575 allocs/op
func Benchmark2_Multiple_Objects(b *testing.B) {
for i := 0; i < b.N; i++ {
transform, err := benchSchema.NewTransform(
"bench", bytes.NewReader(benchInput), &transformctx.Ctx{})
if err != nil {
b.FailNow()
}
for {
_, err = transform.Read()
if err == io.EOF {
break
}
if err != nil {
b.FailNow()
}
}
}
}