xref: /aosp_15_r20/external/pigweed/pw_checksum/crc32_perf_test.cc (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1 // Copyright 2022 The Pigweed Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 // use this file except in compliance with the License. You may obtain a copy of
5 // the License at
6 //
7 //     https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 // License for the specific language governing permissions and limitations under
13 // the License.
14 
15 #include <cstdint>
16 #include <string_view>
17 
18 #include "pw_bytes/array.h"
19 #include "pw_checksum/crc32.h"
20 #include "pw_perf_test/perf_test.h"
21 #include "pw_span/span.h"
22 
23 namespace pw::checksum {
24 namespace {
25 
26 constexpr std::string_view kString =
27     "In the beginning the Universe was created. This has made a lot of "
28     "people very angry and been widely regarded as a bad move.";
29 constexpr auto kBytes = bytes::Array<1, 2, 3, 4, 5, 6, 7, 8, 9>();
30 
Crc32OneBitTest(perf_test::State & state,span<const std::byte> data)31 void Crc32OneBitTest(perf_test::State& state, span<const std::byte> data) {
32   while (state.KeepRunning()) {
33     Crc32OneBit::Calculate(data);
34   }
35 }
36 
Crc32FourBitTest(perf_test::State & state,span<const std::byte> data)37 void Crc32FourBitTest(perf_test::State& state, span<const std::byte> data) {
38   while (state.KeepRunning()) {
39     Crc32FourBit::Calculate(data);
40   }
41 }
42 
Crc32EightBitTest(perf_test::State & state,span<const std::byte> data)43 void Crc32EightBitTest(perf_test::State& state, span<const std::byte> data) {
44   while (state.KeepRunning()) {
45     Crc32EightBit::Calculate(data);
46   }
47 }
48 
49 PW_PERF_TEST(CrcOneBitStringTest, Crc32OneBitTest, as_bytes(span(kString)));
50 PW_PERF_TEST(CrcFourBitStringTest, Crc32FourBitTest, as_bytes(span(kString)));
51 PW_PERF_TEST(CrcEightBitStringTest, Crc32EightBitTest, as_bytes(span(kString)));
52 
53 PW_PERF_TEST(CrcOneBitBytesTest, Crc32OneBitTest, kBytes);
54 PW_PERF_TEST(CrcFourBitBytesTest, Crc32FourBitTest, kBytes);
55 PW_PERF_TEST(CrcEightBitBytesTest, Crc32EightBitTest, kBytes);
56 
57 }  // namespace
58 }  // namespace pw::checksum
59