vitess.io/vitess@v0.16.2/go/test/endtoend/tabletmanager/qps_test.go (about)

     1  /*
     2  Copyright 2019 The Vitess Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8  	http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  package tabletmanager
    17  
    18  import (
    19  	"context"
    20  	"testing"
    21  	"time"
    22  
    23  	"github.com/stretchr/testify/assert"
    24  	"github.com/stretchr/testify/require"
    25  
    26  	"vitess.io/vitess/go/mysql"
    27  	"vitess.io/vitess/go/test/endtoend/cluster"
    28  	"vitess.io/vitess/go/test/endtoend/utils"
    29  )
    30  
    31  func TestQPS(t *testing.T) {
    32  	defer cluster.PanicHandler(t)
    33  	ctx := context.Background()
    34  
    35  	vtParams := mysql.ConnParams{
    36  		Host: "localhost",
    37  		Port: clusterInstance.VtgateMySQLPort,
    38  	}
    39  	vtGateConn, err := mysql.Connect(ctx, &vtParams)
    40  	require.Nil(t, err)
    41  	defer vtGateConn.Close()
    42  
    43  	replicaConn, err := mysql.Connect(ctx, &replicaTabletParams)
    44  	require.Nil(t, err)
    45  	defer replicaConn.Close()
    46  
    47  	// Sanity Check
    48  	utils.Exec(t, vtGateConn, "delete from t1")
    49  	utils.Exec(t, vtGateConn, "insert into t1(id, value) values(1,'a'), (2,'b')")
    50  	checkDataOnReplica(t, replicaConn, `[[VARCHAR("a")] [VARCHAR("b")]]`)
    51  
    52  	// Test that tablet health stream reports a QPS >0.0.
    53  	// Therefore, issue several reads first.
    54  	// NOTE: This may be potentially flaky because we'll observe a QPS >0.0
    55  	//       exactly "once" for the duration of one sampling interval (5s) and
    56  	//       after that we'll see 0.0 QPS rates again. If this becomes actually
    57  	//       flaky, we need to read continuously in a separate thread.
    58  
    59  	for n := 0; n < 15; n++ {
    60  		// Run queries via vtGate so that they are counted.
    61  		utils.Exec(t, vtGateConn, "select * from t1")
    62  	}
    63  
    64  	// This may take up to 5 seconds to become true because we sample the query
    65  	// counts for the rates only every 5 seconds.
    66  
    67  	var qpsIncreased bool
    68  	timeout := time.Now().Add(12 * time.Second)
    69  	for time.Now().Before(timeout) {
    70  		shrs, err := clusterInstance.StreamTabletHealth(ctx, &primaryTablet, 1)
    71  		require.Nil(t, err)
    72  
    73  		streamHealthResponse := shrs[0]
    74  		realTimeStats := streamHealthResponse.GetRealtimeStats()
    75  		qps := realTimeStats.GetQps()
    76  		if qps > 0.0 {
    77  			qpsIncreased = true
    78  			break
    79  		}
    80  		time.Sleep(100 * time.Millisecond)
    81  	}
    82  	assert.True(t, qpsIncreased, "qps should be more that 0")
    83  }