aboutsummaryrefslogtreecommitdiff
path: root/icing/scoring/ranker_benchmark.cc
blob: c2f13de4bbf821847a87b3d16658d4a6c82ad76e (plain)
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// Copyright (C) 2019 Google LLC
//
// Licensed 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.

#include <cstdlib>
#include <random>

#include "testing/base/public/benchmark.h"
#include "icing/scoring/ranker.h"
#include "icing/scoring/scored-document-hit.h"

namespace icing {
namespace lib {

namespace {
// Run on a Linux workstation:
//    $ blaze build -c opt --dynamic_mode=off --copt=-gmlt
//    //icing/scoring:ranker_benchmark
//
//    $ blaze-bin/icing/scoring/ranker_benchmark --benchmark_filter=all
//    --benchmark_memory_usage
//
// Run on an Android device:
//    $ blaze build --copt="-DGOOGLE_COMMANDLINEFLAGS_FULL_API=1"
//    --config=android_arm64 -c opt --dynamic_mode=off --copt=-gmlt
//    //icing/scoring:ranker_benchmark
//
//    $ adb push blaze-bin/icing/scoring/ranker_benchmark
//    /data/local/tmp/
//
//    $ adb shell /data/local/tmp/ranker_benchmark --benchmark_filter=all

void BM_GetTopN(benchmark::State& state) {
  int num_to_score = state.range(0);
  int num_to_return = state.range(1);

  std::mt19937_64 random_generator;
  std::uniform_real_distribution<double> distribution(
      1, std::numeric_limits<double>::max());

  std::vector<ScoredDocumentHit> scored_document_hits;
  scored_document_hits.reserve(num_to_score);
  for (int i = 0; i < num_to_score; i++) {
    scored_document_hits.emplace_back(/*document_id=*/0,
                                      /*hit_section_id_mask=*/0,
                                      /*score=*/distribution(random_generator));
  }

  const ScoredDocumentHitComparator scored_document_hit_comparator(
      /*is_descending=*/true);

  for (auto _ : state) {
    // Pauses timer so that the cost of copying data is not included.
    state.PauseTiming();
    std::vector<ScoredDocumentHit> scored_document_hits_copy =
        scored_document_hits;
    state.ResumeTiming();

    BuildHeapInPlace(&scored_document_hits_copy,
                     scored_document_hit_comparator);
    auto result =
        PopTopResultsFromHeap(&scored_document_hits_copy, num_to_return,
                              scored_document_hit_comparator);
  }
}
BENCHMARK(BM_GetTopN)
    ->ArgPair(1000, 10)  // (num_to_score, num_to_return)
    ->ArgPair(3000, 10)
    ->ArgPair(5000, 10)
    ->ArgPair(7000, 10)
    ->ArgPair(9000, 10)
    ->ArgPair(11000, 10)
    ->ArgPair(13000, 10)
    ->ArgPair(15000, 10)
    ->ArgPair(17000, 10)
    ->ArgPair(19000, 10)
    ->ArgPair(1000, 20)
    ->ArgPair(3000, 20)
    ->ArgPair(5000, 20)
    ->ArgPair(7000, 20)
    ->ArgPair(9000, 20)
    ->ArgPair(11000, 20)
    ->ArgPair(13000, 20)
    ->ArgPair(15000, 20)
    ->ArgPair(17000, 20)
    ->ArgPair(19000, 20)
    ->ArgPair(1000, 30)
    ->ArgPair(3000, 30)
    ->ArgPair(5000, 30)
    ->ArgPair(7000, 30)
    ->ArgPair(9000, 30)
    ->ArgPair(11000, 30)
    ->ArgPair(13000, 30)
    ->ArgPair(15000, 30)
    ->ArgPair(17000, 30)
    ->ArgPair(19000, 30);

void BM_PopTopResultsFromHeap(benchmark::State& state) {
  int num_to_score = state.range(0);
  int num_to_return = state.range(1);

  std::mt19937_64 random_generator;
  std::uniform_real_distribution<double> distribution(
      1, std::numeric_limits<double>::max());

  std::vector<ScoredDocumentHit> scored_document_hits;
  scored_document_hits.reserve(num_to_score);
  for (int i = 0; i < num_to_score; i++) {
    scored_document_hits.emplace_back(/*document_id=*/0,
                                      /*hit_section_id_mask=*/0,
                                      /*score=*/distribution(random_generator));
  }

  const ScoredDocumentHitComparator scored_document_hit_comparator(
      /*is_descending=*/true);

  for (auto _ : state) {
    // Pauses timer so that the cost of copying data and building a heap are not
    // included.
    state.PauseTiming();
    std::vector<ScoredDocumentHit> scored_document_hits_copy =
        scored_document_hits;
    BuildHeapInPlace(&scored_document_hits_copy,
                     scored_document_hit_comparator);
    state.ResumeTiming();

    auto result =
        PopTopResultsFromHeap(&scored_document_hits_copy, num_to_return,
                              scored_document_hit_comparator);
  }
}
BENCHMARK(BM_PopTopResultsFromHeap)
    ->ArgPair(20000, 100)  // (num_to_score, num_to_return)
    ->ArgPair(20000, 300)
    ->ArgPair(20000, 500)
    ->ArgPair(20000, 700)
    ->ArgPair(20000, 900)
    ->ArgPair(20000, 1100)
    ->ArgPair(20000, 1300)
    ->ArgPair(20000, 1500)
    ->ArgPair(20000, 1700)
    ->ArgPair(20000, 1900);
}  // namespace

}  // namespace lib
}  // namespace icing