github.com/matrixorigin/matrixone@v0.7.0/pkg/vm/engine/tae/samples/sample1/main.go (about) 1 // Copyright 2021 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 main 16 17 import ( 18 "os" 19 "runtime/pprof" 20 "sync" 21 "time" 22 23 "github.com/matrixorigin/matrixone/pkg/logutil" 24 "github.com/matrixorigin/matrixone/pkg/vm/engine/tae/catalog" 25 "github.com/matrixorigin/matrixone/pkg/vm/engine/tae/containers" 26 "github.com/matrixorigin/matrixone/pkg/vm/engine/tae/db" 27 "github.com/matrixorigin/matrixone/pkg/vm/engine/tae/testutils/config" 28 "github.com/panjf2000/ants/v2" 29 ) 30 31 var sampleDir = "/tmp/sample1" 32 var dbName = "db" 33 var cpuprofile = "/tmp/sample1/cpuprofile" 34 var memprofile = "/tmp/sample1/memprofile" 35 36 func init() { 37 os.RemoveAll(sampleDir) 38 } 39 40 func startProfile() { 41 f, _ := os.Create(cpuprofile) 42 _ = pprof.StartCPUProfile(f) 43 } 44 45 func stopProfile() { 46 pprof.StopCPUProfile() 47 memf, _ := os.Create(memprofile) 48 defer memf.Close() 49 _ = pprof.Lookup("heap").WriteTo(memf, 0) 50 } 51 52 func main() { 53 opts := config.WithOpts(nil, 1) 54 tae, _ := db.Open(sampleDir, opts) 55 defer tae.Close() 56 57 schema := catalog.MockSchemaAll(10, 3) 58 schema.BlockMaxRows = 20 59 schema.SegmentMaxBlocks = 2 60 batchCnt := uint32(1) 61 batchRows := int(schema.BlockMaxRows * 1 / 20 * batchCnt) 62 { 63 txn, _ := tae.StartTxn(nil) 64 db, _ := txn.CreateDatabase(dbName, "") 65 _, _ = db.CreateRelation(schema) 66 if err := txn.Commit(); err != nil { 67 panic(err) 68 } 69 } 70 bat := catalog.MockBatch(schema, batchRows) 71 bats := bat.Split(int(batchCnt)) 72 var wg sync.WaitGroup 73 doAppend := func(b *containers.Batch) func() { 74 return func() { 75 defer wg.Done() 76 txn, _ := tae.StartTxn(nil) 77 db, err := txn.GetDatabase(dbName) 78 if err != nil { 79 panic(err) 80 } 81 rel, err := db.GetRelationByName(schema.Name) 82 if err != nil { 83 panic(err) 84 } 85 if err := rel.Append(b); err != nil { 86 panic(err) 87 } 88 if err := txn.Commit(); err != nil { 89 panic(err) 90 } 91 } 92 } 93 p, _ := ants.NewPool(200) 94 now := time.Now() 95 startProfile() 96 for _, b := range bats { 97 wg.Add(1) 98 _ = p.Submit(doAppend(b)) 99 } 100 wg.Wait() 101 stopProfile() 102 logutil.Infof("Append takes: %s", time.Since(now)) 103 waitTime := time.Millisecond * time.Duration(batchCnt/200+1) * 300 104 for i := 0; i < 5; i++ { 105 time.Sleep(waitTime) 106 tae.PrintStats() 107 } 108 logutil.Info(tae.TxnBufMgr.String()) 109 logutil.Info(tae.MTBufMgr.String()) 110 }