vitess.io/vitess@v0.16.2/go/vt/vttablet/tabletserver/vstreamer/packet_size_test.go (about)

     1  /*
     2  Copyright 2021 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  
    17  package vstreamer
    18  
    19  import (
    20  	"math"
    21  	"math/rand"
    22  	"testing"
    23  	"time"
    24  )
    25  
    26  type polynomial []float64
    27  
    28  func (p polynomial) fit(x float64) float64 {
    29  	var y float64
    30  	for i, exp := range p {
    31  		y += exp * math.Pow(x, float64(i))
    32  	}
    33  	return y
    34  }
    35  
    36  func simulate(t *testing.T, ps PacketSizer, base, mustSend int, interpolate func(float64) float64) (time.Duration, int) {
    37  	t.Helper()
    38  
    39  	var elapsed time.Duration
    40  	var sent int
    41  	var sentPkt int
    42  	packetRange := float64(base) * 10.0
    43  
    44  	packetSize := 0
    45  	for sent < mustSend {
    46  		packetSize += rand.Intn(base / 100)
    47  
    48  		if ps.ShouldSend(packetSize) {
    49  			x := float64(packetSize) / packetRange
    50  			y := interpolate(x)
    51  			d := time.Duration(float64(time.Microsecond) * y * float64(packetSize))
    52  			ps.Record(packetSize, d)
    53  
    54  			sent += packetSize
    55  			elapsed += d
    56  			sentPkt++
    57  
    58  			packetSize = 0
    59  		}
    60  	}
    61  	return elapsed, sentPkt
    62  }
    63  
    64  func TestPacketSizeSimulation(t *testing.T) {
    65  	cases := []struct {
    66  		name     string
    67  		baseSize int
    68  		p        polynomial
    69  		error    time.Duration
    70  	}{
    71  		{
    72  			name:     "growth with tapper",
    73  			baseSize: 25000,
    74  			p:        polynomial{0.767, 1.278, -12.048, 25.262, -21.270, 6.410},
    75  		},
    76  		{
    77  			name:     "growth without tapper",
    78  			baseSize: 25000,
    79  			p:        polynomial{0.473, 5.333, -38.663, 90.731, -87.005, 30.128},
    80  			error:    5 * time.Millisecond,
    81  		},
    82  		{
    83  			name:     "regression",
    84  			baseSize: 25000,
    85  			p:        polynomial{0.247, -0.726, 2.864, -3.022, 2.273, -0.641},
    86  			error:    1 * time.Second,
    87  		},
    88  	}
    89  
    90  	for _, tc := range cases {
    91  		t.Run(tc.name, func(t *testing.T) {
    92  			seed := time.Now().UnixNano()
    93  			rand.Seed(seed)
    94  
    95  			// Simulate a replication using the given polynomial and the dynamic packet sizer
    96  			ps1 := newDynamicPacketSizer(tc.baseSize)
    97  			elapsed1, sent1 := simulate(t, ps1, tc.baseSize, tc.baseSize*1000, tc.p.fit)
    98  
    99  			// Simulate the same polynomial using a fixed packet size
   100  			ps2 := newFixedPacketSize(tc.baseSize)
   101  			elapsed2, sent2 := simulate(t, ps2, tc.baseSize, tc.baseSize*1000, tc.p.fit)
   102  
   103  			// the simulation for dynamic packet sizing should always be faster then the fixed packet,
   104  			// and should also send fewer packets in total
   105  			delta := elapsed1 - elapsed2
   106  			if delta > tc.error {
   107  				t.Errorf("packet-adjusted simulation is %v slower than fixed approach, seed %d", delta, seed)
   108  			}
   109  			if sent1 > sent2 {
   110  				t.Errorf("packet-adjusted simulation sent more packets (%d) than fixed approach (%d), seed %d", sent1, sent2, seed)
   111  			}
   112  			// t.Logf("dynamic = (%v, %d), fixed = (%v, %d)", elapsed1, sent1, elapsed2, sent2)
   113  		})
   114  	}
   115  }