aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/tests/queryeval/multibitvectoriterator/multibitvectoriterator_bench.cpp
blob: 95e80cd08b83949b18fc53c3bd650ad9af66daf2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include <vespa/vespalib/testkit/testapp.h>
#include <vespa/searchlib/queryeval/multibitvectoriterator.h>
#include <vespa/searchlib/queryeval/emptysearch.h>
#include <vespa/searchlib/common/bitvectoriterator.h>
#include <vespa/searchlib/queryeval/andsearch.h>
#include <vespa/searchlib/queryeval/orsearch.h>
#include <vespa/searchlib/fef/termfieldmatchdata.h>
#include <vespa/searchlib/fef/termfieldmatchdataarray.h>
#include <vespa/log/log.h>
LOG_SETUP("multibitvectoriterator_test");

using namespace search::queryeval;
using namespace search::fef;
using namespace search;

//-----------------------------------------------------------------------------

class Test : public vespalib::TestApp
{
public:
    ~Test() {}
    void benchmark();
    int Main() override;
    template <typename T>
    void testSearch(bool strict);
private:
    void searchAndCompare(SearchIterator::UP s, uint32_t docIdLimit);
    void setup();
    std::vector< BitVector::UP > _bvs;
    uint32_t         _numSearch;
    uint32_t         _numDocs;
    bool             _strict;
    bool             _optimize;
    vespalib::string _type;
    std::vector<int> _fillLimits;
};

void Test::setup()
{
    for(size_t i(0); i < _fillLimits.size(); i++) {
        _bvs.push_back(BitVector::create(_numDocs));
        BitVector & bv(*_bvs.back());
        for (size_t j(0); j < bv.size(); j++) {
            int r = rand();
            if (r < _fillLimits[i]) {
                bv.setBit(j);
            }
        }
        bv.invalidateCachedCount();
        LOG(info, "Filled bitvector %ld with %d bits", i, bv.countTrueBits());
    }
}

using H = std::vector<uint32_t>;

H
seek(SearchIterator & s, uint32_t docIdLimit)
{
    H h;
    for (uint32_t docId(0); docId < docIdLimit; ) {
        if (s.seek(docId)) {
            h.push_back(docId);
            docId++;
        } else {
            if (s.getDocId() > docId) {
                docId = s.getDocId();
            } else {
                docId++;
            }
        }
        //printf("docId = %u\n", docId);
    }
    return h;
}

void
Test::benchmark()
{
    if (_type == "and") {
        LOG(info, "Testing 'and'");
        for (size_t i(0); i < _numSearch; i++) {
            testSearch<AndSearch>(_strict);
        }
    } else {
        LOG(info, "Testing 'or'");
        for (size_t i(0); i < _numSearch; i++) {
            testSearch<OrSearch>(_strict);
        }
    }
}

template <typename T>
void
Test::testSearch(bool strict)
{
    TermFieldMatchData tfmd;
    MultiSearch::Children andd;
    for (size_t i(0); i < _bvs.size(); i++) {
        andd.push_back(BitVectorIterator::create(_bvs[i].get(), tfmd, strict, false));
    }
    SearchIterator::UP s = T::create(std::move(andd), strict);
    if (_optimize) {
        LOG(info, "Optimizing iterator");
        s = MultiBitVectorIteratorBase::optimize(std::move(s));
    }
    H h = seek(*s, _numDocs);
    LOG(info, "Found %ld hits", h.size());
}

int
Test::Main()
{
    TEST_INIT("multibitvectoriterator_benchmark");
    if (_argc < 6) {
        LOG(info, "%s <'and/or'> <'strict/no-strict'> <'optimize/no-optimize> <numsearch> <numdocs> <fill 1> [<fill N>]", _argv[0]);
        return -1;
    }
    _type = _argv[1];
    _strict = vespalib::string(_argv[2]) == vespalib::string("strict");
    _optimize = vespalib::string(_argv[3]) == vespalib::string("optimize");
    _numSearch = strtoul(_argv[4], NULL, 0);
    _numDocs = strtoul(_argv[5], NULL, 0);
    for (int i(6); i < _argc; i++) {
        _fillLimits.push_back((RAND_MAX/100) * strtoul(_argv[i], NULL, 0));
    }
    LOG(info, "Start setup of '%s' isearch with %ld vectors with %d documents", _type.c_str(), _fillLimits.size(), _numDocs);
    setup();
    LOG(info, "Start benchmark");
    benchmark();
    LOG(info, "Done benchmark");
    TEST_FLUSH();
    TEST_DONE();
}

TEST_APPHOOK(Test);