Commit 6592aeb8 authored by Brad Fitzpatrick's avatar Brad Fitzpatrick

net/http, net/http/httputil: make chunked reader alloc test more robust

Use testing.AllocsPerRun now that it exists, instead of doing it by hand.

Fixes #6076

R=golang-codereviews, alex.brainman
CC=golang-codereviews
https://golang.org/cl/53810043
parent 081e2d01
...@@ -8,11 +8,11 @@ ...@@ -8,11 +8,11 @@
package http package http
import ( import (
"bufio"
"bytes" "bytes"
"fmt" "fmt"
"io" "io"
"io/ioutil" "io/ioutil"
"runtime"
"testing" "testing"
) )
...@@ -42,8 +42,6 @@ func TestChunk(t *testing.T) { ...@@ -42,8 +42,6 @@ func TestChunk(t *testing.T) {
} }
func TestChunkReaderAllocs(t *testing.T) { func TestChunkReaderAllocs(t *testing.T) {
// temporarily set GOMAXPROCS to 1 as we are testing memory allocations
defer runtime.GOMAXPROCS(runtime.GOMAXPROCS(1))
var buf bytes.Buffer var buf bytes.Buffer
w := newChunkedWriter(&buf) w := newChunkedWriter(&buf)
a, b, c := []byte("aaaaaa"), []byte("bbbbbbbbbbbb"), []byte("cccccccccccccccccccccccc") a, b, c := []byte("aaaaaa"), []byte("bbbbbbbbbbbb"), []byte("cccccccccccccccccccccccc")
...@@ -52,26 +50,23 @@ func TestChunkReaderAllocs(t *testing.T) { ...@@ -52,26 +50,23 @@ func TestChunkReaderAllocs(t *testing.T) {
w.Write(c) w.Write(c)
w.Close() w.Close()
r := newChunkedReader(&buf)
readBuf := make([]byte, len(a)+len(b)+len(c)+1) readBuf := make([]byte, len(a)+len(b)+len(c)+1)
byter := bytes.NewReader(buf.Bytes())
var ms runtime.MemStats bufr := bufio.NewReader(byter)
runtime.ReadMemStats(&ms) mallocs := testing.AllocsPerRun(10, func() {
m0 := ms.Mallocs byter.Seek(0, 0)
bufr.Reset(byter)
r := newChunkedReader(bufr)
n, err := io.ReadFull(r, readBuf) n, err := io.ReadFull(r, readBuf)
runtime.ReadMemStats(&ms)
mallocs := ms.Mallocs - m0
if mallocs > 1 {
t.Errorf("%d mallocs; want <= 1", mallocs)
}
if n != len(readBuf)-1 { if n != len(readBuf)-1 {
t.Errorf("read %d bytes; want %d", n, len(readBuf)-1) t.Fatalf("read %d bytes; want %d", n, len(readBuf)-1)
} }
if err != io.ErrUnexpectedEOF { if err != io.ErrUnexpectedEOF {
t.Errorf("read error = %v; want ErrUnexpectedEOF", err) t.Fatalf("read error = %v; want ErrUnexpectedEOF", err)
}
})
if mallocs > 1.5 {
t.Logf("mallocs = %v; want 1", mallocs)
} }
} }
......
...@@ -10,11 +10,11 @@ ...@@ -10,11 +10,11 @@
package httputil package httputil
import ( import (
"bufio"
"bytes" "bytes"
"fmt" "fmt"
"io" "io"
"io/ioutil" "io/ioutil"
"runtime"
"testing" "testing"
) )
...@@ -44,8 +44,6 @@ func TestChunk(t *testing.T) { ...@@ -44,8 +44,6 @@ func TestChunk(t *testing.T) {
} }
func TestChunkReaderAllocs(t *testing.T) { func TestChunkReaderAllocs(t *testing.T) {
// temporarily set GOMAXPROCS to 1 as we are testing memory allocations
defer runtime.GOMAXPROCS(runtime.GOMAXPROCS(1))
var buf bytes.Buffer var buf bytes.Buffer
w := NewChunkedWriter(&buf) w := NewChunkedWriter(&buf)
a, b, c := []byte("aaaaaa"), []byte("bbbbbbbbbbbb"), []byte("cccccccccccccccccccccccc") a, b, c := []byte("aaaaaa"), []byte("bbbbbbbbbbbb"), []byte("cccccccccccccccccccccccc")
...@@ -54,26 +52,23 @@ func TestChunkReaderAllocs(t *testing.T) { ...@@ -54,26 +52,23 @@ func TestChunkReaderAllocs(t *testing.T) {
w.Write(c) w.Write(c)
w.Close() w.Close()
r := NewChunkedReader(&buf)
readBuf := make([]byte, len(a)+len(b)+len(c)+1) readBuf := make([]byte, len(a)+len(b)+len(c)+1)
byter := bytes.NewReader(buf.Bytes())
var ms runtime.MemStats bufr := bufio.NewReader(byter)
runtime.ReadMemStats(&ms) mallocs := testing.AllocsPerRun(10, func() {
m0 := ms.Mallocs byter.Seek(0, 0)
bufr.Reset(byter)
r := NewChunkedReader(bufr)
n, err := io.ReadFull(r, readBuf) n, err := io.ReadFull(r, readBuf)
runtime.ReadMemStats(&ms)
mallocs := ms.Mallocs - m0
if mallocs > 1 {
t.Errorf("%d mallocs; want <= 1", mallocs)
}
if n != len(readBuf)-1 { if n != len(readBuf)-1 {
t.Errorf("read %d bytes; want %d", n, len(readBuf)-1) t.Fatalf("read %d bytes; want %d", n, len(readBuf)-1)
} }
if err != io.ErrUnexpectedEOF { if err != io.ErrUnexpectedEOF {
t.Errorf("read error = %v; want ErrUnexpectedEOF", err) t.Fatalf("read error = %v; want ErrUnexpectedEOF", err)
}
})
if mallocs > 1.5 {
t.Logf("mallocs = %v; want 1", mallocs)
} }
} }
......
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