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