2012-04-29 20:41:13 +10:00
|
|
|
// Copyright 2012 The Go Authors. All rights reserved.
|
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
|
|
package flate
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"bytes"
|
|
|
|
|
"io"
|
|
|
|
|
"io/ioutil"
|
|
|
|
|
"runtime"
|
2012-07-16 12:01:18 +10:00
|
|
|
"strings"
|
2012-04-29 20:41:13 +10:00
|
|
|
"testing"
|
|
|
|
|
)
|
|
|
|
|
|
2012-07-16 12:01:18 +10:00
|
|
|
func TestNlitOutOfRange(t *testing.T) {
|
|
|
|
|
// Trying to decode this bogus flate data, which has a Huffman table
|
|
|
|
|
// with nlit=288, should not panic.
|
|
|
|
|
io.Copy(ioutil.Discard, NewReader(strings.NewReader(
|
|
|
|
|
"\xfc\xfe\x36\xe7\x5e\x1c\xef\xb3\x55\x58\x77\xb6\x56\xb5\x43\xf4"+
|
|
|
|
|
"\x6f\xf2\xd2\xe6\x3d\x99\xa0\x85\x8c\x48\xeb\xf8\xda\x83\x04\x2a"+
|
|
|
|
|
"\x75\xc4\xf8\x0f\x12\x11\xb9\xb4\x4b\x09\xa0\xbe\x8b\x91\x4c")))
|
|
|
|
|
}
|
|
|
|
|
|
2012-05-09 08:57:54 +10:00
|
|
|
const (
|
|
|
|
|
digits = iota
|
|
|
|
|
twain
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var testfiles = []string{
|
|
|
|
|
// Digits is the digits of the irrational number e. Its decimal representation
|
2014-04-29 12:44:40 -04:00
|
|
|
// does not repeat, but there are only 10 possible digits, so it should be
|
2012-05-09 08:57:54 +10:00
|
|
|
// reasonably compressible.
|
|
|
|
|
digits: "../testdata/e.txt",
|
2015-11-24 15:36:32 +01:00
|
|
|
// Twain is Mark Twain's classic English novel.
|
2012-05-09 08:57:54 +10:00
|
|
|
twain: "../testdata/Mark.Twain-Tom.Sawyer.txt",
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func benchmarkDecode(b *testing.B, testfile, level, n int) {
|
2013-09-04 15:31:46 -07:00
|
|
|
b.ReportAllocs()
|
2012-04-29 20:41:13 +10:00
|
|
|
b.StopTimer()
|
|
|
|
|
b.SetBytes(int64(n))
|
2012-05-09 08:57:54 +10:00
|
|
|
buf0, err := ioutil.ReadFile(testfiles[testfile])
|
2012-04-29 20:41:13 +10:00
|
|
|
if err != nil {
|
|
|
|
|
b.Fatal(err)
|
|
|
|
|
}
|
2012-05-09 08:57:54 +10:00
|
|
|
if len(buf0) == 0 {
|
|
|
|
|
b.Fatalf("test file %q has no data", testfiles[testfile])
|
|
|
|
|
}
|
2012-04-29 20:41:13 +10:00
|
|
|
compressed := new(bytes.Buffer)
|
|
|
|
|
w, err := NewWriter(compressed, level)
|
|
|
|
|
if err != nil {
|
|
|
|
|
b.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
for i := 0; i < n; i += len(buf0) {
|
2012-05-09 08:57:54 +10:00
|
|
|
if len(buf0) > n-i {
|
|
|
|
|
buf0 = buf0[:n-i]
|
|
|
|
|
}
|
2013-09-04 15:31:46 -07:00
|
|
|
io.Copy(w, bytes.NewReader(buf0))
|
2012-04-29 20:41:13 +10:00
|
|
|
}
|
|
|
|
|
w.Close()
|
|
|
|
|
buf1 := compressed.Bytes()
|
|
|
|
|
buf0, compressed, w = nil, nil, nil
|
|
|
|
|
runtime.GC()
|
|
|
|
|
b.StartTimer()
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
2013-09-04 15:31:46 -07:00
|
|
|
io.Copy(ioutil.Discard, NewReader(bytes.NewReader(buf1)))
|
2012-04-29 20:41:13 +10:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-05-09 08:57:54 +10:00
|
|
|
// These short names are so that gofmt doesn't break the BenchmarkXxx function
|
|
|
|
|
// bodies below over multiple lines.
|
|
|
|
|
const (
|
|
|
|
|
speed = BestSpeed
|
|
|
|
|
default_ = DefaultCompression
|
|
|
|
|
compress = BestCompression
|
compress/flate: add pure huffman deflater
Add a "HuffmanOnly" compression level, where the input is
only entropy encoded.
The output is fully inflate compatible. Typical compression
is reduction is about 50% of typical level 1 compression, however
the compression time is very stable, and does not vary as much as
nearly as much level 1 compression (or Snappy).
This mode is useful for:
* HTTP compression in a CPU limited environment.
* Entropy encoding Snappy compressed data, for archiving, etc.
* Compression where compression time needs to be predictable.
* Fast network transfer.
Snappy "usually" performs inbetween this and level 1 compression-wise,
but at the same speed as "Huffman", so this is not a replacement,
but a good supplement for Snappy, since it usually can compress
Snappy output further.
This is implemented as level -2, since this would be too much of a
compression reduction to replace level 1.
>go test -bench=Encode -cpu=1
BenchmarkEncodeDigitsHuffman1e4 30000 52334 ns/op 191.08 MB/s
BenchmarkEncodeDigitsHuffman1e5 3000 518343 ns/op 192.92 MB/s
BenchmarkEncodeDigitsHuffman1e6 300 5356884 ns/op 186.68 MB/s
BenchmarkEncodeDigitsSpeed1e4 5000 324214 ns/op 30.84 MB/s
BenchmarkEncodeDigitsSpeed1e5 500 3952614 ns/op 25.30 MB/s
BenchmarkEncodeDigitsSpeed1e6 30 40760350 ns/op 24.53 MB/s
BenchmarkEncodeDigitsDefault1e4 5000 387056 ns/op 25.84 MB/s
BenchmarkEncodeDigitsDefault1e5 300 5950614 ns/op 16.80 MB/s
BenchmarkEncodeDigitsDefault1e6 20 63842195 ns/op 15.66 MB/s
BenchmarkEncodeDigitsCompress1e4 5000 391859 ns/op 25.52 MB/s
BenchmarkEncodeDigitsCompress1e5 300 5707112 ns/op 17.52 MB/s
BenchmarkEncodeDigitsCompress1e6 20 59839465 ns/op 16.71 MB/s
BenchmarkEncodeTwainHuffman1e4 20000 73498 ns/op 136.06 MB/s
BenchmarkEncodeTwainHuffman1e5 2000 595892 ns/op 167.82 MB/s
BenchmarkEncodeTwainHuffman1e6 200 6059016 ns/op 165.04 MB/s
BenchmarkEncodeTwainSpeed1e4 5000 321212 ns/op 31.13 MB/s
BenchmarkEncodeTwainSpeed1e5 500 2823873 ns/op 35.41 MB/s
BenchmarkEncodeTwainSpeed1e6 50 27237864 ns/op 36.71 MB/s
BenchmarkEncodeTwainDefault1e4 3000 454634 ns/op 22.00 MB/s
BenchmarkEncodeTwainDefault1e5 200 6859537 ns/op 14.58 MB/s
BenchmarkEncodeTwainDefault1e6 20 71547405 ns/op 13.98 MB/s
BenchmarkEncodeTwainCompress1e4 3000 462307 ns/op 21.63 MB/s
BenchmarkEncodeTwainCompress1e5 200 7534992 ns/op 13.27 MB/s
BenchmarkEncodeTwainCompress1e6 20 80353365 ns/op 12.45 MB/s
PASS
ok compress/flate 55.333s
Change-Id: I8e12ad13220e50d4cf7ddba6f292333efad61b0c
Reviewed-on: https://go-review.googlesource.com/20982
Reviewed-by: Joe Tsai <joetsai@digital-static.net>
Reviewed-by: Nigel Tao <nigeltao@golang.org>
2016-03-21 14:51:28 +01:00
|
|
|
huffman = HuffmanOnly
|
2012-05-09 08:57:54 +10:00
|
|
|
)
|
2012-04-29 20:41:13 +10:00
|
|
|
|
compress/flate: add pure huffman deflater
Add a "HuffmanOnly" compression level, where the input is
only entropy encoded.
The output is fully inflate compatible. Typical compression
is reduction is about 50% of typical level 1 compression, however
the compression time is very stable, and does not vary as much as
nearly as much level 1 compression (or Snappy).
This mode is useful for:
* HTTP compression in a CPU limited environment.
* Entropy encoding Snappy compressed data, for archiving, etc.
* Compression where compression time needs to be predictable.
* Fast network transfer.
Snappy "usually" performs inbetween this and level 1 compression-wise,
but at the same speed as "Huffman", so this is not a replacement,
but a good supplement for Snappy, since it usually can compress
Snappy output further.
This is implemented as level -2, since this would be too much of a
compression reduction to replace level 1.
>go test -bench=Encode -cpu=1
BenchmarkEncodeDigitsHuffman1e4 30000 52334 ns/op 191.08 MB/s
BenchmarkEncodeDigitsHuffman1e5 3000 518343 ns/op 192.92 MB/s
BenchmarkEncodeDigitsHuffman1e6 300 5356884 ns/op 186.68 MB/s
BenchmarkEncodeDigitsSpeed1e4 5000 324214 ns/op 30.84 MB/s
BenchmarkEncodeDigitsSpeed1e5 500 3952614 ns/op 25.30 MB/s
BenchmarkEncodeDigitsSpeed1e6 30 40760350 ns/op 24.53 MB/s
BenchmarkEncodeDigitsDefault1e4 5000 387056 ns/op 25.84 MB/s
BenchmarkEncodeDigitsDefault1e5 300 5950614 ns/op 16.80 MB/s
BenchmarkEncodeDigitsDefault1e6 20 63842195 ns/op 15.66 MB/s
BenchmarkEncodeDigitsCompress1e4 5000 391859 ns/op 25.52 MB/s
BenchmarkEncodeDigitsCompress1e5 300 5707112 ns/op 17.52 MB/s
BenchmarkEncodeDigitsCompress1e6 20 59839465 ns/op 16.71 MB/s
BenchmarkEncodeTwainHuffman1e4 20000 73498 ns/op 136.06 MB/s
BenchmarkEncodeTwainHuffman1e5 2000 595892 ns/op 167.82 MB/s
BenchmarkEncodeTwainHuffman1e6 200 6059016 ns/op 165.04 MB/s
BenchmarkEncodeTwainSpeed1e4 5000 321212 ns/op 31.13 MB/s
BenchmarkEncodeTwainSpeed1e5 500 2823873 ns/op 35.41 MB/s
BenchmarkEncodeTwainSpeed1e6 50 27237864 ns/op 36.71 MB/s
BenchmarkEncodeTwainDefault1e4 3000 454634 ns/op 22.00 MB/s
BenchmarkEncodeTwainDefault1e5 200 6859537 ns/op 14.58 MB/s
BenchmarkEncodeTwainDefault1e6 20 71547405 ns/op 13.98 MB/s
BenchmarkEncodeTwainCompress1e4 3000 462307 ns/op 21.63 MB/s
BenchmarkEncodeTwainCompress1e5 200 7534992 ns/op 13.27 MB/s
BenchmarkEncodeTwainCompress1e6 20 80353365 ns/op 12.45 MB/s
PASS
ok compress/flate 55.333s
Change-Id: I8e12ad13220e50d4cf7ddba6f292333efad61b0c
Reviewed-on: https://go-review.googlesource.com/20982
Reviewed-by: Joe Tsai <joetsai@digital-static.net>
Reviewed-by: Nigel Tao <nigeltao@golang.org>
2016-03-21 14:51:28 +01:00
|
|
|
func BenchmarkDecodeDigitsHuffman1e4(b *testing.B) { benchmarkDecode(b, digits, huffman, 1e4) }
|
|
|
|
|
func BenchmarkDecodeDigitsHuffman1e5(b *testing.B) { benchmarkDecode(b, digits, huffman, 1e5) }
|
|
|
|
|
func BenchmarkDecodeDigitsHuffman1e6(b *testing.B) { benchmarkDecode(b, digits, huffman, 1e6) }
|
2012-05-09 08:57:54 +10:00
|
|
|
func BenchmarkDecodeDigitsSpeed1e4(b *testing.B) { benchmarkDecode(b, digits, speed, 1e4) }
|
|
|
|
|
func BenchmarkDecodeDigitsSpeed1e5(b *testing.B) { benchmarkDecode(b, digits, speed, 1e5) }
|
|
|
|
|
func BenchmarkDecodeDigitsSpeed1e6(b *testing.B) { benchmarkDecode(b, digits, speed, 1e6) }
|
|
|
|
|
func BenchmarkDecodeDigitsDefault1e4(b *testing.B) { benchmarkDecode(b, digits, default_, 1e4) }
|
|
|
|
|
func BenchmarkDecodeDigitsDefault1e5(b *testing.B) { benchmarkDecode(b, digits, default_, 1e5) }
|
|
|
|
|
func BenchmarkDecodeDigitsDefault1e6(b *testing.B) { benchmarkDecode(b, digits, default_, 1e6) }
|
|
|
|
|
func BenchmarkDecodeDigitsCompress1e4(b *testing.B) { benchmarkDecode(b, digits, compress, 1e4) }
|
|
|
|
|
func BenchmarkDecodeDigitsCompress1e5(b *testing.B) { benchmarkDecode(b, digits, compress, 1e5) }
|
|
|
|
|
func BenchmarkDecodeDigitsCompress1e6(b *testing.B) { benchmarkDecode(b, digits, compress, 1e6) }
|
compress/flate: add pure huffman deflater
Add a "HuffmanOnly" compression level, where the input is
only entropy encoded.
The output is fully inflate compatible. Typical compression
is reduction is about 50% of typical level 1 compression, however
the compression time is very stable, and does not vary as much as
nearly as much level 1 compression (or Snappy).
This mode is useful for:
* HTTP compression in a CPU limited environment.
* Entropy encoding Snappy compressed data, for archiving, etc.
* Compression where compression time needs to be predictable.
* Fast network transfer.
Snappy "usually" performs inbetween this and level 1 compression-wise,
but at the same speed as "Huffman", so this is not a replacement,
but a good supplement for Snappy, since it usually can compress
Snappy output further.
This is implemented as level -2, since this would be too much of a
compression reduction to replace level 1.
>go test -bench=Encode -cpu=1
BenchmarkEncodeDigitsHuffman1e4 30000 52334 ns/op 191.08 MB/s
BenchmarkEncodeDigitsHuffman1e5 3000 518343 ns/op 192.92 MB/s
BenchmarkEncodeDigitsHuffman1e6 300 5356884 ns/op 186.68 MB/s
BenchmarkEncodeDigitsSpeed1e4 5000 324214 ns/op 30.84 MB/s
BenchmarkEncodeDigitsSpeed1e5 500 3952614 ns/op 25.30 MB/s
BenchmarkEncodeDigitsSpeed1e6 30 40760350 ns/op 24.53 MB/s
BenchmarkEncodeDigitsDefault1e4 5000 387056 ns/op 25.84 MB/s
BenchmarkEncodeDigitsDefault1e5 300 5950614 ns/op 16.80 MB/s
BenchmarkEncodeDigitsDefault1e6 20 63842195 ns/op 15.66 MB/s
BenchmarkEncodeDigitsCompress1e4 5000 391859 ns/op 25.52 MB/s
BenchmarkEncodeDigitsCompress1e5 300 5707112 ns/op 17.52 MB/s
BenchmarkEncodeDigitsCompress1e6 20 59839465 ns/op 16.71 MB/s
BenchmarkEncodeTwainHuffman1e4 20000 73498 ns/op 136.06 MB/s
BenchmarkEncodeTwainHuffman1e5 2000 595892 ns/op 167.82 MB/s
BenchmarkEncodeTwainHuffman1e6 200 6059016 ns/op 165.04 MB/s
BenchmarkEncodeTwainSpeed1e4 5000 321212 ns/op 31.13 MB/s
BenchmarkEncodeTwainSpeed1e5 500 2823873 ns/op 35.41 MB/s
BenchmarkEncodeTwainSpeed1e6 50 27237864 ns/op 36.71 MB/s
BenchmarkEncodeTwainDefault1e4 3000 454634 ns/op 22.00 MB/s
BenchmarkEncodeTwainDefault1e5 200 6859537 ns/op 14.58 MB/s
BenchmarkEncodeTwainDefault1e6 20 71547405 ns/op 13.98 MB/s
BenchmarkEncodeTwainCompress1e4 3000 462307 ns/op 21.63 MB/s
BenchmarkEncodeTwainCompress1e5 200 7534992 ns/op 13.27 MB/s
BenchmarkEncodeTwainCompress1e6 20 80353365 ns/op 12.45 MB/s
PASS
ok compress/flate 55.333s
Change-Id: I8e12ad13220e50d4cf7ddba6f292333efad61b0c
Reviewed-on: https://go-review.googlesource.com/20982
Reviewed-by: Joe Tsai <joetsai@digital-static.net>
Reviewed-by: Nigel Tao <nigeltao@golang.org>
2016-03-21 14:51:28 +01:00
|
|
|
func BenchmarkDecodeTwainHuffman1e4(b *testing.B) { benchmarkDecode(b, twain, huffman, 1e4) }
|
|
|
|
|
func BenchmarkDecodeTwainHuffman1e5(b *testing.B) { benchmarkDecode(b, twain, huffman, 1e5) }
|
|
|
|
|
func BenchmarkDecodeTwainHuffman1e6(b *testing.B) { benchmarkDecode(b, twain, huffman, 1e6) }
|
2012-05-09 08:57:54 +10:00
|
|
|
func BenchmarkDecodeTwainSpeed1e4(b *testing.B) { benchmarkDecode(b, twain, speed, 1e4) }
|
|
|
|
|
func BenchmarkDecodeTwainSpeed1e5(b *testing.B) { benchmarkDecode(b, twain, speed, 1e5) }
|
|
|
|
|
func BenchmarkDecodeTwainSpeed1e6(b *testing.B) { benchmarkDecode(b, twain, speed, 1e6) }
|
|
|
|
|
func BenchmarkDecodeTwainDefault1e4(b *testing.B) { benchmarkDecode(b, twain, default_, 1e4) }
|
|
|
|
|
func BenchmarkDecodeTwainDefault1e5(b *testing.B) { benchmarkDecode(b, twain, default_, 1e5) }
|
|
|
|
|
func BenchmarkDecodeTwainDefault1e6(b *testing.B) { benchmarkDecode(b, twain, default_, 1e6) }
|
|
|
|
|
func BenchmarkDecodeTwainCompress1e4(b *testing.B) { benchmarkDecode(b, twain, compress, 1e4) }
|
|
|
|
|
func BenchmarkDecodeTwainCompress1e5(b *testing.B) { benchmarkDecode(b, twain, compress, 1e5) }
|
|
|
|
|
func BenchmarkDecodeTwainCompress1e6(b *testing.B) { benchmarkDecode(b, twain, compress, 1e6) }
|