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

     1  // Copyright 2024 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  	"github.com/stretchr/testify/assert"
    21  	"github.com/stretchr/testify/require"
    22  	"testing"
    23  	"time"
    24  )
    25  
    26  func TestCalculateFloat64(t *testing.T) {
    27  	type fields struct {
    28  		dividend float64
    29  		divisor  float64
    30  	}
    31  
    32  	tests := []struct {
    33  		name   string
    34  		fields fields
    35  		want   float64
    36  	}{
    37  		{
    38  			name: "cu",
    39  			fields: fields{
    40  				dividend: 6.79e-24,
    41  				divisor:  1.0026988039e-06,
    42  			},
    43  			want: 6.77172444366172e-18,
    44  		},
    45  	}
    46  	for _, tt := range tests {
    47  		t.Run(tt.name, func(t *testing.T) {
    48  			got := tt.fields.dividend / tt.fields.divisor
    49  			require.Equal(t, tt.want, got)
    50  		})
    51  	}
    52  }
    53  
    54  var dummyOBConfig = config.OBCUConfig{
    55  	CUUnit:        1.0026988039e-06,
    56  	CpuPrice:      7.43e-14,
    57  	MemPrice:      6.79e-24,
    58  	IoInPrice:     1e-06,
    59  	IoOutPrice:    1e-06,
    60  	TrafficPrice0: 8.94e-10,
    61  	TrafficPrice1: 0,
    62  	TrafficPrice2: 8.94e-10,
    63  }
    64  
    65  func TestCalculateCUMemBigNumber(t *testing.T) {
    66  	type args struct {
    67  		memByte    int64
    68  		durationNS int64
    69  	}
    70  	tests := []struct {
    71  		name string
    72  		args args
    73  		want float64
    74  	}{
    75  		{
    76  			name: "case1",
    77  			args: args{
    78  				memByte:    573384797164,
    79  				durationNS: 309319808921,
    80  			},
    81  			//     177359275896974822700044.0
    82  			want: 1.773592758969748e+23,
    83  		},
    84  	}
    85  	for _, tt := range tests {
    86  		t.Run(tt.name, func(t *testing.T) {
    87  			got := float64(tt.args.memByte) * float64(tt.args.durationNS)
    88  			assert.Equalf(t, tt.want, got, "float64 * float64: %v, %v", tt.args.memByte, tt.args.durationNS)
    89  		})
    90  	}
    91  }
    92  
    93  func TestCalculateCUMem(t *testing.T) {
    94  	type args struct {
    95  		memByte    int64
    96  		durationNS int64
    97  		cfg        *config.OBCUConfig
    98  	}
    99  
   100  	defaultCUConfig := config.NewOBCUConfig()
   101  
   102  	tests := []struct {
   103  		name string
   104  		args args
   105  		want float64
   106  	}{
   107  		{
   108  			name: "500GiB * 300s",
   109  			args: args{
   110  				memByte:    573384797164,
   111  				durationNS: 309319808921,
   112  				cfg:        &dummyOBConfig,
   113  			},
   114  			// want: 1.201028143901687e+06,
   115  			want: 1.2010281439016873e+06,
   116  		},
   117  		{
   118  			name: "default_cfg",
   119  			args: args{
   120  				memByte:    573384797164,
   121  				durationNS: 309319808921,
   122  				cfg:        defaultCUConfig,
   123  			},
   124  			// want: 806598.2280355261,
   125  			want: 806598.2280355261,
   126  		},
   127  	}
   128  	for _, tt := range tests {
   129  		t.Run(tt.name, func(t *testing.T) {
   130  			got := CalculateCUMem(tt.args.memByte, tt.args.durationNS, tt.args.cfg)
   131  			assert.Equalf(t, tt.want, got, "CalculateCUMem(%v, %v, %v)", tt.args.memByte, tt.args.durationNS, tt.args.cfg)
   132  		})
   133  	}
   134  }
   135  
   136  func TestCalculateCUMemDecimal(t *testing.T) {
   137  	type args struct {
   138  		memByte    int64
   139  		durationNS int64
   140  		cfg        *config.OBCUConfig
   141  	}
   142  	tests := []struct {
   143  		name string
   144  		args args
   145  		want float64
   146  	}{
   147  		{
   148  			name: "573,384,797,164(byte)*309(sec)",
   149  			args: args{
   150  				memByte:    573384797164,
   151  				durationNS: 309319808921,
   152  				cfg:        &dummyOBConfig,
   153  			},
   154  			want: 1.2010281439016873e+06,
   155  		},
   156  		{
   157  			name: "5,733(G-byte)*86400(sec)",
   158  			args: args{
   159  				memByte:    5733847971640,
   160  				durationNS: 86400e9,
   161  				cfg:        &dummyOBConfig,
   162  			},
   163  			//    3,354,742,523.444667
   164  			want: 3.354742523444667e+09,
   165  		},
   166  		{
   167  			name: "128(GB)*7*86400(sec)",
   168  			args: args{
   169  				memByte:    128 << 30,
   170  				durationNS: 7 * 86400e9,
   171  				cfg:        &dummyOBConfig,
   172  			},
   173  			//  562,886,586.3021176
   174  			want: 5.628865863021175e+08,
   175  		},
   176  		{
   177  			name: "573384797164(byte)*1e9(ns)",
   178  			args: args{
   179  				memByte:    573384797164,
   180  				durationNS: 1e9,
   181  				cfg:        &dummyOBConfig,
   182  			},
   183  			//    3882.803846579476
   184  			want: 3882.803846579476,
   185  		},
   186  	}
   187  	for _, tt := range tests {
   188  		t.Run(tt.name, func(t *testing.T) {
   189  			got, err := CalculateCUMemDecimal(tt.args.memByte, tt.args.durationNS, tt.args.cfg.MemPrice, tt.args.cfg.CUUnit)
   190  			require.Nil(t, err)
   191  			assert.Equalf(t, tt.want, got, "CalculateCUMemDecimal(%v, %v, %v, %v)", tt.args.memByte, tt.args.durationNS, tt.args.cfg.MemPrice, tt.args.cfg.CUUnit)
   192  		})
   193  	}
   194  }
   195  
   196  func TestCalculateDiff(t *testing.T) {
   197  	type args struct {
   198  		memByte    int64
   199  		durationNS int64
   200  	}
   201  	tests := []struct {
   202  		name string
   203  		args args
   204  		want float64
   205  	}{
   206  		{
   207  			name: "case1",
   208  			args: args{
   209  				memByte:    573384797164,
   210  				durationNS: 309319808921,
   211  			},
   212  			want: 1.773592758969748e+23,
   213  		},
   214  	}
   215  
   216  	cfg := &dummyOBConfig
   217  	func1 := func(bytes, ns, price, unit float64) float64 { return bytes * ns * price / unit }
   218  	func2 := func(bytes, ns, price, unit float64) float64 { return bytes * price / unit * ns }
   219  	func3 := func(bytes, ns, price, unit float64) float64 { return ns / unit * price * bytes }
   220  
   221  	for _, tt := range tests {
   222  		t.Run(tt.name, func(t *testing.T) {
   223  			got1 := func1(float64(tt.args.memByte), float64(tt.args.durationNS), cfg.MemPrice, cfg.CUUnit)
   224  			got2 := func2(float64(tt.args.memByte), float64(tt.args.durationNS), cfg.MemPrice, cfg.CUUnit)
   225  			got3 := func3(float64(tt.args.memByte), float64(tt.args.durationNS), cfg.MemPrice, cfg.CUUnit)
   226  			t.Logf("got1 = %v", got1)
   227  			t.Logf("got2 = %v", got2)
   228  			t.Logf("got3 = %v", got3)
   229  		})
   230  	}
   231  }
   232  
   233  func TestCalculateCU(t *testing.T) {
   234  	type args struct {
   235  		stats      statistic.StatsArray
   236  		durationNS time.Duration
   237  		cfg        *config.OBCUConfig
   238  	}
   239  
   240  	defaultCUConfig := config.NewOBCUConfig()
   241  
   242  	// [ 3, 7528422223, 573384797164.000, 0, 1, 247109, 2 ]
   243  	var stats2 statistic.StatsArray
   244  	stats2.Init()
   245  	stats2.WithTimeConsumed(7528422223)
   246  	stats2.WithMemorySize(573384797164)
   247  	stats2.WithS3IOInputCount(0)
   248  	stats2.WithS3IOOutputCount(1)
   249  	stats2.WithOutTrafficBytes(247109)
   250  	stats2.WithConnType(2)
   251  
   252  	tests := []struct {
   253  		name string
   254  		args args
   255  		want float64
   256  	}{
   257  		{
   258  			name: "case1",
   259  			args: args{
   260  				stats:      dummyStatsArray,
   261  				durationNS: 0,
   262  				cfg:        defaultCUConfig,
   263  			},
   264  			want: 44.016069999042564,
   265  		},
   266  		{
   267  			name: "big memory",
   268  			args: args{
   269  				stats:      stats2,
   270  				durationNS: 309319808921,
   271  				cfg:        defaultCUConfig,
   272  			},
   273  			want: 807058.4750616836,
   274  		},
   275  	}
   276  
   277  	for _, tt := range tests {
   278  		t.Run(tt.name, func(t *testing.T) {
   279  			got := CalculateCUWithCfg(tt.args.stats, int64(tt.args.durationNS), tt.args.cfg)
   280  			t.Logf("stats: %s", tt.args.stats.ToJsonString())
   281  			require.Equal(t, tt.want, got)
   282  		})
   283  	}
   284  }