Skip to content

Commit 5fa5981

Browse files
authored
Added pure parsing benchmark (#1887)
* Added benchmark for pure parsing performance (all packets are preloaded into memory) * Lint * Update Examples/PcapPlusPlus-benchmark/README.md * Removed using namespace pcpp in new benchmark. * Update Examples/PcapPlusPlus-benchmark/benchmark-google.cpp
1 parent a5549c2 commit 5fa5981

File tree

2 files changed

+53
-7
lines changed

2 files changed

+53
-7
lines changed

Examples/PcapPlusPlus-benchmark/README.md

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@ A benchmark application used for measuring PcapPlusPlus performance can be found
99

1010
## Directly benchmark PcapPlusPlus
1111

12-
Another application integrates with the Google Benchmark library and can be found in `benchmark-google.cpp`. This application currently consists of four different benchmarks, and each benchmark can be influenced by various factors. These benchmarks aim to utilize different influence factors to provide accurate results for different scenarios. You can check the table below for more information. For performance-critical applications using PcapPlusPlus, it is recommended to run benchmarks in your specific environment for more accurate results. Using larger pcap files and those with diverse protocols and sessions can provide better insights into PcapPlusPlus performance in your setup.
12+
Another application integrates with the Google Benchmark library and can be found in `benchmark-google.cpp`. This application currently consists of five different benchmarks, and each benchmark can be influenced by various factors. These benchmarks aim to utilize different influence factors to provide accurate results for different scenarios. You can check the table below for more information. For performance-critical applications using PcapPlusPlus, it is recommended to run benchmarks in your specific environment for more accurate results. Using larger pcap files and those with diverse protocols and sessions can provide better insights into PcapPlusPlus performance in your setup.
1313

14-
| Benchmark | Operation | Influencing factors |
15-
|:-----------------:|:-------------:|:--------------------:|
16-
| BM_PcapFileRead | Read | CPU + Disk (Read) |
17-
| BM_PcapFileWrite | Write | CPU + Disk (Write) |
18-
| BM_PacketParsing | Read + Parse | CPU + Disk (Read) |
19-
| BM_PacketCrafting | Craft | CPU |
14+
| Benchmark | Operation | Influencing factors |
15+
|:-----------------: |:-------------:|:--------------------:|
16+
| BM_PcapFileRead | Read | CPU + Disk (Read) |
17+
| BM_PcapFileWrite | Write | CPU + Disk (Write) |
18+
| BM_PacketParsing | Read + Parse | CPU + Disk (Read) |
19+
| BM_PacketPureParsing | Parse | CPU |
20+
| BM_PacketCrafting | Craft | CPU |

Examples/PcapPlusPlus-benchmark/benchmark-google.cpp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,51 @@ static void BM_PacketParsing(benchmark::State& state)
142142
}
143143
BENCHMARK(BM_PacketParsing);
144144

145+
static void BM_PacketPureParsing(benchmark::State& state)
146+
{
147+
pcpp::PcapFileReaderDevice reader(pcapFileName);
148+
if (!reader.open())
149+
{
150+
state.SkipWithError("Cannot open pcap file for reading");
151+
return;
152+
}
153+
154+
// Preloads all packets into memory
155+
pcpp::RawPacketVector rawPackets;
156+
reader.getNextPackets(rawPackets);
157+
158+
if (rawPackets.size() == 0)
159+
{
160+
state.SkipWithError("No packets to parse");
161+
return;
162+
}
163+
164+
size_t totalProcessedItems = 0;
165+
size_t totalProcessedBytes = 0;
166+
size_t currentPacketIndex = 0;
167+
for (auto _ : state)
168+
{
169+
pcpp::RawPacket* rawPacket = rawPackets.at(currentPacketIndex);
170+
171+
pcpp::Packet parsedPacket(rawPacket);
172+
173+
benchmark::DoNotOptimize(parsedPacket.getFirstLayer());
174+
175+
++totalProcessedItems;
176+
totalProcessedBytes += rawPacket->getRawDataLen();
177+
++currentPacketIndex;
178+
179+
if (currentPacketIndex >= rawPackets.size())
180+
{
181+
currentPacketIndex = 0; // Loop back to the start
182+
}
183+
}
184+
185+
state.SetBytesProcessed(totalProcessedBytes);
186+
state.SetItemsProcessed(totalProcessedItems);
187+
}
188+
BENCHMARK(BM_PacketPureParsing);
189+
145190
static void BM_PacketCrafting(benchmark::State& state)
146191
{
147192
size_t totalBytes = 0;

0 commit comments

Comments
 (0)