golang.org/x/tools/gopls@v0.15.3/internal/test/integration/bench/codeaction_test.go (about)

     1  // Copyright 2023 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package bench
     6  
     7  import (
     8  	"fmt"
     9  	"sync/atomic"
    10  	"testing"
    11  
    12  	"golang.org/x/tools/gopls/internal/protocol"
    13  )
    14  
    15  func BenchmarkCodeAction(b *testing.B) {
    16  	for _, test := range didChangeTests {
    17  		b.Run(test.repo, func(b *testing.B) {
    18  			env := getRepo(b, test.repo).sharedEnv(b)
    19  			env.OpenFile(test.file)
    20  			defer closeBuffer(b, env, test.file)
    21  			env.AfterChange()
    22  
    23  			env.CodeAction(test.file, nil) // pre-warm
    24  
    25  			b.ResetTimer()
    26  
    27  			if stopAndRecord := startProfileIfSupported(b, env, qualifiedName(test.repo, "hover")); stopAndRecord != nil {
    28  				defer stopAndRecord()
    29  			}
    30  
    31  			for i := 0; i < b.N; i++ {
    32  				env.CodeAction(test.file, nil)
    33  			}
    34  		})
    35  	}
    36  }
    37  
    38  func BenchmarkCodeActionFollowingEdit(b *testing.B) {
    39  	for _, test := range didChangeTests {
    40  		b.Run(test.repo, func(b *testing.B) {
    41  			env := getRepo(b, test.repo).sharedEnv(b)
    42  			env.OpenFile(test.file)
    43  			defer closeBuffer(b, env, test.file)
    44  			env.EditBuffer(test.file, protocol.TextEdit{NewText: "// __TEST_PLACEHOLDER_0__\n"})
    45  			env.AfterChange()
    46  
    47  			env.CodeAction(test.file, nil) // pre-warm
    48  
    49  			b.ResetTimer()
    50  
    51  			if stopAndRecord := startProfileIfSupported(b, env, qualifiedName(test.repo, "hover")); stopAndRecord != nil {
    52  				defer stopAndRecord()
    53  			}
    54  
    55  			for i := 0; i < b.N; i++ {
    56  				edits := atomic.AddInt64(&editID, 1)
    57  				env.EditBuffer(test.file, protocol.TextEdit{
    58  					Range: protocol.Range{
    59  						Start: protocol.Position{Line: 0, Character: 0},
    60  						End:   protocol.Position{Line: 1, Character: 0},
    61  					},
    62  					// Increment the placeholder text, to ensure cache misses.
    63  					NewText: fmt.Sprintf("// __TEST_PLACEHOLDER_%d__\n", edits),
    64  				})
    65  				env.CodeAction(test.file, nil)
    66  			}
    67  		})
    68  	}
    69  }