| 1 |
// Copyright 2012 The Go Authors. All rights reserved. |
| 2 |
// Use of this source code is governed by a BSD-style |
| 3 |
// license that can be found in the LICENSE file. |
| 4 |
|
| 5 |
package flate |
| 6 |
|
| 7 |
import ( |
| 8 |
"bytes" |
| 9 |
"io" |
| 10 |
"os" |
| 11 |
"runtime" |
| 12 |
"strings" |
| 13 |
"testing" |
| 14 |
) |
| 15 |
|
| 16 |
func TestNlitOutOfRange(t *testing.T) { |
| 17 |
// Trying to decode this bogus flate data, which has a Huffman table |
| 18 |
// with nlit=288, should not panic. |
| 19 |
io.Copy(io.Discard, NewReader(strings.NewReader( |
| 20 |
"\xfc\xfe\x36\xe7\x5e\x1c\xef\xb3\x55\x58\x77\xb6\x56\xb5\x43\xf4"+ |
| 21 |
"\x6f\xf2\xd2\xe6\x3d\x99\xa0\x85\x8c\x48\xeb\xf8\xda\x83\x04\x2a"+ |
| 22 |
"\x75\xc4\xf8\x0f\x12\x11\xb9\xb4\x4b\x09\xa0\xbe\x8b\x91\x4c"))) |
| 23 |
} |
| 24 |
|
| 25 |
const ( |
| 26 |
digits = iota |
| 27 |
twain |
| 28 |
random |
| 29 |
) |
| 30 |
|
| 31 |
var testfiles = []string{ |
| 32 |
// Digits is the digits of the irrational number e. Its decimal representation |
| 33 |
// does not repeat, but there are only 10 possible digits, so it should be |
| 34 |
// reasonably compressible. |
| 35 |
digits: "../testdata/e.txt", |
| 36 |
// Twain is Project Gutenberg's edition of Mark Twain's classic English novel. |
| 37 |
twain: "../testdata/Mark.Twain-Tom.Sawyer.txt", |
| 38 |
// Random bytes |
| 39 |
random: "../testdata/sharnd.out", |
| 40 |
} |
| 41 |
|
| 42 |
func benchmarkDecode(b *testing.B, testfile, level, n int) { |
| 43 |
b.ReportAllocs() |
| 44 |
b.StopTimer() |
| 45 |
b.SetBytes(int64(n)) |
| 46 |
buf0, err := os.ReadFile(testfiles[testfile]) |
| 47 |
if err != nil { |
| 48 |
b.Fatal(err) |
| 49 |
} |
| 50 |
if len(buf0) == 0 { |
| 51 |
b.Fatalf("test file %q has no data", testfiles[testfile]) |
| 52 |
} |
| 53 |
compressed := new(bytes.Buffer) |
| 54 |
w, err := NewWriter(compressed, level) |
| 55 |
if err != nil { |
| 56 |
b.Fatal(err) |
| 57 |
} |
| 58 |
for i := 0; i < n; i += len(buf0) { |
| 59 |
if len(buf0) > n-i { |
| 60 |
buf0 = buf0[:n-i] |
| 61 |
} |
| 62 |
io.Copy(w, bytes.NewReader(buf0)) |
| 63 |
} |
| 64 |
w.Close() |
| 65 |
buf1 := compressed.Bytes() |
| 66 |
buf0, compressed, w = nil, nil, nil |
| 67 |
r := NewReader(bytes.NewReader(buf1)) |
| 68 |
res := r.(Resetter) |
| 69 |
runtime.GC() |
| 70 |
b.StartTimer() |
| 71 |
|
| 72 |
for i := 0; i < b.N; i++ { |
| 73 |
_ = res.Reset(bytes.NewReader(buf1), nil) |
| 74 |
_, _ = io.Copy(io.Discard, r) |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
// These short names are so that gofmt doesn't break the BenchmarkXxx function |
| 79 |
// bodies below over multiple lines. |
| 80 |
const ( |
| 81 |
constant = ConstantCompression |
| 82 |
speed = BestSpeed |
| 83 |
default_ = DefaultCompression |
| 84 |
compress = BestCompression |
| 85 |
oneK = -1024 |
| 86 |
) |
| 87 |
|
| 88 |
func BenchmarkDecodeDigitsSpeed1e4(b *testing.B) { benchmarkDecode(b, digits, speed, 1e4) } |
| 89 |
func BenchmarkDecodeDigitsSpeed1e5(b *testing.B) { benchmarkDecode(b, digits, speed, 1e5) } |
| 90 |
func BenchmarkDecodeDigitsSpeed1e6(b *testing.B) { benchmarkDecode(b, digits, speed, 1e6) } |
| 91 |
func BenchmarkDecodeDigitsDefault1e4(b *testing.B) { benchmarkDecode(b, digits, default_, 1e4) } |
| 92 |
func BenchmarkDecodeDigitsDefault1e5(b *testing.B) { benchmarkDecode(b, digits, default_, 1e5) } |
| 93 |
func BenchmarkDecodeDigitsDefault1e6(b *testing.B) { benchmarkDecode(b, digits, default_, 1e6) } |
| 94 |
func BenchmarkDecodeDigitsCompress1e4(b *testing.B) { benchmarkDecode(b, digits, compress, 1e4) } |
| 95 |
func BenchmarkDecodeDigitsCompress1e5(b *testing.B) { benchmarkDecode(b, digits, compress, 1e5) } |
| 96 |
func BenchmarkDecodeDigitsCompress1e6(b *testing.B) { benchmarkDecode(b, digits, compress, 1e6) } |
| 97 |
func BenchmarkDecodeTwainSpeed1e4(b *testing.B) { benchmarkDecode(b, twain, speed, 1e4) } |
| 98 |
func BenchmarkDecodeTwainSpeed1e5(b *testing.B) { benchmarkDecode(b, twain, speed, 1e5) } |
| 99 |
func BenchmarkDecodeTwainSpeed1e6(b *testing.B) { benchmarkDecode(b, twain, speed, 1e6) } |
| 100 |
func BenchmarkDecodeTwainDefault1e4(b *testing.B) { benchmarkDecode(b, twain, default_, 1e4) } |
| 101 |
func BenchmarkDecodeTwainDefault1e5(b *testing.B) { benchmarkDecode(b, twain, default_, 1e5) } |
| 102 |
func BenchmarkDecodeTwainDefault1e6(b *testing.B) { benchmarkDecode(b, twain, default_, 1e6) } |
| 103 |
func BenchmarkDecodeTwainCompress1e4(b *testing.B) { benchmarkDecode(b, twain, compress, 1e4) } |
| 104 |
func BenchmarkDecodeTwainCompress1e5(b *testing.B) { benchmarkDecode(b, twain, compress, 1e5) } |
| 105 |
func BenchmarkDecodeTwainCompress1e6(b *testing.B) { benchmarkDecode(b, twain, compress, 1e6) } |
| 106 |
func BenchmarkDecodeRandomSpeed1e4(b *testing.B) { benchmarkDecode(b, random, speed, 1e4) } |
| 107 |
func BenchmarkDecodeRandomSpeed1e5(b *testing.B) { benchmarkDecode(b, random, speed, 1e5) } |
| 108 |
func BenchmarkDecodeRandomSpeed1e6(b *testing.B) { benchmarkDecode(b, random, speed, 1e6) } |