Skip to content

Commit ba3c31c

Browse files
committed
gopls/internal/regtest/bench: add a test simulating typing
Our existing benchmarks measure latency of various operations, but while typing most operations do not run to completion: they are canceled. Add a benchmark that simulates making changes at various fixed intervals, and records the total CPU used. For golang/go#60926 Change-Id: I972187358b6c802518ac781a12d9cff32faa6670 Reviewed-on: https://go-review.googlesource.com/c/tools/+/509559 Run-TryBot: Robert Findley <[email protected]> Reviewed-by: Alan Donovan <[email protected]> TryBot-Result: Gopher Robot <[email protected]> gopls-CI: kokoro <[email protected]>
1 parent 80c9aad commit ba3c31c

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package bench
2+
3+
import (
4+
"fmt"
5+
"sync/atomic"
6+
"testing"
7+
"time"
8+
9+
"golang.org/x/tools/gopls/internal/lsp/protocol"
10+
)
11+
12+
// BenchmarkTyping simulates typing steadily in a single file at different
13+
// paces.
14+
//
15+
// The key metric for this benchmark is not latency, but cpu_seconds per
16+
// operation.
17+
func BenchmarkTyping(b *testing.B) {
18+
for _, test := range didChangeTests {
19+
b.Run(test.repo, func(b *testing.B) {
20+
env := getRepo(b, test.repo).sharedEnv(b)
21+
env.OpenFile(test.file)
22+
// Insert the text we'll be modifying at the top of the file.
23+
env.EditBuffer(test.file, protocol.TextEdit{NewText: "// __REGTEST_PLACEHOLDER_0__\n"})
24+
env.AfterChange()
25+
26+
delays := []time.Duration{
27+
10 * time.Millisecond, // automated changes
28+
50 * time.Millisecond, // very fast mashing, or fast key sequences
29+
150 * time.Millisecond, // avg interval for 80wpm typing.
30+
}
31+
32+
for _, delay := range delays {
33+
b.Run(delay.String(), func(b *testing.B) {
34+
if stopAndRecord := startProfileIfSupported(b, env, qualifiedName(test.repo, "typing")); stopAndRecord != nil {
35+
defer stopAndRecord()
36+
}
37+
ticker := time.NewTicker(delay)
38+
for i := 0; i < b.N; i++ {
39+
edits := atomic.AddInt64(&editID, 1)
40+
env.EditBuffer(test.file, protocol.TextEdit{
41+
Range: protocol.Range{
42+
Start: protocol.Position{Line: 0, Character: 0},
43+
End: protocol.Position{Line: 1, Character: 0},
44+
},
45+
// Increment the placeholder text, to ensure cache misses.
46+
NewText: fmt.Sprintf("// __REGTEST_PLACEHOLDER_%d__\n", edits),
47+
})
48+
<-ticker.C
49+
}
50+
b.StopTimer()
51+
ticker.Stop()
52+
env.AfterChange() // wait for all change processing to complete
53+
})
54+
}
55+
})
56+
}
57+
}

0 commit comments

Comments
 (0)