github.com/matrixorigin/matrixone@v1.2.0/pkg/util/trace/impl/motrace/cu_bench_test.go (about)

     1  // Copyright 2023 Matrix Origin
     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 motrace
    16  
    17  import (
    18  	"github.com/matrixorigin/matrixone/pkg/config"
    19  	"github.com/matrixorigin/matrixone/pkg/util/trace/impl/motrace/statistic"
    20  	"testing"
    21  	"time"
    22  )
    23  
    24  // BenchmarkCalculateMem
    25  //
    26  // goos: darwin
    27  // goarch: arm64
    28  // BenchmarkCalculateMem/100MiB_100ms
    29  // BenchmarkCalculateMem/100MiB_100ms-10         	564399114	         2.090 ns/op
    30  // BenchmarkCalculateMem/500GiB_300s
    31  // BenchmarkCalculateMem/500GiB_300s-10          	  117150	     10161 ns/op
    32  func BenchmarkCalculateMem(b *testing.B) {
    33  	type args struct {
    34  		memByte    int64
    35  		durationNS int64
    36  		cfg        *config.OBCUConfig
    37  	}
    38  	benchmarks := []struct {
    39  		name string
    40  		args args
    41  	}{
    42  		{
    43  			name: "100MiB_100ms",
    44  			args: args{
    45  				memByte:    100 << 20,
    46  				durationNS: int64(100 * time.Millisecond),
    47  				cfg:        &dummyOBConfig,
    48  			},
    49  		},
    50  		{
    51  			name: "500GiB_300s",
    52  			args: args{
    53  				memByte:    573384797164,
    54  				durationNS: 309319808921,
    55  				cfg:        &dummyOBConfig,
    56  			},
    57  		},
    58  	}
    59  
    60  	for _, bm := range benchmarks {
    61  		b.Run(bm.name, func(b *testing.B) {
    62  			b.ResetTimer()
    63  			for i := 0; i < b.N; i++ {
    64  				CalculateCUMem(bm.args.memByte, bm.args.durationNS, bm.args.cfg)
    65  			}
    66  		})
    67  	}
    68  }
    69  
    70  // BenchmarkCalculate10kTimes
    71  // goos: darwin
    72  // goarch: arm64
    73  // pkg: github.com/matrixorigin/matrixone/pkg/util/trace/impl/motrace
    74  // BenchmarkCalculate10kTimes/100MiB_100ms
    75  // BenchmarkCalculate10kTimes/100MiB_100ms-10         	   26626	     44598 ns/op
    76  // BenchmarkCalculate10kTimes/500GiB_300s
    77  // BenchmarkCalculate10kTimes/500GiB_300s-10          	      10	 103729042 ns/op
    78  // BenchmarkCalculate10kTimes/100MiB_100ms_sum_fist
    79  // BenchmarkCalculate10kTimes/100MiB_100ms_sum_fist-10         	    9730	    128880 ns/op
    80  // BenchmarkCalculate10kTimes/500GiB_300s_sum_fist
    81  // BenchmarkCalculate10kTimes/500GiB_300s_sum_fist-10          	    9130	    119859 ns/op
    82  //
    83  // -- 2rd time
    84  // BenchmarkCalculate10kTimes
    85  // BenchmarkCalculate10kTimes/100MiB_100ms
    86  // BenchmarkCalculate10kTimes/100MiB_100ms-10         	   26158	     45206 ns/op
    87  // BenchmarkCalculate10kTimes/500GiB_300s
    88  // BenchmarkCalculate10kTimes/500GiB_300s-10          	      12	  97179323 ns/op
    89  // BenchmarkCalculate10kTimes/100MiB_100ms_sum_fist
    90  // BenchmarkCalculate10kTimes/100MiB_100ms_sum_fist-10         	   10000	    130159 ns/op
    91  // BenchmarkCalculate10kTimes/500GiB_300s_sum_fist
    92  // BenchmarkCalculate10kTimes/500GiB_300s_sum_fist-10          	    9540	    120183 ns/op
    93  func BenchmarkCalculate10kTimes(b *testing.B) {
    94  	type args struct {
    95  		stats      statistic.StatsArray
    96  		durationNS int64
    97  		cfg        *config.OBCUConfig
    98  	}
    99  
   100  	// [ 3, 7528422223, 573384797164.000, 0, 1, 247109, 2 ]
   101  	var stats statistic.StatsArray
   102  	stats.WithTimeConsumed(123456)
   103  	stats.WithMemorySize(128 << 20)
   104  	stats.WithS3IOInputCount(1)
   105  	stats.WithS3IOOutputCount(1)
   106  	stats.WithOutTrafficBytes(247109)
   107  	stats.WithConnType(2)
   108  
   109  	benchmarks := []struct {
   110  		name string
   111  		args args
   112  	}{
   113  		{
   114  			name: "100MiB_100ms",
   115  			args: args{
   116  				stats:      stats,
   117  				durationNS: int64(100 * time.Millisecond),
   118  				cfg:        &dummyOBConfig,
   119  			},
   120  		},
   121  		{
   122  			name: "500GiB_300s",
   123  			args: args{
   124  				stats:      stats,
   125  				durationNS: 309319808921,
   126  				cfg:        &dummyOBConfig,
   127  			},
   128  		},
   129  	}
   130  
   131  	batch := int(10e3)
   132  
   133  	for _, bm := range benchmarks {
   134  		b.Run(bm.name, func(b *testing.B) {
   135  			val := float64(0)
   136  			b.ResetTimer()
   137  			for i := 0; i < b.N; i++ {
   138  				for cnt := 0; cnt < batch; cnt++ {
   139  					val += CalculateCUWithCfg(bm.args.stats, bm.args.durationNS, bm.args.cfg)
   140  				}
   141  			}
   142  		})
   143  	}
   144  	for _, bm := range benchmarks {
   145  		b.Run(bm.name+"_sum_fist", func(b *testing.B) {
   146  			var stats statistic.StatsArray
   147  			var durationNS int64
   148  			b.ResetTimer()
   149  			for i := 0; i < b.N; i++ {
   150  				for cnt := 0; cnt < batch; cnt++ {
   151  					stats.Add(&bm.args.stats)
   152  					durationNS += bm.args.durationNS
   153  				}
   154  				_ = CalculateCUWithCfg(stats, durationNS, bm.args.cfg)
   155  			}
   156  		})
   157  	}
   158  }
   159  
   160  // BenchmarkCalculateElem
   161  // goos: darwin
   162  // goarch: arm64
   163  // pkg: github.com/matrixorigin/matrixone/pkg/util/trace/impl/motrace
   164  // BenchmarkCalculateElem/100MiB_100ms
   165  // BenchmarkCalculateElem/100MiB_100ms-10         	336092049	         3.546 ns/op
   166  // BenchmarkCalculateElem/100MiB_100ms/cpu
   167  // BenchmarkCalculateElem/100MiB_100ms/cpu-10     	568659853	         2.062 ns/op
   168  // BenchmarkCalculateElem/100MiB_100ms/mem
   169  // BenchmarkCalculateElem/100MiB_100ms/mem-10     	583113988	         2.052 ns/op
   170  // BenchmarkCalculateElem/100MiB_100ms/io
   171  // BenchmarkCalculateElem/100MiB_100ms/io-10      	425241382	         2.849 ns/op
   172  // BenchmarkCalculateElem/100MiB_100ms/traffic
   173  // BenchmarkCalculateElem/100MiB_100ms/traffic-10 	585322668	         2.070 ns/op
   174  func BenchmarkCalculateElem(b *testing.B) {
   175  	type args struct {
   176  		stats      statistic.StatsArray
   177  		durationNS int64
   178  		cfg        *config.OBCUConfig
   179  	}
   180  
   181  	// [ 3, 7528422223, 573384797164.000, 0, 1, 247109, 2 ]
   182  	var stats statistic.StatsArray
   183  	stats.WithTimeConsumed(123456)
   184  	stats.WithMemorySize(128 << 20)
   185  	stats.WithS3IOInputCount(1)
   186  	stats.WithS3IOOutputCount(1)
   187  	stats.WithOutTrafficBytes(247109)
   188  	stats.WithConnType(2)
   189  
   190  	benchmarks := []struct {
   191  		name string
   192  		args args
   193  	}{
   194  		{
   195  			name: "100MiB_100ms",
   196  			args: args{
   197  				stats:      stats,
   198  				durationNS: int64(100 * time.Millisecond),
   199  				cfg:        &dummyOBConfig,
   200  			},
   201  		},
   202  	}
   203  
   204  	for _, bm := range benchmarks {
   205  		b.Run(bm.name, func(b *testing.B) {
   206  			b.ResetTimer()
   207  			for i := 0; i < b.N; i++ {
   208  				_ = CalculateCUWithCfg(bm.args.stats, bm.args.durationNS, bm.args.cfg)
   209  			}
   210  		})
   211  	}
   212  	for _, bm := range benchmarks {
   213  		b.Run(bm.name+"/cpu", func(b *testing.B) {
   214  			b.ResetTimer()
   215  			for i := 0; i < b.N; i++ {
   216  				_ = CalculateCUCpu(int64(bm.args.stats.GetTimeConsumed()), bm.args.cfg)
   217  			}
   218  		})
   219  	}
   220  	for _, bm := range benchmarks {
   221  		b.Run(bm.name+"/mem", func(b *testing.B) {
   222  			b.ResetTimer()
   223  			for i := 0; i < b.N; i++ {
   224  				_ = CalculateCUMem(int64(bm.args.stats.GetMemorySize()), bm.args.durationNS, bm.args.cfg)
   225  			}
   226  		})
   227  	}
   228  	for _, bm := range benchmarks {
   229  		b.Run(bm.name+"/io", func(b *testing.B) {
   230  			b.ResetTimer()
   231  			for i := 0; i < b.N; i++ {
   232  				_ = CalculateCUIOIn(int64(bm.args.stats.GetS3IOInputCount()), bm.args.cfg)
   233  				_ = CalculateCUIOOut(int64(bm.args.stats.GetS3IOOutputCount()), bm.args.cfg)
   234  			}
   235  		})
   236  	}
   237  	for _, bm := range benchmarks {
   238  		b.Run(bm.name+"/traffic", func(b *testing.B) {
   239  			b.ResetTimer()
   240  			for i := 0; i < b.N; i++ {
   241  				_ = CalculateCUTraffic(int64(bm.args.stats.GetOutTrafficBytes()), bm.args.stats.GetConnType(), bm.args.cfg)
   242  			}
   243  		})
   244  	}
   245  
   246  }