Commit 13122745 authored by Han-Wen Nienhuys's avatar Han-Wen Nienhuys

Add test for memory use.

parent 1106b7e2
......@@ -9,6 +9,7 @@ import (
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"syscall"
"testing"
......@@ -570,6 +571,41 @@ func CompareSlices(t *testing.T, got, want []byte) {
}
}
// Check that reading large files doesn't lead to large allocations.
func TestReadLargeMemCheck(t *testing.T) {
ts := NewTestCase(t)
defer ts.Cleanup()
content := RandomData(385*1023)
err := ioutil.WriteFile(ts.origFile, []byte(content), 0644)
CheckSuccess(err)
f, err := os.Open(ts.mountFile)
CheckSuccess(err)
buf := make([]byte, len(content)+1024)
f.Read(buf)
CheckSuccess(err)
f.Close()
runtime.GC()
var before, after runtime.MemStats
N := 100
runtime.ReadMemStats(&before)
for i := 0; i < N; i++ {
f, _ := os.Open(ts.mountFile)
f.Read(buf)
f.Close()
}
runtime.ReadMemStats(&after)
delta := int((after.TotalAlloc - before.TotalAlloc))
delta = (delta - 40000)/ N
limit := 5000
if delta > limit {
t.Errorf("bytes per loop: %d, limit %d", delta, limit)
}
}
func TestReadLarge(t *testing.T) {
ts := NewTestCase(t)
defer ts.Cleanup()
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment