File: typing_test.go

package info (click to toggle)
golang-golang-x-tools 1%3A0.25.0%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 22,724 kB
  • sloc: javascript: 2,027; asm: 1,645; sh: 166; yacc: 155; makefile: 49; ansic: 8
file content (63 lines) | stat: -rw-r--r-- 1,919 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// Copyright 2023 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package bench

import (
	"fmt"
	"sync/atomic"
	"testing"
	"time"

	"golang.org/x/tools/gopls/internal/protocol"
)

// BenchmarkTyping simulates typing steadily in a single file at different
// paces.
//
// The key metric for this benchmark is not latency, but cpu_seconds per
// operation.
func BenchmarkTyping(b *testing.B) {
	for _, test := range didChangeTests {
		b.Run(test.repo, func(b *testing.B) {
			env := getRepo(b, test.repo).sharedEnv(b)
			env.OpenFile(test.file)
			defer closeBuffer(b, env, test.file)

			// Insert the text we'll be modifying at the top of the file.
			env.EditBuffer(test.file, protocol.TextEdit{NewText: "// __TEST_PLACEHOLDER_0__\n"})
			env.AfterChange()

			delays := []time.Duration{
				10 * time.Millisecond,  // automated changes
				50 * time.Millisecond,  // very fast mashing, or fast key sequences
				150 * time.Millisecond, // avg interval for 80wpm typing.
			}

			for _, delay := range delays {
				b.Run(delay.String(), func(b *testing.B) {
					if stopAndRecord := startProfileIfSupported(b, env, qualifiedName(test.repo, "typing")); stopAndRecord != nil {
						defer stopAndRecord()
					}
					ticker := time.NewTicker(delay)
					for i := 0; i < b.N; i++ {
						edits := atomic.AddInt64(&editID, 1)
						env.EditBuffer(test.file, protocol.TextEdit{
							Range: protocol.Range{
								Start: protocol.Position{Line: 0, Character: 0},
								End:   protocol.Position{Line: 1, Character: 0},
							},
							// Increment the placeholder text, to ensure cache misses.
							NewText: fmt.Sprintf("// __TEST_PLACEHOLDER_%d__\n", edits),
						})
						<-ticker.C
					}
					b.StopTimer()
					ticker.Stop()
					env.AfterChange() // wait for all change processing to complete
				})
			}
		})
	}
}