-
Notifications
You must be signed in to change notification settings - Fork 11
/
brotli_test.go
171 lines (136 loc) · 3.93 KB
/
brotli_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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package j8a
import (
"bytes"
"fmt"
"github.com/andybalholm/brotli"
"io/ioutil"
"math/rand"
"sync"
"testing"
)
func TestBrotliCompressionRatio(t *testing.T) {
nums := []int{1, 2, 3}
brotlis := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}
for _, i := range nums {
b, _ := ioutil.ReadFile(fmt.Sprintf("./unit/example%d.json", i))
for _, z := range brotlis {
var buf bytes.Buffer
w := brotli.NewWriterLevel(&buf, z)
w.Write(b)
w.Flush()
w.Close()
r := float32(buf.Len()) / float32(len(b))
t.Logf("json size %d, compressed size %d, brotli level %d, compression ratio %v", len(b), buf.Len(), z, r)
}
}
}
//test pool allocation of zipper
func TestBrotliEncoder(t *testing.T) {
//run small loop to ensure pool allocation works
for i := 0; i <= 100; i++ {
json := []byte("{\"routes\": [{\n\t\t\t\"path\": \"/about\",\n\t\t\t\"resource\": \"aboutj8a\"\n\t\t},\n\t\t{\n\t\t\t\"path\": \"/customer\",\n\t\t\t\"resource\": \"customer\",\n\t\t\t\"policy\": \"ab\"\n\t\t}\n\t]}")
br := *BrotliEncode(json)
var want = 111
if len(br) != want {
t.Errorf("brotli compression not working, should be compressed []byte %d for data size %d provided, but got %d", want, len(json), len(br))
}
}
}
func TestBrotliDecoder(t *testing.T) {
for i := 0; i <= 100; i++ {
json := []byte(fmt.Sprintf(`{ "key":"value%d" }`, i))
if c := bytes.Compare(json, *BrotliDecode(*BrotliEncode(json))); c != 0 {
t.Error("brotli data is not equal to original")
}
}
}
func TestBrotliEncodeThenBrotliDecodePoolIntegrity(t *testing.T) {
var wg sync.WaitGroup
for i := 0; i <= 100000; i++ {
json := []byte(fmt.Sprintf(`{ "key":"value %v" }`, rand.Float64()*float64(i)))
wg.Add(1)
go func() {
if c := bytes.Compare(json, *BrotliDecode(*BrotliEncode(json))); c != 0 {
t.Error("brotli decoded data is not equal to original")
}
wg.Done()
}()
}
wg.Wait()
}
func BenchmahkBrotliEncodeNBytes(b *testing.B, n int) {
b.StopTimer()
text := []byte(randSeq(n))
b.StartTimer()
for i := 0; i < b.N; i++ {
BrotliEncode(text)
}
}
func BenchmahkBrotliDecodeNBytes(b *testing.B, n int) {
b.StopTimer()
br := *BrotliEncode([]byte(randSeq(n)))
b.StartTimer()
for i := 0; i < b.N; i++ {
BrotliDecode(br)
}
}
func BenchmarkBrotliEncode128B(b *testing.B) {
BenchmahkBrotliEncodeNBytes(b, 2<<6)
}
func BenchmarkBrotliDecode128B(b *testing.B) {
BenchmahkBrotliDecodeNBytes(b, 2<<6)
}
func BenchmarkBrotliEncode1KB(b *testing.B) {
BenchmahkBrotliEncodeNBytes(b, 2<<9)
}
func BenchmarkBrotliDecode1KB(b *testing.B) {
BenchmahkBrotliDecodeNBytes(b, 2<<9)
}
func BenchmarkBrotliEncode64KB(b *testing.B) {
BenchmahkBrotliEncodeNBytes(b, 2<<15)
}
func BenchmarkBrotliDecode64KB(b *testing.B) {
BenchmahkBrotliDecodeNBytes(b, 2<<15)
}
func BenchmarkBrotliEncode128KB(b *testing.B) {
BenchmahkBrotliEncodeNBytes(b, 2<<16)
}
func BenchmarkBrotliDecode128KB(b *testing.B) {
BenchmahkBrotliDecodeNBytes(b, 2<<16)
}
func BenchmarkBrotliEncode1MB(b *testing.B) {
BenchmahkBrotliEncodeNBytes(b, 2<<19)
}
func BenchmarkBrotliDecode1MB(b *testing.B) {
BenchmahkBrotliDecodeNBytes(b, 2<<19)
}
func BenchmarkBrotliEncode2MB(b *testing.B) {
BenchmahkBrotliEncodeNBytes(b, 2<<20)
}
func BenchmarkBrotliDecode2MB(b *testing.B) {
BenchmahkBrotliDecodeNBytes(b, 2<<20)
}
func BenchmarkBrotliEncode4MB(b *testing.B) {
BenchmahkBrotliEncodeNBytes(b, 2<<21)
}
func BenchmarkBrotliDecode4MB(b *testing.B) {
BenchmahkBrotliDecodeNBytes(b, 2<<21)
}
func BenchmarkBrotliEncode8MB(b *testing.B) {
BenchmahkBrotliEncodeNBytes(b, 2<<22)
}
func BenchmarkBrotliDecode8MB(b *testing.B) {
BenchmahkBrotliDecodeNBytes(b, 2<<22)
}
func BenchmarkBrotliEncode16MB(b *testing.B) {
BenchmahkBrotliEncodeNBytes(b, 2<<23)
}
func BenchmarkBrotliDecode16MB(b *testing.B) {
BenchmahkBrotliDecodeNBytes(b, 2<<23)
}
func BenchmarkBrotliEncode32MB(b *testing.B) {
BenchmahkBrotliEncodeNBytes(b, 2<<24)
}
func BenchmarkBrotliDecode32MB(b *testing.B) {
BenchmahkBrotliDecodeNBytes(b, 2<<24)
}