xref: /aosp_15_r20/external/federated-compute/fcp/secagg/shared/secagg_vector_bench.cc (revision 14675a029014e728ec732f129a32e299b2da0601)
1 /*
2  * Copyright 2020 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 <vector>
19 
20 #include "benchmark/benchmark.h"
21 #include "fcp/secagg/shared/secagg_vector.h"
22 
23 namespace fcp {
24 namespace secagg {
25 namespace {
26 
27 constexpr auto kVectorSize = 32 * 1024 * 1024;
28 
BM_CreatePowerOfTwo(benchmark::State & state)29 static void BM_CreatePowerOfTwo(benchmark::State& state) {
30   auto items_processed = 0;
31   std::vector<uint64_t> input;
32   input.resize(kVectorSize);
33   for (auto s : state) {
34     uint64_t modulus = 1ULL << static_cast<int>(state.range(1));
35     SecAggVector vec(input, modulus, state.range(0));
36     benchmark::DoNotOptimize(vec.GetAsUint64Vector());
37     items_processed += vec.num_elements();
38   }
39   state.SetItemsProcessed(items_processed);
40 }
41 
BM_CreateArbitrary(benchmark::State & state)42 static void BM_CreateArbitrary(benchmark::State& state) {
43   auto items_processed = 0;
44   std::vector<uint64_t> input;
45   input.resize(kVectorSize);
46   for (auto s : state) {
47     uint64_t modulus = static_cast<uint64_t>(state.range(1));
48     SecAggVector vec(input, modulus, state.range(0));
49     benchmark::DoNotOptimize(vec.GetAsUint64Vector());
50     items_processed += vec.num_elements();
51   }
52   state.SetItemsProcessed(items_processed);
53 }
54 
55 BENCHMARK(BM_CreatePowerOfTwo)
56     ->RangeMultiplier(2)
57     ->Ranges({{false, true},
58               {1, absl::bit_width(SecAggVector::kMaxModulus - 1ULL)}});
59 
60 BENCHMARK(BM_CreatePowerOfTwo)->Args({false, 41})->Args({true, 41});
61 
62 BENCHMARK(BM_CreateArbitrary)
63     ->Args({false, 5})
64     ->Args({false, 39})
65     ->Args({false, 485})
66     ->Args({false, 2400})
67     ->Args({false, 14901})
68     ->Args({false, 51813})
69     ->Args({false, 532021})
70     ->Args({false, 13916946})
71     ->Args({false, 39549497})
72     ->Args({false, 548811945})
73     ->Args({false, 590549014})
74     ->Args({false, 48296031686})
75     ->Args({false, 156712951284})
76     ->Args({false, 2636861836189})
77     ->Args({false, 14673852658160})
78     ->Args({false, 92971495438615})
79     ->Args({false, 304436005557271})
80     ->Args({false, 14046234330484262})
81     ->Args({false, 38067457113486645})
82     ->Args({false, 175631339105057682})
83     ->Args({true, 5})
84     ->Args({true, 39})
85     ->Args({true, 485})
86     ->Args({true, 2400})
87     ->Args({true, 14901})
88     ->Args({true, 51813})
89     ->Args({true, 532021})
90     ->Args({true, 13916946})
91     ->Args({true, 39549497})
92     ->Args({true, 548811945})
93     ->Args({true, 590549014})
94     ->Args({true, 48296031686})
95     ->Args({true, 156712951284})
96     ->Args({true, 2636861836189})
97     ->Args({true, 14673852658160})
98     ->Args({true, 92971495438615})
99     ->Args({true, 304436005557271})
100     ->Args({true, 14046234330484262})
101     ->Args({true, 38067457113486645})
102     ->Args({true, 175631339105057682});
103 
104 }  // namespace
105 }  // namespace secagg
106 }  // namespace fcp
107