github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/pkg/causality/tests/integration_test.go (about)

     1  // Copyright 2022 PingCAP, 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  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package tests
    15  
    16  import (
    17  	"context"
    18  	"testing"
    19  	"time"
    20  
    21  	"github.com/pingcap/log"
    22  	"github.com/stretchr/testify/require"
    23  	"go.uber.org/zap/zapcore"
    24  )
    25  
    26  func TestConflictBasics(t *testing.T) {
    27  	ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
    28  	defer cancel()
    29  
    30  	const (
    31  		numWorkers     = 8
    32  		numSlots       = 4096
    33  		workingSetSize = 4096
    34  		batchSize      = 32
    35  		totalBatches   = 10000
    36  	)
    37  
    38  	conflictArray := make([]int, workingSetSize)
    39  	driver := newConflictTestDriver(
    40  		numWorkers, numSlots, newUniformGenerator(workingSetSize, batchSize, numSlots),
    41  	).WithExecFunc(
    42  		func(txn *txnForTest) error {
    43  			for _, key := range txn.ConflictKeys() {
    44  				// Access a position in the array without synchronization,
    45  				// so that if causality check is buggy, the Go race detection would fail.
    46  				conflictArray[key]++
    47  			}
    48  			return nil
    49  		})
    50  
    51  	require.NoError(t, driver.Run(ctx, totalBatches))
    52  	require.NoError(t, driver.Wait(ctx))
    53  	driver.Close()
    54  }
    55  
    56  func BenchmarkLowConflicts(b *testing.B) {
    57  	log.SetLevel(zapcore.WarnLevel)
    58  	defer log.SetLevel(zapcore.InfoLevel)
    59  
    60  	ctx, cancel := context.WithTimeout(context.Background(), 120*time.Second)
    61  	defer cancel()
    62  
    63  	const (
    64  		numWorkers = 8
    65  		numSlots   = 1024 * 1024
    66  		// Expected conflict rate = 0.00006
    67  		workingSetSize = 4096 * 4096
    68  		batchSize      = 8
    69  	)
    70  
    71  	totalBatches := b.N / 8
    72  	driver := newConflictTestDriver(
    73  		numWorkers,
    74  		numSlots,
    75  		newUniformGenerator(workingSetSize, batchSize, numSlots))
    76  	if err := driver.Run(ctx, totalBatches); err != nil {
    77  		panic(err)
    78  	}
    79  	driver.Close()
    80  }
    81  
    82  func BenchmarkMediumConflicts(b *testing.B) {
    83  	log.SetLevel(zapcore.WarnLevel)
    84  	defer log.SetLevel(zapcore.InfoLevel)
    85  
    86  	ctx, cancel := context.WithTimeout(context.Background(), 120*time.Second)
    87  	defer cancel()
    88  
    89  	const (
    90  		numWorkers = 8
    91  		numSlots   = 1024 * 1024
    92  		// Expected conflict rate = 0.0155
    93  		workingSetSize = 4096
    94  		batchSize      = 8
    95  	)
    96  
    97  	totalBatches := b.N / 8
    98  	driver := newConflictTestDriver(
    99  		numWorkers,
   100  		numSlots,
   101  		newUniformGenerator(workingSetSize, batchSize, numSlots))
   102  	if err := driver.Run(ctx, totalBatches); err != nil {
   103  		panic(err)
   104  	}
   105  	driver.Close()
   106  }
   107  
   108  func BenchmarkHighConflicts(b *testing.B) {
   109  	log.SetLevel(zapcore.WarnLevel)
   110  	defer log.SetLevel(zapcore.InfoLevel)
   111  
   112  	ctx, cancel := context.WithTimeout(context.Background(), 120*time.Second)
   113  	defer cancel()
   114  
   115  	const (
   116  		numWorkers = 8
   117  		numSlots   = 1024 * 1024
   118  		// Expected conflict rate = 0.41
   119  		workingSetSize = 128
   120  		batchSize      = 8
   121  	)
   122  
   123  	totalBatches := b.N / 8
   124  	driver := newConflictTestDriver(
   125  		numWorkers,
   126  		numSlots,
   127  		newUniformGenerator(workingSetSize, batchSize, numSlots))
   128  	if err := driver.Run(ctx, totalBatches); err != nil {
   129  		panic(err)
   130  	}
   131  	driver.Close()
   132  }