初始化项目

This commit is contained in:
皮蛋13361098506
2025-01-06 16:01:02 +08:00
commit 1b77f62820
575 changed files with 69193 additions and 0 deletions

View File

@@ -0,0 +1,4 @@
/*
lz4压缩、解压缩相关的助手包
*/
package lz4Util

View File

@@ -0,0 +1,36 @@
package lz4Util
import (
"fmt"
"github.com/bkaradzic/go-lz4"
)
// 压缩
// data待压缩数据
// 返回值:
// 压缩后数据
// 对应的错误
func Compress(data []byte)([]byte, error) {
compressed, err := lz4.Encode(nil, data)
if err != nil {
fmt.Println("Failed to encode:", err)
return nil,err
}
return compressed,nil
}
// 解压缩
// data:待解压缩数据
// 返回值:
// 解压缩后数据
// 对应的错误
func Decompress(data []byte) ([]byte, error){
decompressed, err := lz4.Decode(nil, data)
if err != nil {
fmt.Println("Failed to decode:", err)
return nil,err
}
return decompressed,nil
}

View File

@@ -0,0 +1,65 @@
package lz4Util
import (
"fmt"
"strings"
"testing"
)
var (
InitString = `{"Code":4,"Message":"IPNotAuthorized","Data":null}`
CompressBytes []byte
)
//正确性测试
func TestCompress(t *testing.T){
s := "hello world"
repeatCount:= 1000
toCompress := []byte(strings.Repeat(s, repeatCount))
compressed,err := Compress(toCompress)
if err!=nil{
fmt.Println(err)
}
fmt.Println("compressed Data:", string(compressed))
//decompress
decompressed,err := Decompress(compressed)
if err!=nil{
fmt.Println(err)
}
if strings.Repeat(s, repeatCount)!=string(decompressed){
fmt.Println("有问题")
}else{
fmt.Println("没问题")
}
fmt.Println("\ndecompressed Data:", string(decompressed))
}
//BenchmarkCompress-12 28647 37948 ns/op
func BenchmarkCompress(b *testing.B) {
toCompress := []byte(strings.Repeat(InitString, 100))
b.ResetTimer()
for i := 0; i < b.N; i++ {
_,err := Compress(toCompress)
if err!=nil{
b.Log(err)
}
}
b.StopTimer()
}
//BenchmarkDeCompress-12 267382 4127 ns/op
func BenchmarkDeCompress(b *testing.B) {
toCompress := []byte(strings.Repeat(InitString, 100))
compressedData,_ := Compress(toCompress)
b.ResetTimer()
for i := 0; i < b.N; i++ {
_,err := Decompress(compressedData)
if err!=nil{
b.Log(err)
}
}
b.StopTimer()
}