Skip to content

Commit 77012f8

Browse files
自定义json编解码器
1 parent f5a4b01 commit 77012f8

7 files changed

+1086
-0
lines changed

decode/builder.go

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright 2021 Junebao
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
18+
package decode
19+
20+
import (
21+
"unsafe"
22+
)
23+
24+
type builder struct {
25+
data []byte
26+
// strings.Builder
27+
}
28+
29+
func (b *builder) Write(d []byte) {
30+
b.data = append(b.data, d...)
31+
}
32+
33+
func (b *builder) WriteByte(d byte) error {
34+
b.data = append(b.data, d)
35+
return nil
36+
}
37+
38+
func (b *builder) Bytes() []byte {
39+
return b.data
40+
}
41+
42+
func (b *builder) String() string {
43+
return *(*string)(unsafe.Pointer(&b.data))
44+
}
45+
46+
// func (b *builder) getData() []byte {
47+
// ptr := unsafe.Pointer(b)
48+
// addr := (*strings.Builder)(ptr)
49+
// offset := unsafe.Sizeof(addr)
50+
// return *(*[]byte)(unsafe.Pointer(uintptr(ptr) + offset))
51+
// }

decode/builder_test.go

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* Copyright 2021 Junebao
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
18+
package decode
19+
20+
import (
21+
"bytes"
22+
"github.com/valyala/bytebufferpool"
23+
"strings"
24+
"testing"
25+
)
26+
27+
// 测试多种[]byte拼接方式的优劣
28+
func BenchmarkWrite(b *testing.B) {
29+
length := 1000000000
30+
b.Run("bytes.buffer", func(b *testing.B) {
31+
for i := 0; i < b.N; i++ {
32+
buffer := bytes.NewBuffer([]byte{})
33+
for j := 0; j < length; j++ {
34+
buffer.WriteByte(byte('a' + j))
35+
}
36+
_ = buffer.Bytes()
37+
}
38+
})
39+
b.Run("strings.builder", func(b *testing.B) {
40+
for i := 0; i < b.N; i++ {
41+
build := strings.Builder{}
42+
for j := 0; j < length; j++ {
43+
build.WriteByte(byte('a' + j))
44+
}
45+
_ = []byte(build.String())
46+
}
47+
})
48+
b.Run("decode.builder", func(b *testing.B) {
49+
for i := 0; i < b.N; i++ {
50+
build := builder{}
51+
for j := 0; j < length; j++ {
52+
_ = build.WriteByte(byte('a' + j))
53+
}
54+
_ = build.Bytes()
55+
}
56+
})
57+
b.Run("[]byte", func(b *testing.B) {
58+
for i := 0; i < b.N; i++ {
59+
db := make([]byte, 0)
60+
for j := 0; j < length; j++ {
61+
db = append(db, byte('a'+1))
62+
}
63+
_ = db
64+
}
65+
})
66+
67+
b.Run("buffer pool", func(b *testing.B) {
68+
for i := 0; i < b.N; i++ {
69+
pool := bytebufferpool.Get()
70+
for j := 0; j < length; j++ {
71+
_ = pool.WriteByte(byte('a' + j))
72+
}
73+
_ = pool.Bytes()
74+
}
75+
})
76+
}

decode/jsonnode_test.go

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Copyright 2021 Junebao
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
18+
package decode
19+
20+
import (
21+
"testing"
22+
)
23+
24+
func Test_keyReplace(t *testing.T) {
25+
type args struct {
26+
key string
27+
}
28+
tests := []struct {
29+
name string
30+
args args
31+
want string
32+
}{
33+
{"common", args{key: "article~1a~01~001name"}, "article~01a~001~0001name"},
34+
{"common1", args{key: "01~1"}, "01~01"},
35+
{"common2", args{key: "0101~"}, "0101~"},
36+
{"common3", args{key: "0101/01"}, "0101~101"},
37+
{"common4", args{key: "0101/01~01"}, "0101~101~001"},
38+
{"common5", args{key: "article/name"}, "article~1name"},
39+
}
40+
for _, tt := range tests {
41+
t.Run(tt.name, func(t *testing.T) {
42+
if got := KeyReplace(tt.args.key); got != tt.want {
43+
t.Errorf("keyReplace() = %v, want %v", got, tt.want)
44+
}
45+
})
46+
}
47+
}
48+
49+
func Test_keyRestore(t *testing.T) {
50+
type args struct {
51+
key string
52+
}
53+
tests := []struct {
54+
name string
55+
args args
56+
want string
57+
}{
58+
{"common", args{key: "article~01a~001~0001name"}, "article~1a~01~001name"},
59+
{"common1", args{key: "article~1name"}, "article/name"},
60+
{"common2", args{key: "article_name"}, "article_name"},
61+
{"common3", args{key: "article~1a~001~0001name"}, "article/a~01~001name"},
62+
}
63+
for _, tt := range tests {
64+
t.Run(tt.name, func(t *testing.T) {
65+
if got := KeyRestore(tt.args.key); got != tt.want {
66+
t.Errorf("keyRestore() = %v, want %v", got, tt.want)
67+
}
68+
})
69+
}
70+
}

0 commit comments

Comments
 (0)