-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathtesting.go
196 lines (160 loc) · 5.75 KB
/
testing.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*
* Copyright (c) 2013 Zhen, LLC. http://zhen.io. All rights reserved.
* Use of this source code is governed by the Apache 2.0 license.
*
*/
package encoding
import (
"testing"
"os"
"io"
"bytes"
"log"
"time"
"compress/gzip"
"compress/lzw"
"runtime/pprof"
"code.google.com/p/snappy-go/snappy"
)
func TestCodec(codec Integer, data []int32, sizes []int, t *testing.T) {
for _, k := range sizes {
if k > len(data) {
continue
}
log.Printf("encoding/TestCodec: Testing with %d integers\n", k)
now := time.Now()
compressed := Compress(codec, data, k)
log.Printf("encoding/TestCodec: Compressed %d integers from %d bytes to %d bytes in %d ns\n", k, k*4, len(compressed)*4, time.Since(now).Nanoseconds())
recovered := Uncompress(codec, compressed, k)
log.Printf("encoding/TestCodec: Uncompressed from %d bytes to %d bytes in %d ns\n", len(compressed)*4, len(recovered)*4, time.Since(now).Nanoseconds())
for i := 0; i < k; i++ {
if data[i] != recovered[i] {
t.Fatalf("encoding/TestCodec: Problem recovering. index = %d, data = %d, recovered = %d, original length = %d, recovered length = %d\n", i, data[i], recovered[i], k, len(recovered))
}
}
}
}
func TestCodecPprof(codec Integer, data []int32, sizes []int, t *testing.T) {
for _, k := range sizes {
if k > len(data) {
continue
}
f, e := os.Create("cpu.compress.prof")
if e != nil {
log.Fatal(e)
}
defer f.Close()
now := time.Now()
pprof.StartCPUProfile(f)
compressed := Compress(codec, data, k)
pprof.StopCPUProfile()
log.Printf("encoding/TestCodecPprof: Compressed %d integers from %d bytes to %d bytes in %d ns\n", k, k*4, len(compressed)*4, time.Since(now).Nanoseconds())
f2, e2 := os.Create("cpu.uncompress.prof")
if e2 != nil {
log.Fatal(e2)
}
defer f2.Close()
log.Printf("encoding/TestCodecPprof: Testing decompression\n")
now = time.Now()
pprof.StartCPUProfile(f2)
recovered := Uncompress(codec, compressed, k)
pprof.StopCPUProfile()
log.Printf("encoding/TestCodecPprof: Uncompressed from %d bytes to %d bytes in %d ns\n", len(compressed)*4, len(recovered)*4, time.Since(now).Nanoseconds())
for i := 0; i < k; i++ {
if data[i] != recovered[i] {
t.Fatalf("encoding/TestCodecPprof: Problem recovering. index = %d, data = %d, recovered = %d, original length = %d, recovered length = %d\n", i, data[i], recovered[i], k, len(recovered))
}
}
}
}
func BenchmarkCompress(codec Integer, data []int32, b *testing.B) {
k := CeilBy(b.N, 128)
b.ResetTimer()
now := time.Now()
compressed := Compress(codec, data, k)
b.StopTimer()
log.Printf("encoding/BenchmarkCompress: Compressed %d integers from %d bytes to %d bytes in %d ns\n", k, k*4, len(compressed)*4, time.Since(now).Nanoseconds())
}
func BenchmarkUncompress(codec Integer, data []int32, b *testing.B) {
k := CeilBy(b.N, 128)
compressed := Compress(codec, data, k)
b.ResetTimer()
recovered := Uncompress(codec, compressed, k)
b.StopTimer()
log.Printf("encoding/BenchmarkUncompress: Uncompressed from %d bytes to %d bytes\n", len(compressed)*4, len(recovered)*4)
}
func Compress(codec Integer, data []int32, length int) []int32 {
compressed := make([]int32, length*2)
inpos := NewCursor()
outpos := NewCursor()
codec.Compress(data, inpos, length, compressed, outpos)
compressed = compressed[:outpos.Get()]
return compressed
}
func Uncompress(codec Integer, data []int32, length int) []int32 {
recovered := make([]int32, length)
rinpos := NewCursor()
routpos := NewCursor()
codec.Uncompress(data, rinpos, len(data), recovered, routpos)
recovered = recovered[:routpos.Get()]
return recovered
}
func RunTestGzip(data []byte, t *testing.T) {
log.Printf("encoding/RunTestGzip: Testing comprssion Gzip\n")
var compressed bytes.Buffer
w := gzip.NewWriter(&compressed)
defer w.Close()
now := time.Now()
w.Write(data)
cl := compressed.Len()
log.Printf("encoding/RunTestGzip: Compressed from %d bytes to %d bytes in %d ns\n", len(data), cl, time.Since(now).Nanoseconds())
recovered := make([]byte, len(data))
r, _ := gzip.NewReader(&compressed)
defer r.Close()
total := 0
n := 100
var err error = nil
for err != io.EOF && n != 0 {
n, err = r.Read(recovered[total:])
total += n
}
log.Printf("encoding/RunTestGzip: Uncompressed from %d bytes to %d bytes in %d ns\n", cl, len(recovered), time.Since(now).Nanoseconds())
}
func RunTestLZW(data []byte, t *testing.T) {
log.Printf("encoding/RunTestLZW: Testing comprssion LZW\n")
var compressed bytes.Buffer
w := lzw.NewWriter(&compressed, lzw.MSB, 8)
defer w.Close()
now := time.Now()
w.Write(data)
cl := compressed.Len()
log.Printf("encoding/RunTestLZW: Compressed from %d bytes to %d bytes in %d ns\n", len(data), cl, time.Since(now).Nanoseconds())
recovered := make([]byte, len(data))
r := lzw.NewReader(&compressed, lzw.MSB, 8)
defer r.Close()
total := 0
n := 100
var err error = nil
for err != io.EOF && n != 0 {
n, err = r.Read(recovered[total:])
total += n
}
log.Printf("encoding/RunTestLZW: Uncompressed from %d bytes to %d bytes in %d ns\n", cl, len(recovered), time.Since(now).Nanoseconds())
}
func RunTestSnappy(data []byte, t *testing.T) {
log.Printf("encoding/RunTestSnappy: Testing comprssion Snappy\n")
now := time.Now()
e, err := snappy.Encode(nil, data)
if err != nil {
t.Fatalf("encoding/RunTestSnappy: encoding error: %v\n", err)
}
log.Printf("encoding/RunTestSnappy: Compressed from %d bytes to %d bytes in %d ns\n", len(data), len(e), time.Since(now).Nanoseconds())
d, err := snappy.Decode(nil, e)
if err != nil {
t.Fatalf("encoding/RunTestSnappy: decoding error: %v\n", err)
}
log.Printf("encoding/RunTestSnappy: Uncompressed from %d bytes to %d bytes in %d ns\n", len(e), len(d), time.Since(now).Nanoseconds())
if !bytes.Equal(data, d) {
t.Fatalf("encoding/RunTestSnappy: roundtrip mismatch\n")
}
}