gateway: TestCRTidyTwentyFourMBLine bounds the bytes searched and allocated, not the wall clock (#5282)
The test is the relay hot path's bill for one 24 MB SSE line in 32 KB reads. It had an 8s wall-clock bar. On main CI's Race step, beside heavy shards, one_call_mended took 16.9s on ubuntu and 10.7s on macOS. Alone under -race on mote it takes 4.4s. The bar measured the machine, and it also missed one of the regressions it was there for: a copy of the held bytes on every read took 0.37s. crTidy now counts the bytes it searches for a line's end or the escape (looked, one add per write and per region). The test bounds two things the code controls, each against the line's size: - bytes searched, at most 3x. Measured: 2x with no escape, 3x mended. - bytes allocated (runtime.MemStats), at most 8x with no escape and 40x mended. Measured: 5.4x (append grows the held line a quarter at a time), and mended 27x, 32x under -race (decoded into maps and said again). Broken by hand, on mote: - searching the whole held buffer for a line's end on each read (the quadratic read from yetone's review of #4256): 9.7 GB searched, red in both subtests. - copying what is held on each read: 9.7 GB and 10.2 GB allocated, red in both subtests. The old 8s bar passed it at 0.37s and 0.77s. Restored, green. Run on mote at this head: - go test -race -count=3 -run TestCRTidyTwentyFourMBLine ./internal/gateway: ok. mended takes 4.4s, no escape 0.19s. - go test -race -run 'CR|Glm|GLM|Tidy' ./internal/gateway: ok. - go vet ./internal/gateway on darwin, GOOS=linux and GOOS=windows: ok. gofmt -l: clean. Not tried: a regression that searches without going through the counted sites. The count bounds only what passes them. The allocation bound still catches one that copies.
This commit is contained in:
@@ -236,6 +236,9 @@ type crTidy struct {
|
||||
buf []byte
|
||||
calls map[string]*crDropper // a streamed call's cut escape, by its index or item id
|
||||
texts map[string]*crBytes // a custom tool's input, by its item id
|
||||
// looked is how many bytes were searched for a line's end or the
|
||||
// escape, what a test bounds by the stream's length (#5282)
|
||||
looked int
|
||||
}
|
||||
|
||||
func (t *crTidy) write(b []byte) []byte {
|
||||
@@ -246,6 +249,7 @@ func (t *crTidy) write(b []byte) []byte {
|
||||
old := len(t.buf)
|
||||
t.buf = append(t.buf, b...)
|
||||
// the bytes before these were searched for a line's end when they came
|
||||
t.looked += len(b)
|
||||
i := bytes.LastIndexByte(b, '\n')
|
||||
if i < 0 {
|
||||
return nil
|
||||
@@ -292,6 +296,7 @@ func (t *crTidy) cut() bool {
|
||||
// still has a tail waiting behind it, in whose room an unchanged region
|
||||
// would not survive being said from where it lies.
|
||||
func (t *crTidy) lines(b []byte, copy bool) []byte {
|
||||
t.looked += len(b)
|
||||
if !bytes.Contains(b, crSeq) && !t.cut() {
|
||||
if copy {
|
||||
return append([]byte(nil), b...)
|
||||
@@ -332,6 +337,7 @@ func (t *crTidy) lines(b []byte, copy bool) []byte {
|
||||
// its event carries gone. A line with none of the escape, and no cut one
|
||||
// to answer, goes as it came.
|
||||
func (t *crTidy) line(l []byte) ([]byte, bool) {
|
||||
t.looked += len(l)
|
||||
if !bytes.Contains(l, crSeq) && !t.cut() {
|
||||
return l, false
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package gateway
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
@@ -933,7 +934,11 @@ func TestGLMFlashScope(t *testing.T) {
|
||||
// line (a big response.completed) in 32KB reads. Without the escape the
|
||||
// line comes back as it came, at a scan's cost; with one, the call it
|
||||
// carries is read once and said again. The reads themselves were
|
||||
// quadratic once (yetone's review of #4256 measured 2.43s).
|
||||
// quadratic once (yetone's review of #4256 measured 2.43s). What is
|
||||
// bounded is the work the code controls, the bytes searched and the bytes
|
||||
// allocated, each a small multiple of the line's: a wall-clock bar
|
||||
// measured the machine, and went red under -race beside heavy shards
|
||||
// (16.9s on ubuntu CI, #5282).
|
||||
func TestCRTidyTwentyFourMBLine(t *testing.T) {
|
||||
unit := `{"type":"text","text":"hello \"world\" padding "}`
|
||||
rep := (24 << 20) / (len(unit) + 1)
|
||||
@@ -981,32 +986,43 @@ func TestCRTidyTwentyFourMBLine(t *testing.T) {
|
||||
name string
|
||||
line string
|
||||
want string
|
||||
// times the line's size it may allocate: the held line grows a
|
||||
// quarter at a time (~5x in all), and a mended line is also
|
||||
// decoded into maps and said again (27x, 32x under -race); a
|
||||
// copy of what is held on each read is ~400x
|
||||
room uint64
|
||||
}{
|
||||
{"no escape", big(false), big(false)},
|
||||
{"one call mended", big(true), ""},
|
||||
{"no escape", big(false), big(false), 8},
|
||||
{"one call mended", big(true), "", 40},
|
||||
} {
|
||||
t.Run(c.name, func(t *testing.T) {
|
||||
tidy := &crTidy{proto: provider.Responses, sse: true}
|
||||
line := []byte(c.line)
|
||||
outs := make([][]byte, 0, len(line)/(32<<10)+2)
|
||||
start := time.Now()
|
||||
var got []byte
|
||||
for i := 0; i < len(c.line); i += 32 << 10 {
|
||||
j := min(i+32<<10, len(c.line))
|
||||
if out := tidy.write([]byte(c.line[i:j])); out != nil {
|
||||
got = append(got, out...)
|
||||
made := allocated(func() {
|
||||
for i := 0; i < len(line); i += 32 << 10 {
|
||||
if out := tidy.write(line[i:min(i+32<<10, len(line))]); out != nil {
|
||||
outs = append(outs, out)
|
||||
}
|
||||
}
|
||||
}
|
||||
if out := tidy.flush(); out != nil {
|
||||
got = append(got, out...)
|
||||
}
|
||||
elapsed := time.Since(start)
|
||||
t.Logf("24MB line in 32KB reads: %s", elapsed)
|
||||
if c.want == "" {
|
||||
if out := tidy.flush(); out != nil {
|
||||
outs = append(outs, out)
|
||||
}
|
||||
})
|
||||
t.Logf("24MB line in 32KB reads: %s, %d bytes searched, %d allocated", time.Since(start), tidy.looked, made)
|
||||
if got := bytes.Join(outs, nil); c.want == "" {
|
||||
mended(t, string(got))
|
||||
} else if string(got) != c.want {
|
||||
t.Fatal("the line came out other than wanted")
|
||||
}
|
||||
if elapsed > 8*time.Second {
|
||||
t.Fatalf("24MB took %s", elapsed)
|
||||
// a read searched only for its own bytes, the line once
|
||||
// for the escape, and the changed line once more
|
||||
if n := len(line); tidy.looked > 3*n {
|
||||
t.Errorf("a %d-byte line was searched through %d bytes, over 3 times its size", n, tidy.looked)
|
||||
}
|
||||
if n := uint64(len(line)); made > c.room*n {
|
||||
t.Errorf("a %d-byte line allocated %d bytes, over %d times its size", n, made, c.room)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user