有没有办法在golang的单元测试中限制内存使用量/增长量?
例如,在java中,我们可以这样做:
long before = Runtime.getRuntime().freeMemory()
// allocate a bunch of memory
long after = Runtime.getRuntime().freeMemory()
Assert.AssertTrue(before-after < 100)
(粗略地)断言我们没有使用超过100个字节。
答案 0 :(得分:4)
使用Go基准来分析内存使用情况。例如:
mem.go
:
package mem
func memUse() {
var stack [1024]byte
heap := make([]byte, 64*1024)
_, _ = stack, heap
}
mem_test.go
:
package mem
import "testing"
func BenchmarkMemUse(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
memUse()
}
b.StopTimer()
}
输出:
$ go test -bench=.
goos: linux
goarch: amd64
pkg: mem
BenchmarkMemUse-4 200000 8188 ns/op 65536 B/op 1 allocs/op
PASS
ok mem 1.745s
memUse
函数使一个堆分配为65536(64 * 1024)个字节。堆栈分配很便宜并且功能是本地的,所以我们不计算它们。
您可以使用ReportAllocs
标记代替-benchmem
方法。例如,
go test -bench=. -benchmem
参考文献:
Go: Package testing: Benchmarks
Command go: Description of testing functions
Command go: Description of testing flags
如果您真的必须在Go testing
包测试功能期间应用内存限制,请尝试使用runtime.MemStats
。例如,
func TestMemUse(t *testing.T) {
defer runtime.GOMAXPROCS(runtime.GOMAXPROCS(1))
var start, end runtime.MemStats
runtime.GC()
runtime.ReadMemStats(&start)
memUse()
runtime.ReadMemStats(&end)
alloc := end.TotalAlloc - start.TotalAlloc
limit := uint64(64 * 1000)
if alloc > limit {
t.Error("memUse:", "allocated", alloc, "limit", limit)
}
}
输出:
$ go test
--- FAIL: TestMemUse (0.00s)
mem_test.go:18: memUse: allocated 65536 limit 64000
FAIL
exit status 1
FAIL mem 0.003s