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

     1  // Copyright 2020 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  	"context"
     9  	"flag"
    10  	"fmt"
    11  	"testing"
    12  	"time"
    13  
    14  	"golang.org/x/tools/gopls/internal/cache"
    15  	"golang.org/x/tools/gopls/internal/hooks"
    16  	"golang.org/x/tools/gopls/internal/lsprpc"
    17  	"golang.org/x/tools/gopls/internal/test/integration/fake"
    18  	"golang.org/x/tools/internal/jsonrpc2"
    19  	"golang.org/x/tools/internal/jsonrpc2/servertest"
    20  )
    21  
    22  // github.com/pilosa/pilosa is a repository that has historically caused
    23  // significant memory problems for Gopls. We use it for a simple stress test
    24  // that types arbitrarily in a file with lots of dependents.
    25  
    26  var pilosaPath = flag.String("pilosa_path", "", "Path to a directory containing "+
    27  	"github.com/pilosa/pilosa, for stress testing. Do not set this unless you "+
    28  	"know what you're doing!")
    29  
    30  func TestPilosaStress(t *testing.T) {
    31  	// TODO(rfindley): revisit this test and make it is hermetic: it should check
    32  	// out pilosa into a directory.
    33  	//
    34  	// Note: This stress test has not been run recently, and may no longer
    35  	// function properly.
    36  	if *pilosaPath == "" {
    37  		t.Skip("-pilosa_path not configured")
    38  	}
    39  
    40  	sandbox, err := fake.NewSandbox(&fake.SandboxConfig{
    41  		Workdir: *pilosaPath,
    42  		GOPROXY: "https://proxy.golang.org",
    43  	})
    44  	if err != nil {
    45  		t.Fatal(err)
    46  	}
    47  
    48  	server := lsprpc.NewStreamServer(cache.New(nil), false, hooks.Options)
    49  	ts := servertest.NewPipeServer(server, jsonrpc2.NewRawStream)
    50  	ctx := context.Background()
    51  
    52  	const skipApplyEdits = false
    53  	editor, err := fake.NewEditor(sandbox, fake.EditorConfig{}).Connect(ctx, ts, fake.ClientHooks{}, skipApplyEdits)
    54  	if err != nil {
    55  		t.Fatal(err)
    56  	}
    57  
    58  	files := []string{
    59  		"cmd.go",
    60  		"internal/private.pb.go",
    61  		"roaring/roaring.go",
    62  		"roaring/roaring_internal_test.go",
    63  		"server/handler_test.go",
    64  	}
    65  	for _, file := range files {
    66  		if err := editor.OpenFile(ctx, file); err != nil {
    67  			t.Fatal(err)
    68  		}
    69  	}
    70  	ctx, cancel := context.WithTimeout(ctx, 10*time.Minute)
    71  	defer cancel()
    72  
    73  	i := 1
    74  	// MagicNumber is an identifier that occurs in roaring.go. Just change it
    75  	// arbitrarily.
    76  	if err := editor.RegexpReplace(ctx, "roaring/roaring.go", "MagicNumber", fmt.Sprintf("MagicNumber%d", 1)); err != nil {
    77  		t.Fatal(err)
    78  	}
    79  	for {
    80  		select {
    81  		case <-ctx.Done():
    82  			return
    83  		default:
    84  		}
    85  		if err := editor.RegexpReplace(ctx, "roaring/roaring.go", fmt.Sprintf("MagicNumber%d", i), fmt.Sprintf("MagicNumber%d", i+1)); err != nil {
    86  			t.Fatal(err)
    87  		}
    88  		// Simulate (very fast) typing.
    89  		//
    90  		// Typing 80 wpm ~150ms per keystroke.
    91  		time.Sleep(150 * time.Millisecond)
    92  		i++
    93  	}
    94  }