2022-03-08 10:15:59 +08:00
|
|
|
// Licensed to the LF AI & Data foundation under one
|
|
|
|
// or more contributor license agreements. See the NOTICE file
|
|
|
|
// distributed with this work for additional information
|
|
|
|
// regarding copyright ownership. The ASF licenses this file
|
|
|
|
// to you under the Apache License, Version 2.0 (the
|
|
|
|
// "License"); you may not use this file except in compliance
|
|
|
|
// with the License. You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package querynode
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"os"
|
|
|
|
"runtime/pprof"
|
|
|
|
"strconv"
|
|
|
|
"testing"
|
|
|
|
|
2022-05-23 16:41:58 +08:00
|
|
|
queryPb "github.com/milvus-io/milvus/internal/proto/querypb"
|
|
|
|
|
2022-03-08 10:15:59 +08:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"go.uber.org/zap/zapcore"
|
|
|
|
|
2022-09-16 16:56:49 +08:00
|
|
|
"github.com/milvus-io/milvus/api/schemapb"
|
2022-03-08 10:15:59 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/log"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2022-05-23 16:41:58 +08:00
|
|
|
benchmarkMaxNQ = 100
|
|
|
|
nb = 10000
|
2022-03-08 10:15:59 +08:00
|
|
|
)
|
|
|
|
|
2022-05-23 16:41:58 +08:00
|
|
|
func benchmarkQueryCollectionSearch(nq int64, b *testing.B) {
|
2022-03-08 10:15:59 +08:00
|
|
|
log.SetLevel(zapcore.ErrorLevel)
|
|
|
|
defer log.SetLevel(zapcore.DebugLevel)
|
|
|
|
|
|
|
|
tx, cancel := context.WithCancel(context.Background())
|
2022-05-23 16:41:58 +08:00
|
|
|
defer cancel()
|
|
|
|
queryShardObj, err := genSimpleQueryShard(tx)
|
2022-03-08 10:15:59 +08:00
|
|
|
assert.NoError(b, err)
|
|
|
|
|
|
|
|
// search only one segment
|
2022-05-31 13:42:03 +08:00
|
|
|
assert.Equal(b, 0, queryShardObj.metaReplica.getSegmentNum(segmentTypeSealed))
|
|
|
|
assert.Equal(b, 0, queryShardObj.metaReplica.getSegmentNum(segmentTypeGrowing))
|
2022-03-08 10:15:59 +08:00
|
|
|
|
2022-04-29 13:35:49 +08:00
|
|
|
segment, err := genSimpleSealedSegment(nb)
|
2022-03-08 10:15:59 +08:00
|
|
|
assert.NoError(b, err)
|
2022-05-31 13:42:03 +08:00
|
|
|
err = queryShardObj.metaReplica.setSegment(segment)
|
2022-03-08 10:15:59 +08:00
|
|
|
assert.NoError(b, err)
|
|
|
|
|
|
|
|
// segment check
|
2022-05-31 13:42:03 +08:00
|
|
|
assert.Equal(b, 1, queryShardObj.metaReplica.getSegmentNum(segmentTypeSealed))
|
|
|
|
assert.Equal(b, 0, queryShardObj.metaReplica.getSegmentNum(segmentTypeGrowing))
|
|
|
|
seg, err := queryShardObj.metaReplica.getSegmentByID(defaultSegmentID, segmentTypeSealed)
|
2022-03-08 10:15:59 +08:00
|
|
|
assert.NoError(b, err)
|
|
|
|
assert.Equal(b, int64(nb), seg.getRowCount())
|
2022-04-29 13:35:49 +08:00
|
|
|
|
|
|
|
// TODO:: check string data in segcore
|
|
|
|
//sizePerRecord, err := typeutil.EstimateSizePerRecord(genTestCollectionSchema(schemapb.DataType_Int64))
|
|
|
|
//assert.NoError(b, err)
|
|
|
|
//expectSize := sizePerRecord * nb
|
|
|
|
//assert.Equal(b, seg.getMemSize(), int64(expectSize))
|
2022-03-08 10:15:59 +08:00
|
|
|
|
|
|
|
// warming up
|
2022-03-08 17:39:58 +08:00
|
|
|
|
2022-05-31 13:42:03 +08:00
|
|
|
collection, err := queryShardObj.metaReplica.getCollectionByID(defaultCollectionID)
|
2022-04-29 13:35:49 +08:00
|
|
|
assert.NoError(b, err)
|
2022-05-23 16:41:58 +08:00
|
|
|
|
|
|
|
iReq, _ := genSearchRequest(nq, IndexFaissIDMap, collection.schema)
|
|
|
|
queryReq := &queryPb.SearchRequest{
|
|
|
|
Req: iReq,
|
2022-07-06 15:06:21 +08:00
|
|
|
DmlChannels: []string{defaultDMLChannel},
|
2022-05-23 16:41:58 +08:00
|
|
|
SegmentIDs: []UniqueID{defaultSegmentID},
|
|
|
|
FromShardLeader: true,
|
|
|
|
Scope: queryPb.DataScope_Historical,
|
|
|
|
}
|
|
|
|
searchReq, err := newSearchRequest(collection, queryReq, queryReq.Req.GetPlaceholderGroup())
|
2022-03-08 10:15:59 +08:00
|
|
|
assert.NoError(b, err)
|
|
|
|
for j := 0; j < 10000; j++ {
|
2022-08-23 10:44:52 +08:00
|
|
|
_, _, _, err := searchHistorical(context.TODO(), queryShardObj.metaReplica, searchReq, defaultCollectionID, nil, queryReq.GetSegmentIDs())
|
2022-03-08 10:15:59 +08:00
|
|
|
assert.NoError(b, err)
|
|
|
|
}
|
|
|
|
|
2022-05-23 16:41:58 +08:00
|
|
|
reqs := make([]*searchRequest, benchmarkMaxNQ/nq)
|
|
|
|
for i := 0; i < benchmarkMaxNQ/int(nq); i++ {
|
|
|
|
sReq, err := genSearchPlanAndRequests(collection, IndexFaissIDMap, nq)
|
2022-03-08 10:15:59 +08:00
|
|
|
assert.NoError(b, err)
|
2022-05-23 16:41:58 +08:00
|
|
|
reqs[i] = sReq
|
2022-03-08 10:15:59 +08:00
|
|
|
}
|
|
|
|
|
2022-05-23 16:41:58 +08:00
|
|
|
f, err := os.Create("nq_" + strconv.Itoa(int(nq)) + ".perf")
|
2022-03-08 10:15:59 +08:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
if err = pprof.StartCPUProfile(f); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
defer pprof.StopCPUProfile()
|
|
|
|
|
|
|
|
// start benchmark
|
|
|
|
b.ResetTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
2022-05-23 16:41:58 +08:00
|
|
|
for j := int64(0); j < benchmarkMaxNQ/nq; j++ {
|
2022-08-23 10:44:52 +08:00
|
|
|
_, _, _, err := searchHistorical(context.TODO(), queryShardObj.metaReplica, searchReq, defaultCollectionID, nil, queryReq.GetSegmentIDs())
|
2022-03-08 10:15:59 +08:00
|
|
|
assert.NoError(b, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-23 16:41:58 +08:00
|
|
|
func benchmarkQueryCollectionSearchIndex(nq int64, indexType string, b *testing.B) {
|
2022-03-08 10:15:59 +08:00
|
|
|
log.SetLevel(zapcore.ErrorLevel)
|
|
|
|
defer log.SetLevel(zapcore.DebugLevel)
|
|
|
|
|
|
|
|
tx, cancel := context.WithCancel(context.Background())
|
2022-05-23 16:41:58 +08:00
|
|
|
defer cancel()
|
|
|
|
queryShardObj, err := genSimpleQueryShard(tx)
|
2022-03-08 10:15:59 +08:00
|
|
|
assert.NoError(b, err)
|
|
|
|
|
2022-05-31 13:42:03 +08:00
|
|
|
assert.Equal(b, 0, queryShardObj.metaReplica.getSegmentNum(segmentTypeSealed))
|
|
|
|
assert.Equal(b, 0, queryShardObj.metaReplica.getSegmentNum(segmentTypeGrowing))
|
2022-03-08 10:15:59 +08:00
|
|
|
|
|
|
|
node, err := genSimpleQueryNode(tx)
|
|
|
|
assert.NoError(b, err)
|
2022-05-31 13:42:03 +08:00
|
|
|
node.loader.metaReplica = queryShardObj.metaReplica
|
2022-03-08 10:15:59 +08:00
|
|
|
|
2022-04-29 13:35:49 +08:00
|
|
|
err = loadIndexForSegment(tx, node, defaultSegmentID, nb, indexType, L2, schemapb.DataType_Int64)
|
2022-03-08 10:15:59 +08:00
|
|
|
assert.NoError(b, err)
|
|
|
|
|
|
|
|
// segment check
|
2022-05-31 13:42:03 +08:00
|
|
|
assert.Equal(b, 1, queryShardObj.metaReplica.getSegmentNum(segmentTypeSealed))
|
|
|
|
assert.Equal(b, 0, queryShardObj.metaReplica.getSegmentNum(segmentTypeGrowing))
|
|
|
|
seg, err := queryShardObj.metaReplica.getSegmentByID(defaultSegmentID, segmentTypeSealed)
|
2022-03-08 10:15:59 +08:00
|
|
|
assert.NoError(b, err)
|
|
|
|
assert.Equal(b, int64(nb), seg.getRowCount())
|
2022-04-29 13:35:49 +08:00
|
|
|
//TODO:: check string data in segcore
|
|
|
|
//sizePerRecord, err := typeutil.EstimateSizePerRecord(genSimpleSegCoreSchema())
|
|
|
|
//assert.NoError(b, err)
|
|
|
|
//expectSize := sizePerRecord * nb
|
|
|
|
//assert.Equal(b, seg.getMemSize(), int64(expectSize))
|
2022-03-08 10:15:59 +08:00
|
|
|
|
|
|
|
// warming up
|
2022-05-31 13:42:03 +08:00
|
|
|
collection, err := queryShardObj.metaReplica.getCollectionByID(defaultCollectionID)
|
2022-03-08 10:15:59 +08:00
|
|
|
assert.NoError(b, err)
|
2022-05-23 16:41:58 +08:00
|
|
|
|
|
|
|
//ollection *Collection, indexType string, nq int32
|
|
|
|
|
|
|
|
searchReq, _ := genSearchPlanAndRequests(collection, indexType, nq)
|
2022-03-08 10:15:59 +08:00
|
|
|
for j := 0; j < 10000; j++ {
|
2022-08-23 10:44:52 +08:00
|
|
|
_, _, _, err := searchHistorical(context.TODO(), queryShardObj.metaReplica, searchReq, defaultCollectionID, nil, []UniqueID{defaultSegmentID})
|
2022-03-08 10:15:59 +08:00
|
|
|
assert.NoError(b, err)
|
|
|
|
}
|
|
|
|
|
2022-05-23 16:41:58 +08:00
|
|
|
reqs := make([]*searchRequest, benchmarkMaxNQ/nq)
|
|
|
|
for i := int64(0); i < benchmarkMaxNQ/nq; i++ {
|
|
|
|
req, err := genSearchPlanAndRequests(collection, indexType, defaultNQ)
|
|
|
|
//msg, err := genSearchMsg(collection.schema, nq, indexType)
|
2022-03-08 10:15:59 +08:00
|
|
|
assert.NoError(b, err)
|
2022-05-23 16:41:58 +08:00
|
|
|
reqs[i] = req
|
2022-03-08 10:15:59 +08:00
|
|
|
}
|
|
|
|
|
2022-05-23 16:41:58 +08:00
|
|
|
f, err := os.Create(indexType + "_nq_" + strconv.Itoa(int(nq)) + ".perf")
|
2022-03-08 10:15:59 +08:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
if err = pprof.StartCPUProfile(f); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
defer pprof.StopCPUProfile()
|
|
|
|
|
|
|
|
// start benchmark
|
|
|
|
b.ResetTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
2022-05-23 16:41:58 +08:00
|
|
|
for j := 0; j < benchmarkMaxNQ/int(nq); j++ {
|
2022-08-23 10:44:52 +08:00
|
|
|
_, _, _, err := searchHistorical(context.TODO(), queryShardObj.metaReplica, searchReq, defaultCollectionID, nil, []UniqueID{defaultSegmentID})
|
2022-03-08 10:15:59 +08:00
|
|
|
assert.NoError(b, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-08 17:39:58 +08:00
|
|
|
func BenchmarkSearch_NQ1(b *testing.B) { benchmarkQueryCollectionSearch(1, b) }
|
|
|
|
|
|
|
|
//func BenchmarkSearch_NQ10(b *testing.B) { benchmarkQueryCollectionSearch(10, b) }
|
|
|
|
//func BenchmarkSearch_NQ100(b *testing.B) { benchmarkQueryCollectionSearch(100, b) }
|
|
|
|
//func BenchmarkSearch_NQ1000(b *testing.B) { benchmarkQueryCollectionSearch(1000, b) }
|
|
|
|
//func BenchmarkSearch_NQ10000(b *testing.B) { benchmarkQueryCollectionSearch(10000, b) }
|
|
|
|
|
|
|
|
func BenchmarkSearch_HNSW_NQ1(b *testing.B) {
|
|
|
|
benchmarkQueryCollectionSearchIndex(1, IndexHNSW, b)
|
|
|
|
}
|
2022-03-08 10:15:59 +08:00
|
|
|
|
|
|
|
func BenchmarkSearch_IVFFLAT_NQ1(b *testing.B) {
|
|
|
|
benchmarkQueryCollectionSearchIndex(1, IndexFaissIVFFlat, b)
|
|
|
|
}
|
2022-03-08 17:39:58 +08:00
|
|
|
|
|
|
|
/*
|
2022-03-08 10:15:59 +08:00
|
|
|
func BenchmarkSearch_IVFFLAT_NQ10(b *testing.B) {
|
|
|
|
benchmarkQueryCollectionSearchIndex(10, IndexFaissIVFFlat, b)
|
|
|
|
}
|
|
|
|
func BenchmarkSearch_IVFFLAT_NQ100(b *testing.B) {
|
|
|
|
benchmarkQueryCollectionSearchIndex(100, IndexFaissIVFFlat, b)
|
|
|
|
}
|
|
|
|
func BenchmarkSearch_IVFFLAT_NQ1000(b *testing.B) {
|
|
|
|
benchmarkQueryCollectionSearchIndex(1000, IndexFaissIVFFlat, b)
|
|
|
|
}
|
|
|
|
func BenchmarkSearch_IVFFLAT_NQ10000(b *testing.B) {
|
|
|
|
benchmarkQueryCollectionSearchIndex(10000, IndexFaissIVFFlat, b)
|
|
|
|
}
|
2022-03-08 17:39:58 +08:00
|
|
|
*/
|