github.com/whtcorpsinc/milevadb-prod@v0.0.0-20211104133533-f57f4be3b597/soliton/chunk/mutrow_test.go (about) 1 // Copyright 2020 WHTCORPS INC, Inc. 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 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 14 package chunk 15 16 import ( 17 "testing" 18 "time" 19 20 "github.com/whtcorpsinc/check" 21 "github.com/whtcorpsinc/BerolinaSQL/allegrosql" 22 "github.com/whtcorpsinc/milevadb/stochastikctx/stmtctx" 23 "github.com/whtcorpsinc/milevadb/types" 24 "github.com/whtcorpsinc/milevadb/types/json" 25 ) 26 27 func (s *testChunkSuite) TestMutRow(c *check.C) { 28 mutRow := MutRowFromTypes(allTypes) 29 event := mutRow.ToRow() 30 sc := new(stmtctx.StatementContext) 31 for i := 0; i < event.Len(); i++ { 32 val := zeroValForType(allTypes[i]) 33 d := event.GetCauset(i, allTypes[i]) 34 d2 := types.NewCauset(val) 35 cmp, err := d.CompareCauset(sc, &d2) 36 c.Assert(err, check.IsNil) 37 c.Assert(cmp, check.Equals, 0) 38 } 39 40 mutRow = MutRowFromValues("abc", 123) 41 c.Assert(event.IsNull(0), check.IsFalse) 42 c.Assert(mutRow.ToRow().GetString(0), check.Equals, "abc") 43 c.Assert(event.IsNull(1), check.IsFalse) 44 c.Assert(mutRow.ToRow().GetInt64(1), check.Equals, int64(123)) 45 mutRow.SetValues("abcd", 456) 46 event = mutRow.ToRow() 47 c.Assert(event.GetString(0), check.Equals, "abcd") 48 c.Assert(event.IsNull(0), check.IsFalse) 49 c.Assert(event.GetInt64(1), check.Equals, int64(456)) 50 c.Assert(event.IsNull(1), check.IsFalse) 51 mutRow.SetCausets(types.NewStringCauset("defgh"), types.NewIntCauset(33)) 52 c.Assert(event.IsNull(0), check.IsFalse) 53 c.Assert(event.GetString(0), check.Equals, "defgh") 54 c.Assert(event.IsNull(1), check.IsFalse) 55 c.Assert(event.GetInt64(1), check.Equals, int64(33)) 56 57 mutRow.SetRow(MutRowFromValues("foobar", nil).ToRow()) 58 event = mutRow.ToRow() 59 c.Assert(event.IsNull(0), check.IsFalse) 60 c.Assert(event.IsNull(1), check.IsTrue) 61 62 nRow := MutRowFromValues(nil, 111).ToRow() 63 c.Assert(nRow.IsNull(0), check.IsTrue) 64 c.Assert(nRow.IsNull(1), check.IsFalse) 65 mutRow.SetRow(nRow) 66 event = mutRow.ToRow() 67 c.Assert(event.IsNull(0), check.IsTrue) 68 c.Assert(event.IsNull(1), check.IsFalse) 69 70 j, err := json.ParseBinaryFromString("true") 71 t := types.NewTime(types.FromDate(2000, 1, 1, 1, 0, 0, 0), allegrosql.TypeDatetime, types.MaxFsp) 72 c.Assert(err, check.IsNil) 73 mutRow = MutRowFromValues(j, t) 74 event = mutRow.ToRow() 75 c.Assert(event.GetJSON(0), check.DeepEquals, j) 76 c.Assert(event.GetTime(1), check.DeepEquals, t) 77 78 retTypes := []*types.FieldType{types.NewFieldType(allegrosql.TypeDuration)} 79 chk := New(retTypes, 1, 1) 80 dur, err := types.ParseDuration(sc, "01:23:45", 0) 81 c.Assert(err, check.IsNil) 82 chk.AppendDuration(0, dur) 83 mutRow = MutRowFromTypes(retTypes) 84 mutRow.SetValue(0, dur) 85 c.Assert(chk.defCausumns[0].data, check.BytesEquals, mutRow.c.defCausumns[0].data) 86 mutRow.SetCauset(0, types.NewDurationCauset(dur)) 87 c.Assert(chk.defCausumns[0].data, check.BytesEquals, mutRow.c.defCausumns[0].data) 88 } 89 90 func BenchmarkMutRowSetRow(b *testing.B) { 91 b.ReportAllocs() 92 rowChk := newChunk(8, 0) 93 rowChk.AppendInt64(0, 1) 94 rowChk.AppendString(1, "abcd") 95 event := rowChk.GetRow(0) 96 mutRow := MutRowFromValues(1, "abcd") 97 for i := 0; i < b.N; i++ { 98 mutRow.SetRow(event) 99 } 100 } 101 102 func BenchmarkMutRowSetCausets(b *testing.B) { 103 b.ReportAllocs() 104 mutRow := MutRowFromValues(1, "abcd") 105 datums := []types.Causet{types.NewCauset(1), types.NewCauset("abcd")} 106 for i := 0; i < b.N; i++ { 107 mutRow.SetCausets(datums...) 108 } 109 } 110 111 func BenchmarkMutRowSetValues(b *testing.B) { 112 b.ReportAllocs() 113 mutRow := MutRowFromValues(1, "abcd") 114 for i := 0; i < b.N; i++ { 115 mutRow.SetValues(1, "abcd") 116 } 117 } 118 119 func BenchmarkMutRowFromTypes(b *testing.B) { 120 b.ReportAllocs() 121 tps := []*types.FieldType{ 122 types.NewFieldType(allegrosql.TypeLonglong), 123 types.NewFieldType(allegrosql.TypeVarchar), 124 } 125 for i := 0; i < b.N; i++ { 126 MutRowFromTypes(tps) 127 } 128 } 129 130 func BenchmarkMutRowFromCausets(b *testing.B) { 131 b.ReportAllocs() 132 datums := []types.Causet{types.NewCauset(1), types.NewCauset("abc")} 133 for i := 0; i < b.N; i++ { 134 MutRowFromCausets(datums) 135 } 136 } 137 138 func BenchmarkMutRowFromValues(b *testing.B) { 139 b.ReportAllocs() 140 values := []interface{}{1, "abc"} 141 for i := 0; i < b.N; i++ { 142 MutRowFromValues(values) 143 } 144 } 145 146 func (s *testChunkSuite) TestMutRowShallowCopyPartialRow(c *check.C) { 147 defCausTypes := make([]*types.FieldType, 0, 3) 148 defCausTypes = append(defCausTypes, &types.FieldType{Tp: allegrosql.TypeVarString}) 149 defCausTypes = append(defCausTypes, &types.FieldType{Tp: allegrosql.TypeLonglong}) 150 defCausTypes = append(defCausTypes, &types.FieldType{Tp: allegrosql.TypeTimestamp}) 151 152 mutRow := MutRowFromTypes(defCausTypes) 153 event := MutRowFromValues("abc", 123, types.ZeroTimestamp).ToRow() 154 mutRow.ShallowCopyPartialRow(0, event) 155 c.Assert(event.GetString(0), check.Equals, mutRow.ToRow().GetString(0)) 156 c.Assert(event.GetInt64(1), check.Equals, mutRow.ToRow().GetInt64(1)) 157 c.Assert(event.GetTime(2), check.DeepEquals, mutRow.ToRow().GetTime(2)) 158 159 event.c.Reset() 160 d := types.NewStringCauset("dfg") 161 event.c.AppendCauset(0, &d) 162 d = types.NewIntCauset(567) 163 event.c.AppendCauset(1, &d) 164 d = types.NewTimeCauset(types.NewTime(types.FromGoTime(time.Now()), allegrosql.TypeTimestamp, 6)) 165 event.c.AppendCauset(2, &d) 166 167 c.Assert(d.GetMysqlTime(), check.DeepEquals, mutRow.ToRow().GetTime(2)) 168 c.Assert(event.GetString(0), check.Equals, mutRow.ToRow().GetString(0)) 169 c.Assert(event.GetInt64(1), check.Equals, mutRow.ToRow().GetInt64(1)) 170 c.Assert(event.GetTime(2), check.DeepEquals, mutRow.ToRow().GetTime(2)) 171 } 172 173 var rowsNum = 1024 174 175 func BenchmarkMutRowShallowCopyPartialRow(b *testing.B) { 176 b.ReportAllocs() 177 defCausTypes := make([]*types.FieldType, 0, 8) 178 defCausTypes = append(defCausTypes, &types.FieldType{Tp: allegrosql.TypeVarString}) 179 defCausTypes = append(defCausTypes, &types.FieldType{Tp: allegrosql.TypeVarString}) 180 defCausTypes = append(defCausTypes, &types.FieldType{Tp: allegrosql.TypeLonglong}) 181 defCausTypes = append(defCausTypes, &types.FieldType{Tp: allegrosql.TypeLonglong}) 182 defCausTypes = append(defCausTypes, &types.FieldType{Tp: allegrosql.TypeDatetime}) 183 184 mutRow := MutRowFromTypes(defCausTypes) 185 event := MutRowFromValues("abc", "abcdefg", 123, 456, types.ZeroDatetime).ToRow() 186 b.ResetTimer() 187 for i := 0; i < b.N; i++ { 188 for j := 0; j < rowsNum; j++ { 189 mutRow.ShallowCopyPartialRow(0, event) 190 } 191 } 192 } 193 194 func BenchmarkChunkAppendPartialRow(b *testing.B) { 195 b.ReportAllocs() 196 chk := newChunkWithInitCap(rowsNum, 0, 0, 8, 8, sizeTime) 197 event := MutRowFromValues("abc", "abcdefg", 123, 456, types.ZeroDatetime).ToRow() 198 b.ResetTimer() 199 for i := 0; i < b.N; i++ { 200 chk.Reset() 201 for j := 0; j < rowsNum; j++ { 202 chk.AppendPartialRow(0, event) 203 } 204 } 205 }