github.com/TeaOSLab/EdgeNode@v1.3.8/internal/compressions/writer_zstd_test.go (about) 1 // Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved. 2 3 package compressions_test 4 5 import ( 6 "bytes" 7 "github.com/TeaOSLab/EdgeNode/internal/compressions" 8 "strings" 9 "testing" 10 ) 11 12 func TestNewZSTDWriter_Level0(t *testing.T) { 13 var buf = &bytes.Buffer{} 14 writer, err := compressions.NewZSTDWriter(buf, 0) 15 if err != nil { 16 t.Fatal(err) 17 } 18 var originData = []byte(strings.Repeat("Hello", 1024)) 19 _, err = writer.Write(originData) 20 if err != nil { 21 t.Fatal(err) 22 } 23 err = writer.Close() 24 if err != nil { 25 t.Fatal(err) 26 } 27 t.Log("origin data:", len(originData), "result:", buf.Len()) 28 } 29 30 func TestNewZSTDWriter(t *testing.T) { 31 var buf = &bytes.Buffer{} 32 writer, err := compressions.NewZSTDWriter(buf, 10) 33 if err != nil { 34 t.Fatal(err) 35 } 36 var originData = []byte(strings.Repeat("Hello", 1024)) 37 _, err = writer.Write(originData) 38 if err != nil { 39 t.Fatal(err) 40 } 41 err = writer.Close() 42 if err != nil { 43 t.Fatal(err) 44 } 45 t.Log("origin data:", len(originData), "result:", buf.Len()) 46 } 47 48 func BenchmarkZSTDWriter_Write(b *testing.B) { 49 var data = []byte(strings.Repeat("A", 1024)) 50 51 for i := 0; i < b.N; i++ { 52 var buf = &bytes.Buffer{} 53 writer, err := compressions.NewZSTDWriter(buf, 5) 54 if err != nil { 55 b.Fatal(err) 56 } 57 58 for j := 0; j < 100; j++ { 59 _, err = writer.Write(data) 60 if err != nil { 61 b.Fatal(err) 62 } 63 64 /**err = writer.Flush() 65 if err != nil { 66 b.Fatal(err) 67 }**/ 68 } 69 70 _ = writer.Close() 71 } 72 } 73 74 func BenchmarkZSTDWriter_Write_Parallel(b *testing.B) { 75 var data = []byte(strings.Repeat("A", 1024)) 76 77 b.RunParallel(func(pb *testing.PB) { 78 for pb.Next() { 79 var buf = &bytes.Buffer{} 80 writer, err := compressions.NewZSTDWriter(buf, 5) 81 if err != nil { 82 b.Fatal(err) 83 } 84 85 for j := 0; j < 100; j++ { 86 _, err = writer.Write(data) 87 if err != nil { 88 b.Fatal(err) 89 } 90 91 /**err = writer.Flush() 92 if err != nil { 93 b.Fatal(err) 94 }**/ 95 } 96 97 _ = writer.Close() 98 } 99 }) 100 }