|
| 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