forked from ego-component/eoss
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcompress_test.go
80 lines (76 loc) · 1.92 KB
/
compress_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package eos
import (
"io"
"os"
"runtime"
"testing"
)
// TestCompress_gzip
// src_path 原始文件
// target_path 生成的目标文件
// 测试读取文件后压缩 输出到指定path 对比是否正常
func TestCompress_gzip(t *testing.T) {
path := os.Getenv("src_path")
source, err := os.Open(path)
if err != nil {
panic(err)
}
reader, _, err := DefaultGzipCompressor.Compress(source)
if err != nil {
panic(err)
}
targetPath := os.Getenv("target_path")
_, err = os.Stat(targetPath)
if err == nil {
os.Remove(targetPath)
}
target, err := os.Create(targetPath)
if err != nil {
panic(err)
}
defer target.Close()
defer source.Close()
_, err = io.Copy(target, reader)
if err != nil {
panic(err)
}
}
func TestGetLength(t *testing.T) {
path := os.Getenv("src_path")
source, err := os.Open(path)
if err != nil {
panic(err)
}
var m runtime.MemStats
runtime.ReadMemStats(&m)
t.Logf("内存分配 (HeapAlloc): %v bytes\n", m.HeapAlloc)
t.Logf("内存总分配 (HeapSys): %v bytes\n", m.HeapSys)
t.Logf("内存被释放但还未被重新分配 (HeapIdle): %v bytes\n", m.HeapIdle)
t.Logf("内存仍然被使用 (HeapInuse): %v bytes\n", m.HeapInuse)
length, err := GetReaderLength(source)
t.Logf("-----------------------------")
runtime.ReadMemStats(&m)
t.Logf("内存分配 (HeapAlloc): %v bytes\n", m.HeapAlloc)
t.Logf("内存总分配 (HeapSys): %v bytes\n", m.HeapSys)
t.Logf("内存被释放但还未被重新分配 (HeapIdle): %v bytes\n", m.HeapIdle)
t.Logf("内存仍然被使用 (HeapInuse): %v bytes\n", m.HeapInuse)
if err != nil {
panic(err)
}
t.Logf("length %d", length)
seeker, _, _ := DefaultGzipCompressor.Compress(source)
targetPath := os.Getenv("target_path")
_, err = os.Stat(targetPath)
if err == nil {
os.Remove(targetPath)
}
target, err := os.Create(targetPath)
if err != nil {
panic(err)
}
defer target.Close()
_, err = io.Copy(target, seeker)
if err != nil {
panic(err)
}
}