github.com/dolthub/dolt/go@v0.40.5-0.20240520175717-68db7794bea6/performance/replicationbench/async_replica_test.go (about)

     1  // Copyright 2021 Dolthub, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package serverbench
    16  
    17  import (
    18  	"context"
    19  	"fmt"
    20  	"runtime/pprof"
    21  	"strings"
    22  	"testing"
    23  
    24  	"github.com/dolthub/dolt/go/libraries/doltcore/servercfg"
    25  
    26  	"github.com/dolthub/dolt/go/libraries/doltcore/dtestutils/testcommands"
    27  	"github.com/dolthub/dolt/go/libraries/doltcore/env"
    28  	"github.com/dolthub/dolt/go/libraries/doltcore/sqle/dsess"
    29  )
    30  
    31  // usage: `go test -bench .`
    32  func BenchmarkAsyncPushOnWrite(b *testing.B) {
    33  	setup := make([]query, 1)
    34  	setup[0] = "CREATE TABLE bench (a int, b int, c int);"
    35  
    36  	q := strings.Builder{}
    37  	q.WriteString("INSERT INTO bench (a, b, c) VALUES (0, 0, 0)")
    38  	i := 1
    39  	for i < 1000 {
    40  		q.WriteString(fmt.Sprintf(",(%d, %d, %d)", i, i, i))
    41  		i++
    42  	}
    43  	qs := q.String()
    44  
    45  	bench := make([]query, 100)
    46  	commit := query("select dolt_commit('-am', 'cm')")
    47  	i = 0
    48  	for i < len(bench) {
    49  		bench[i] = query(qs)
    50  		bench[i+1] = commit
    51  		i += 2
    52  	}
    53  
    54  	benchmarkAsyncPush(b, serverTest{
    55  		name:  "smoke bench",
    56  		setup: setup,
    57  		bench: bench,
    58  	})
    59  }
    60  
    61  func benchmarkAsyncPush(b *testing.B, test serverTest) {
    62  	var dEnv *env.DoltEnv
    63  	var cfg servercfg.ServerConfig
    64  	ctx := context.Background()
    65  
    66  	// setup
    67  	dEnv, cfg = getAsyncEnvAndConfig(ctx, b)
    68  	dsess.InitPersistedSystemVars(dEnv)
    69  	executeServerQueries(ctx, b, dEnv, cfg, test.setup)
    70  
    71  	// bench
    72  	f := getProfFile(b)
    73  	err := pprof.StartCPUProfile(f)
    74  	if err != nil {
    75  		b.Fatal(err)
    76  	}
    77  	defer func() {
    78  		pprof.StopCPUProfile()
    79  		if err = f.Close(); err != nil {
    80  			b.Fatal(err)
    81  		}
    82  		fmt.Printf("\twriting CPU profile for %s: %s\n", b.Name(), f.Name())
    83  	}()
    84  
    85  	b.Run(test.name, func(b *testing.B) {
    86  		executeServerQueries(ctx, b, dEnv, cfg, test.bench)
    87  	})
    88  }
    89  
    90  func getAsyncEnvAndConfig(ctx context.Context, b *testing.B) (dEnv *env.DoltEnv, cfg servercfg.ServerConfig) {
    91  	multiSetup := testcommands.NewMultiRepoTestSetup(b.Fatal)
    92  
    93  	multiSetup.NewDB("dolt_bench")
    94  	multiSetup.NewRemote("remote1")
    95  
    96  	writerName := multiSetup.DbNames[0]
    97  
    98  	localCfg, ok := multiSetup.GetEnv(writerName).Config.GetConfig(env.LocalConfig)
    99  	if !ok {
   100  		b.Fatal("local config does not exist")
   101  	}
   102  	localCfg.SetStrings(map[string]string{fmt.Sprintf("%s.%s", env.SqlServerGlobalsPrefix, dsess.ReplicateToRemote): "remote1", fmt.Sprintf("%s.%s", env.SqlServerGlobalsPrefix, dsess.AsyncReplication): "1"})
   103  
   104  	yaml := []byte(fmt.Sprintf(`
   105  log_level: warning
   106  
   107  behavior:
   108   read_only: false
   109  
   110  user:
   111   name: "root"
   112   password: ""
   113  
   114  databases:
   115   - name: "%s"
   116     path: "%s"
   117  
   118  listener:
   119   host: localhost
   120   port: %d
   121   max_connections: 128
   122   read_timeout_millis: 28800000
   123   write_timeout_millis: 28800000
   124  `, writerName, multiSetup.DbPaths[writerName], port))
   125  
   126  	cfg, err := servercfg.NewYamlConfig(yaml)
   127  	if err != nil {
   128  		b.Fatal(err)
   129  	}
   130  
   131  	return multiSetup.GetEnv(writerName), cfg
   132  }