xref: /aosp_15_r20/external/federated-compute/fcp/secagg/shared/add_maps_bench.cc (revision 14675a029014e728ec732f129a32e299b2da0601)
1 /*
2  * Copyright 2021 Google LLC
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     https://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <cstdint>
18 #include <memory>
19 #include <vector>
20 
21 #include "fcp/secagg/shared/map_of_masks.h"
22 #include "fcp/secagg/shared/secagg_vector.h"
23 
24 // Open-source version of benchmarking library
25 #include "benchmark/benchmark.h"
26 
27 namespace fcp {
28 namespace secagg {
29 namespace {
30 
31 // Open-source version of benchmarking library
32 using benchmark::internal::Benchmark;
33 
34 // This function produces varied pairs of {bit_width, size} for the benchmark.
CustomArguments(Benchmark * b)35 static void CustomArguments(Benchmark* b) {
36   constexpr int bit_widths[] = {8, 25, 38, 53,
37                                 absl::bit_width(SecAggVector::kMaxModulus - 1)};
38   for (int bit_width : bit_widths) {
39     for (int size = 32; size <= 32 * 1024 * 1024; size *= 32) {
40       b->ArgPair(bit_width, size);
41     }
42   }
43 }
44 
MakeMap(int64_t bit_width,int64_t size,uint64_t start,uint64_t step)45 std::unique_ptr<SecAggVectorMap> MakeMap(int64_t bit_width, int64_t size,
46                                          uint64_t start, uint64_t step) {
47   std::vector<uint64_t> vec;
48   vec.resize(size);
49 
50   uint64_t modulus = 1ULL << bit_width;
51   uint64_t v = start;
52   for (int64_t i = 0; i < size; i++) {
53     vec[i] = v;
54     v = (v + step) % modulus;
55   }
56 
57   auto map = std::make_unique<SecAggVectorMap>();
58   map->emplace("test", SecAggVector(vec, modulus));
59   return map;
60 }
61 
BM_AddMaps(benchmark::State & state)62 void BM_AddMaps(benchmark::State& state) {
63   auto map_a = MakeMap(state.range(0), state.range(1), 1, 1);
64   auto map_b = MakeMap(state.range(0), state.range(1), 2, 3);
65   for (auto _ : state) {
66     auto map_sum = AddMaps(*map_a, *map_b);
67     benchmark::DoNotOptimize(map_sum);
68   }
69 }
70 
71 BENCHMARK(BM_AddMaps)->Apply(CustomArguments);
72 
73 }  // namespace
74 }  // namespace secagg
75 }  // namespace fcp
76