github.com/voedger/voedger@v0.0.0-20240520144910-273e84102129/pkg/processors/query/bench_test.go (about) 1 /* 2 * Copyright (c) 2021-present unTill Pro, Ltd. 3 * @author Maxim Geraskin 4 */ 5 6 package queryprocessor 7 8 /* 9 10 import ( 11 "context" 12 "testing" 13 "time" 14 15 "github.com/stretchr/testify/require" 16 "github.com/voedger/voedger/pkg/appparts" 17 "github.com/voedger/voedger/pkg/iauthnzimpl" 18 "github.com/voedger/voedger/pkg/iprocbus" 19 "github.com/voedger/voedger/pkg/istructs" 20 imetrics "github.com/voedger/voedger/pkg/metrics" 21 ibus "github.com/voedger/voedger/staging/src/github.com/untillpro/airs-ibus" 22 ) 23 24 func Benchmark_pipelineIService_Sequential(b *testing.B) { 25 require := require.New(b) 26 serviceChannel := make(iprocbus.ServiceChannel) 27 res := make(chan interface{}) 28 body := []byte(`{ 29 "args":{ 30 "from":1257894000, 31 "till":2257894000 32 }, 33 "elements":[ 34 { 35 "path":"", 36 "fields":["sys.ID","name"], 37 "refs":[["id_department","name"]] 38 } 39 ], 40 "filters":[{"expr":"eq","args":{"field":"id_department/name","value":"Alcohol drinks"}}], 41 "orderBy":[{"field":"name"}], 42 "count":1, 43 "startFrom":1 44 }`) 45 rs := testResultSenderClosable{ 46 startArraySection: func(sectionType string, path []string) {}, 47 sendElement: func(name string, element interface{}) (err error) { 48 values := element.([]interface{}) 49 res <- values 50 return 51 }, 52 close: func(err error) { 53 }, 54 } 55 authn := iauthnzimpl.NewDefaultAuthenticator(iauthnzimpl.TestSubjectRolesGetter) 56 authz := iauthnzimpl.NewDefaultAuthorizer() 57 appDef, appStructsProvider, appTokens := getTestCfg(require, nil) 58 59 appParts, cleanAppParts, err := appparts.New(appStructsProvider) 60 require.NoError(err) 61 defer cleanAppParts() 62 appParts.DeployApp(appName, appDef, appPartsCount, appEngines) 63 appParts.DeployAppPartitions(appName, []istructs.PartitionID{partID}) 64 65 queryProcessor := ProvideServiceFactory()( 66 serviceChannel, 67 func(ctx context.Context, sender ibus.ISender) IResultSenderClosable { return rs }, 68 appParts, 69 3, // MaxPrepareQueries 70 imetrics.Provide(), "vvm", authn, authz) 71 go queryProcessor.Run(context.Background()) 72 start := time.Now() 73 sysToken := getSystemToken(appTokens) 74 75 b.ResetTimer() 76 77 for i := 0; i < b.N; i++ { 78 serviceChannel <- NewQueryMessage(context.Background(), appName, partID, wsID, nil, body, qNameFunction, "", sysToken) 79 <-res 80 } 81 82 b.ReportMetric(float64(b.N)/time.Since(start).Seconds(), "ops") 83 } 84 85 //Before 6730 180564 ns/op 5538 ops 86 //With sync pipeline re-use 12390 97833 ns/op 10221 ops 87 88 func Benchmark_pipelineIService_Parallel(b *testing.B) { 89 start := time.Now() 90 91 b.SetParallelism(4) 92 93 b.RunParallel(func(pb *testing.PB) { 94 require := require.New(b) 95 serviceChannel := make(iprocbus.ServiceChannel) 96 body := []byte(`{ 97 "args":{ 98 "from":1257894000, 99 "till":2257894000 100 }, 101 "elements":[ 102 { 103 "path":"", 104 "fields":["sys.ID","name"], 105 "refs":[["id_department","name"]] 106 } 107 ], 108 "filters":[{"expr":"eq","args":{"field":"id_department/name","value":"Alcohol drinks"}}], 109 "orderBy":[{"field":"name"}], 110 "count":1, 111 "startFrom":1 112 }`) 113 res := make(chan interface{}) 114 rs := testResultSenderClosable{ 115 startArraySection: func(sectionType string, path []string) {}, 116 sendElement: func(name string, element interface{}) (err error) { 117 values := element.([]interface{}) 118 res <- values 119 return 120 }, 121 close: func(err error) { 122 }, 123 } 124 authn := iauthnzimpl.NewDefaultAuthenticator(iauthnzimpl.TestSubjectRolesGetter) 125 authz := iauthnzimpl.NewDefaultAuthorizer() 126 appDef, appStructsProvider, appTokens := getTestCfg(require, nil) 127 128 appParts, cleanAppParts, err := appparts.New(appStructsProvider) 129 require.NoError(err) 130 defer cleanAppParts() 131 appParts.DeployApp(appName, appDef, appPartsCount, appEngines) 132 appParts.DeployAppPartitions(appName, []istructs.PartitionID{partID}) 133 134 queryProcessor := ProvideServiceFactory()( 135 serviceChannel, 136 func(ctx context.Context, sender ibus.ISender) IResultSenderClosable { return rs }, 137 appParts, 138 3, // MaxPrepareQueries 139 imetrics.Provide(), "vvm", authn, authz) 140 go queryProcessor.Run(context.Background()) 141 sysToken := getSystemToken(appTokens) 142 143 b.ResetTimer() 144 145 for pb.Next() { 146 serviceChannel <- NewQueryMessage(context.Background(), appName, partID, wsID, nil, body, qNameFunction, "", sysToken) 147 <-res 148 } 149 }) 150 b.ReportMetric(float64(b.N)/time.Since(start).Seconds(), "ops") 151 } 152 153 //Before 19144 61210 ns/op 16172 ops 154 //With sync pipeline re-use 37027 32734 ns/op 30474 ops 155 156 */