github.com/matrixorigin/matrixone@v1.2.0/pkg/util/trace/impl/motrace/statistic/stats_array_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 statistic
    16  
    17  import (
    18  	"testing"
    19  	"time"
    20  
    21  	"github.com/stretchr/testify/require"
    22  )
    23  
    24  func TestNewStatsArray(t *testing.T) {
    25  	type field struct {
    26  		version      float64
    27  		timeConsumed float64
    28  		memory       float64
    29  		s3in         float64
    30  		s3out        float64
    31  	}
    32  	tests := []struct {
    33  		name       string
    34  		field      field
    35  		wantString []byte
    36  	}{
    37  		{
    38  			name: "normal",
    39  			field: field{
    40  				version:      1,
    41  				timeConsumed: 2,
    42  				memory:       3,
    43  				s3in:         4,
    44  				s3out:        5,
    45  			},
    46  			wantString: []byte(`[1,2,3.000,4,5]`),
    47  		},
    48  		{
    49  			name: "random",
    50  			field: field{
    51  				version:      1,
    52  				timeConsumed: 65,
    53  				memory:       6,
    54  				s3in:         78,
    55  				s3out:        3494,
    56  			},
    57  			wantString: []byte(`[1,65,6.000,78,3494]`),
    58  		},
    59  		{
    60  			name: "random_with_0",
    61  			field: field{
    62  				version:      1,
    63  				timeConsumed: 65,
    64  				memory:       0,
    65  				s3in:         7834,
    66  				s3out:        0,
    67  			},
    68  			wantString: []byte(`[1,65,0,7834,0]`),
    69  		},
    70  		{
    71  			name: "scale_3",
    72  			field: field{
    73  				version:      1,
    74  				timeConsumed: 65,
    75  				memory:       0.001,
    76  				s3in:         7834,
    77  				s3out:        0,
    78  			},
    79  			wantString: []byte(`[1,65,0.001,7834,0]`),
    80  		},
    81  		{
    82  			name: "scale_0.00049",
    83  			field: field{
    84  				version:      1,
    85  				timeConsumed: 65,
    86  				memory:       0.0001,
    87  				s3in:         7834,
    88  				s3out:        0,
    89  			},
    90  			wantString: []byte(`[1,65,0.000,7834,0]`),
    91  		},
    92  		{
    93  			name: "scale_0.0005",
    94  			field: field{
    95  				version:      1,
    96  				timeConsumed: 65,
    97  				memory:       0.0005,
    98  				s3in:         7834,
    99  				s3out:        0,
   100  			},
   101  			wantString: []byte(`[1,65,0.001,7834,0]`),
   102  		},
   103  	}
   104  	for _, tt := range tests {
   105  		t.Run(tt.name, func(t *testing.T) {
   106  			s := NewStatsArrayV1().
   107  				WithVersion(tt.field.version).
   108  				WithTimeConsumed(tt.field.timeConsumed).
   109  				WithMemorySize(tt.field.memory).
   110  				WithS3IOInputCount(tt.field.s3in).
   111  				WithS3IOOutputCount(tt.field.s3out)
   112  			got := s.ToJsonString()
   113  			require.Equal(t, tt.wantString, got)
   114  			require.Equal(t, tt.field.version, s.GetVersion())
   115  			require.Equal(t, tt.field.timeConsumed, s.GetTimeConsumed())
   116  			require.Equal(t, tt.field.memory, s.GetMemorySize())
   117  			require.Equal(t, tt.field.s3in, s.GetS3IOInputCount())
   118  			require.Equal(t, tt.field.s3out, s.GetS3IOOutputCount())
   119  		})
   120  	}
   121  }
   122  
   123  func TestStatsArray_Add(t *testing.T) {
   124  	type field struct {
   125  		version      float64
   126  		timeConsumed float64
   127  		memory       float64
   128  		s3in         float64
   129  		s3out        float64
   130  	}
   131  	tests := []struct {
   132  		name       string
   133  		field      field
   134  		wantString []byte
   135  	}{
   136  		{
   137  			name: "normal",
   138  			field: field{
   139  				version:      1,
   140  				timeConsumed: 2,
   141  				memory:       3,
   142  				s3in:         4,
   143  				s3out:        5,
   144  			},
   145  			wantString: []byte(`[1,3,5.000,7,9]`),
   146  		},
   147  		{
   148  			name: "random",
   149  			field: field{
   150  				version:      1,
   151  				timeConsumed: 65,
   152  				memory:       6,
   153  				s3in:         78,
   154  				s3out:        3494,
   155  			},
   156  			wantString: []byte(`[1,66,8.000,81,3498]`),
   157  		},
   158  	}
   159  
   160  	getInitStatsArray := func() *StatsArray {
   161  		return NewStatsArrayV1().WithTimeConsumed(1).WithMemorySize(2).WithS3IOInputCount(3).WithS3IOOutputCount(4)
   162  	}
   163  
   164  	for _, tt := range tests {
   165  		t.Run(tt.name, func(t *testing.T) {
   166  			dst := getInitStatsArray()
   167  			s := NewStatsArrayV1().
   168  				WithVersion(tt.field.version).
   169  				WithTimeConsumed(tt.field.timeConsumed).
   170  				WithMemorySize(tt.field.memory).
   171  				WithS3IOInputCount(tt.field.s3in).
   172  				WithS3IOOutputCount(tt.field.s3out)
   173  			got := dst.Add(s).ToJsonString()
   174  			require.Equal(t, tt.wantString, got)
   175  		})
   176  	}
   177  }
   178  
   179  func TestStatsArray_AddV2(t *testing.T) {
   180  	type field struct {
   181  		version      float64
   182  		timeConsumed float64
   183  		memory       float64
   184  		s3in         float64
   185  		s3out        float64
   186  		traffic      float64
   187  	}
   188  	tests := []struct {
   189  		name       string
   190  		field      field
   191  		wantString []byte
   192  	}{
   193  		{
   194  			name: "normal",
   195  			field: field{
   196  				version:      1,
   197  				timeConsumed: 2,
   198  				memory:       3,
   199  				s3in:         4,
   200  				s3out:        5,
   201  				traffic:      6,
   202  			},
   203  			wantString: []byte(`[2,3,5.000,7,9,11]`),
   204  		},
   205  		{
   206  			name: "random",
   207  			field: field{
   208  				version:      1,
   209  				timeConsumed: 65,
   210  				memory:       6,
   211  				s3in:         78,
   212  				s3out:        3494,
   213  				traffic:      456,
   214  			},
   215  			wantString: []byte(`[2,66,8.000,81,3498,461]`),
   216  		},
   217  	}
   218  
   219  	getInitStatsArray := func() *StatsArray {
   220  		return NewStatsArrayV2().WithTimeConsumed(1).WithMemorySize(2).WithS3IOInputCount(3).WithS3IOOutputCount(4).WithOutTrafficBytes(5)
   221  	}
   222  
   223  	for _, tt := range tests {
   224  		t.Run(tt.name, func(t *testing.T) {
   225  			dst := getInitStatsArray()
   226  			s := NewStatsArrayV2().
   227  				WithTimeConsumed(tt.field.timeConsumed).
   228  				WithMemorySize(tt.field.memory).
   229  				WithS3IOInputCount(tt.field.s3in).
   230  				WithS3IOOutputCount(tt.field.s3out).
   231  				WithOutTrafficBytes(tt.field.traffic)
   232  			got := dst.Add(s).ToJsonString()
   233  			require.Equal(t, tt.wantString, got)
   234  		})
   235  	}
   236  }
   237  
   238  func TestStatsArray_AddV3(t *testing.T) {
   239  	type field struct {
   240  		version      float64
   241  		timeConsumed float64
   242  		memory       float64
   243  		s3in         float64
   244  		s3out        float64
   245  		traffic      float64
   246  		connType     ConnType
   247  	}
   248  	tests := []struct {
   249  		name       string
   250  		field      field
   251  		wantString []byte
   252  	}{
   253  		{
   254  			name: "normal",
   255  			field: field{
   256  				version:      1,
   257  				timeConsumed: 2,
   258  				memory:       3,
   259  				s3in:         4,
   260  				s3out:        5,
   261  				traffic:      6,
   262  				connType:     ConnTypeInternal,
   263  			},
   264  			wantString: []byte(`[3,3,5.000,7,9,11,1]`),
   265  		},
   266  		{
   267  			name: "random",
   268  			field: field{
   269  				version:      1,
   270  				timeConsumed: 65,
   271  				memory:       6,
   272  				s3in:         78,
   273  				s3out:        3494,
   274  				traffic:      456,
   275  				connType:     ConnTypeExternal,
   276  			},
   277  			wantString: []byte(`[3,66,8.000,81,3498,461,1]`),
   278  		},
   279  	}
   280  
   281  	getInitStatsArray := func() *StatsArray {
   282  		return NewStatsArrayV3().WithTimeConsumed(1).WithMemorySize(2).WithS3IOInputCount(3).WithS3IOOutputCount(4).WithOutTrafficBytes(5).WithConnType(ConnTypeInternal)
   283  	}
   284  
   285  	for _, tt := range tests {
   286  		t.Run(tt.name, func(t *testing.T) {
   287  			dst := getInitStatsArray()
   288  			s := NewStatsArrayV3().
   289  				WithTimeConsumed(tt.field.timeConsumed).
   290  				WithMemorySize(tt.field.memory).
   291  				WithS3IOInputCount(tt.field.s3in).
   292  				WithS3IOOutputCount(tt.field.s3out).
   293  				WithOutTrafficBytes(tt.field.traffic).
   294  				WithConnType(tt.field.connType)
   295  			got := dst.Add(s).ToJsonString()
   296  			require.Equal(t, tt.wantString, got)
   297  		})
   298  	}
   299  }
   300  
   301  func TestStatsArray_InitIfEmpty(t *testing.T) {
   302  	tests := []struct {
   303  		name string
   304  		s    StatsArray
   305  		want *StatsArray
   306  	}{
   307  		{
   308  			name: "normal",
   309  			s:    StatsArray{1, 2, 3, 4},
   310  			want: &StatsArray{1, 2, 3, 4},
   311  		},
   312  		{
   313  			name: "empty",
   314  			s:    StatsArray{},
   315  			want: &StatsArray{StatsArrayVersion},
   316  		},
   317  		{
   318  			name: "valid",
   319  			s:    StatsArray{0, 1},
   320  			want: &StatsArray{0, 1, 0, 0, 0},
   321  		},
   322  	}
   323  	for _, tt := range tests {
   324  		t.Run(tt.name, func(t *testing.T) {
   325  			got := tt.s.InitIfEmpty()
   326  			require.Equal(t, tt.want, got)
   327  		})
   328  	}
   329  }
   330  
   331  func BenchmarkStatsInfo(b *testing.B) {
   332  	s := StatsInfo{}
   333  	b.ResetTimer()
   334  	for i := 0; i < b.N; i++ {
   335  		s.ParseStartTime = time.Now()
   336  		s.ParseDuration = time.Since(s.ParseStartTime)
   337  		s.PlanStart()
   338  		s.PlanEnd()
   339  		s.CompileStart()
   340  		s.CompileEnd()
   341  		s.ExecutionStart()
   342  		s.ExecutionEnd()
   343  	}
   344  }