compress
+ string
This commit is contained in:
parent
b183cec51f
commit
62f3644ba4
@ -7,7 +7,15 @@ import (
|
|||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
)
|
)
|
||||||
|
|
||||||
func CompressDataGzip(data []byte) []byte {
|
func CompressDataGzip(str string) []byte {
|
||||||
|
return CompressDataGzipByte([]byte(str))
|
||||||
|
}
|
||||||
|
|
||||||
|
func CompressDataGzipLevel(str string, level int) ([]byte, error) {
|
||||||
|
return CompressDataGzipLevelByte([]byte(str), level)
|
||||||
|
}
|
||||||
|
|
||||||
|
func CompressDataGzipByte(data []byte) []byte {
|
||||||
var b bytes.Buffer
|
var b bytes.Buffer
|
||||||
w := gzip.NewWriter(&b)
|
w := gzip.NewWriter(&b)
|
||||||
|
|
||||||
@ -16,7 +24,7 @@ func CompressDataGzip(data []byte) []byte {
|
|||||||
return b.Bytes()
|
return b.Bytes()
|
||||||
}
|
}
|
||||||
|
|
||||||
func CompressDataGzipLevel(data []byte, level int) ([]byte, error) {
|
func CompressDataGzipLevelByte(data []byte, level int) ([]byte, error) {
|
||||||
var b bytes.Buffer
|
var b bytes.Buffer
|
||||||
w := gzip.NewWriter(&b)
|
w := gzip.NewWriter(&b)
|
||||||
|
|
||||||
|
@ -11,7 +11,7 @@ func TestCompress(t *testing.T) {
|
|||||||
|
|
||||||
t.Logf("original length : %d \r\n", len(jsonStr))
|
t.Logf("original length : %d \r\n", len(jsonStr))
|
||||||
start := time.Now()
|
start := time.Now()
|
||||||
bytes := CompressDataGzip([]byte(jsonStr))
|
bytes := CompressDataGzip(jsonStr)
|
||||||
elapsed := time.Since(start)
|
elapsed := time.Since(start)
|
||||||
t.Logf("compress length : %d \r\n", len(bytes))
|
t.Logf("compress length : %d \r\n", len(bytes))
|
||||||
t.Log(elapsed)
|
t.Log(elapsed)
|
||||||
@ -25,7 +25,7 @@ func TestCompressLevel(t *testing.T) {
|
|||||||
level := 9
|
level := 9
|
||||||
t.Logf("original length : %d \r\n", len(jsonStr))
|
t.Logf("original length : %d \r\n", len(jsonStr))
|
||||||
start := time.Now()
|
start := time.Now()
|
||||||
bytes, _ := CompressDataGzipLevel([]byte(jsonStr), level)
|
bytes, _ := CompressDataGzipLevel(jsonStr, level)
|
||||||
elapsed := time.Since(start)
|
elapsed := time.Since(start)
|
||||||
t.Logf("compress length : %d, level %d \r\n", len(bytes), level)
|
t.Logf("compress length : %d, level %d \r\n", len(bytes), level)
|
||||||
t.Log(elapsed)
|
t.Log(elapsed)
|
||||||
@ -36,7 +36,7 @@ func TestUnCompress(t *testing.T) {
|
|||||||
|
|
||||||
jsonStr := "{ \"glossary\": { \"title\": \"example glossary\", \"GlossDiv\": { \"title\": \"S\", \"GlossList\": { \"GlossEntry\": { \"ID\": \"SGML\", \"SortAs\": \"SGML\", \"GlossTerm\": \"Standard Generalized Markup Language\", \"Acronym\": \"SGML\", \"Abbrev\": \"ISO 8879:1986\", \"GlossDef\": { \"para\": \"A meta-markup language, used to create markup languages such as DocBook.\", \"GlossSeeAlso\": [\"GML\", \"XML\"]},\"GlossSee\": \"markup\"}}}}}"
|
jsonStr := "{ \"glossary\": { \"title\": \"example glossary\", \"GlossDiv\": { \"title\": \"S\", \"GlossList\": { \"GlossEntry\": { \"ID\": \"SGML\", \"SortAs\": \"SGML\", \"GlossTerm\": \"Standard Generalized Markup Language\", \"Acronym\": \"SGML\", \"Abbrev\": \"ISO 8879:1986\", \"GlossDef\": { \"para\": \"A meta-markup language, used to create markup languages such as DocBook.\", \"GlossSeeAlso\": [\"GML\", \"XML\"]},\"GlossSee\": \"markup\"}}}}}"
|
||||||
|
|
||||||
bytes := CompressDataGzip([]byte(jsonStr))
|
bytes := CompressDataGzip(jsonStr)
|
||||||
|
|
||||||
t.Logf("compress length : %d\r\n", len(bytes))
|
t.Logf("compress length : %d\r\n", len(bytes))
|
||||||
start := time.Now()
|
start := time.Now()
|
||||||
@ -57,7 +57,7 @@ func TestUnCompressLevel(t *testing.T) {
|
|||||||
jsonStr := "{ \"glossary\": { \"title\": \"example glossary\", \"GlossDiv\": { \"title\": \"S\", \"GlossList\": { \"GlossEntry\": { \"ID\": \"SGML\", \"SortAs\": \"SGML\", \"GlossTerm\": \"Standard Generalized Markup Language\", \"Acronym\": \"SGML\", \"Abbrev\": \"ISO 8879:1986\", \"GlossDef\": { \"para\": \"A meta-markup language, used to create markup languages such as DocBook.\", \"GlossSeeAlso\": [\"GML\", \"XML\"]},\"GlossSee\": \"markup\"}}}}}"
|
jsonStr := "{ \"glossary\": { \"title\": \"example glossary\", \"GlossDiv\": { \"title\": \"S\", \"GlossList\": { \"GlossEntry\": { \"ID\": \"SGML\", \"SortAs\": \"SGML\", \"GlossTerm\": \"Standard Generalized Markup Language\", \"Acronym\": \"SGML\", \"Abbrev\": \"ISO 8879:1986\", \"GlossDef\": { \"para\": \"A meta-markup language, used to create markup languages such as DocBook.\", \"GlossSeeAlso\": [\"GML\", \"XML\"]},\"GlossSee\": \"markup\"}}}}}"
|
||||||
|
|
||||||
level := 9
|
level := 9
|
||||||
bytes, _ := CompressDataGzipLevel([]byte(jsonStr), level)
|
bytes, _ := CompressDataGzipLevel(jsonStr, level)
|
||||||
|
|
||||||
t.Logf("compress length : %d, level %d\r\n", len(bytes), level)
|
t.Logf("compress length : %d, level %d\r\n", len(bytes), level)
|
||||||
start := time.Now()
|
start := time.Now()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user