-
Notifications
You must be signed in to change notification settings - Fork 11
/
gzip_test.go
180 lines (142 loc) · 3.8 KB
/
gzip_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
172
173
174
175
176
177
178
179
180
package j8a
import (
"bytes"
"fmt"
"github.com/klauspost/compress/gzip"
"io/ioutil"
"math/rand"
"sync"
"testing"
)
//test pool allocation of zipper
func TestGzipper(t *testing.T) {
//run small loop to ensure pool allocation works
for i := 0; i <= 10; 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]}")
zipped := *Gzip(json)
if c := bytes.Compare(zipped[0:2], gzipMagicBytes); c != 0 {
t.Errorf("gzip format not properly encoded, want %v, got %v", gzipMagicBytes, zipped[0:2])
}
var want = [2]int{100, 120}
if !(len(zipped) >= want[0] && len(zipped) <= want[1]) {
t.Errorf("gzip compression not working")
}
}
}
func TestGzipThenUnzipPoolIntegrity(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, *Gunzip(*Gzip(json))); c != 0 {
t.Error("unzipped data is not equal to original")
}
wg.Done()
}()
}
wg.Wait()
}
func TestGzipCompressionRatio(t *testing.T) {
nums := []int{1, 2, 3}
zips := []int{1, 2, 3, 4, 5, 6, 7, 8, 9}
for _, i := range nums {
b, _ := ioutil.ReadFile(fmt.Sprintf("./unit/example%d.json", i))
for _, z := range zips {
var buf bytes.Buffer
w, _ := gzip.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, gzip level %d, compression ratio %v", len(b), buf.Len(), z, r)
}
}
}
func BenchmahkGzipNBytes(b *testing.B, n int) {
b.StopTimer()
text := []byte(randSeq(n))
//var res *[]byte
b.StartTimer()
for i := 0; i < b.N; i++ {
Gzip(text)
}
//r := float32(binary.Size(*res))/float32(binary.Size(text))
//b.Logf("compression ratio gzip level%d/identity: %g", gzipLevel, r)
}
func BenchmahkGunzipNBytes(b *testing.B, n int) {
b.StopTimer()
zipped := *Gzip([]byte(randSeq(n)))
b.StartTimer()
for i := 0; i < b.N; i++ {
Gunzip(zipped)
}
}
func BenchmarkGzip128B(b *testing.B) {
BenchmahkGzipNBytes(b, 2<<6)
}
func BenchmarkGunzip128B(b *testing.B) {
BenchmahkGunzipNBytes(b, 2<<6)
}
func BenchmarkGzip1KB(b *testing.B) {
BenchmahkGzipNBytes(b, 2<<9)
}
func BenchmarkGunzip1KB(b *testing.B) {
BenchmahkGunzipNBytes(b, 2<<9)
}
func BenchmarkGzip64KB(b *testing.B) {
BenchmahkGzipNBytes(b, 2<<15)
}
func BenchmarkGunzip64KB(b *testing.B) {
BenchmahkGunzipNBytes(b, 2<<15)
}
func BenchmarkGzip128KB(b *testing.B) {
BenchmahkGzipNBytes(b, 2<<16)
}
func BenchmarkGunzip128KB(b *testing.B) {
BenchmahkGunzipNBytes(b, 2<<16)
}
func BenchmarkGzip1MB(b *testing.B) {
BenchmahkGzipNBytes(b, 2<<19)
}
func BenchmarkGunzip1MB(b *testing.B) {
BenchmahkGunzipNBytes(b, 2<<19)
}
func BenchmarkGzip2MB(b *testing.B) {
BenchmahkGzipNBytes(b, 2<<20)
}
func BenchmarkGunzip2MB(b *testing.B) {
BenchmahkGunzipNBytes(b, 2<<20)
}
func BenchmarkGzip4MB(b *testing.B) {
BenchmahkGzipNBytes(b, 2<<21)
}
func BenchmarkGunzip4MB(b *testing.B) {
BenchmahkGunzipNBytes(b, 2<<21)
}
func BenchmarkGzip8MB(b *testing.B) {
BenchmahkGzipNBytes(b, 2<<22)
}
func BenchmarkGunzip8MB(b *testing.B) {
BenchmahkGunzipNBytes(b, 2<<22)
}
func BenchmarkGzip16MB(b *testing.B) {
BenchmahkGzipNBytes(b, 2<<23)
}
func BenchmarkGunzip16MB(b *testing.B) {
BenchmahkGunzipNBytes(b, 2<<23)
}
func BenchmarkGzip32MB(b *testing.B) {
BenchmahkGzipNBytes(b, 2<<24)
}
func BenchmarkGunzip32MB(b *testing.B) {
BenchmahkGunzipNBytes(b, 2<<24)
}
var letters = []rune("{}\":123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
func randSeq(n int) string {
b := make([]rune, n)
for i := range b {
b[i] = letters[rand.Intn(len(letters))]
}
return string(b)
}