Repository navigation
Expand file tree
/
Copy pathexample_benchmark.cpp
More file actions
306 lines (255 loc) · 13.8 KB
/
Copy pathexample_benchmark.cpp
File metadata and controls
306 lines (255 loc) · 13.8 KB
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
// Copyright 2026 momentics <momentics@gmail.com>
// Copyright libgsml3parser contributors
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
// Performance benchmark for parseL3() and L3StreamProcessor across all 13 PD
// domains (RR, MM, CC, SS, GMM, SM, SMS, BCC, GCC, LS, PDSS1, EXT, TST). Also includes a mixed-
// domain stream benchmark with all message types interleaved.
#include <chrono>
#include <cinttypes>
#include <cstdint>
#include <cstdio>
#include <cstring>
#include <vector>
#include "gsml3parser/parser.h"
#include "gsml3parser/bitstream/stream_processor.h"
#include "gsml3parser/bitstream/zero_copy_processor.h"
#include "gsml3parser/visitor.h"
#include "gsml3parser/benchmark_hw.h"
using namespace gsml3parser;
struct BenchmarkResult {
const char* name;
uint64_t messages;
double seconds;
uint64_t perSecond;
};
static void runParseBenchmark(const char* label, std::span<const uint8_t> singleMsg, uint64_t iterations) {
size_t totalSize = singleMsg.size() * iterations;
std::vector<uint8_t> data(totalSize);
uint8_t* ptr = data.data();
for (uint64_t i = 0; i < iterations; ++i) {
std::memcpy(ptr, singleMsg.data(), singleMsg.size());
ptr += singleMsg.size();
}
auto start = std::chrono::high_resolution_clock::now();
uint64_t ok = 0;
uint64_t errs = 0;
for (uint64_t i = 0; i < iterations; ++i) {
auto result = parseL3(std::span<const uint8_t>(data).subspan(i * singleMsg.size(), singleMsg.size()));
if (result) {
ok++;
} else {
errs++;
}
}
auto end = std::chrono::high_resolution_clock::now();
double secs = std::chrono::duration<double>(end - start).count();
uint64_t perSec = secs > 0 ? static_cast<uint64_t>(iterations / secs) : 0;
printf(" %-40s %" PRIu64 " msgs %8.4f s %" PRIu64 " msg/s (ok=%" PRIu64 " err=%" PRIu64 ")\n",
label, iterations, secs, perSec, ok, errs);
}
// Build L2-framed data: each message preceded by its length byte.
static std::vector<uint8_t> buildL2Data(const std::vector<std::pair<const uint8_t*, size_t>>& msgs, uint64_t iterations) {
size_t cycleSize = 0;
for (const auto& [p, n] : msgs) {
(void)p;
cycleSize += 1 + n; // length byte + message
}
std::vector<uint8_t> data(cycleSize * iterations);
uint8_t* ptr = data.data();
for (uint64_t i = 0; i < iterations; ++i) {
for (const auto& [p, n] : msgs) {
*ptr = static_cast<uint8_t>(n);
++ptr;
std::memcpy(ptr, p, n);
ptr += n;
}
}
return data;
}
static uint64_t runStreamBenchmark(const char* label, std::span<const uint8_t> singleMsg, uint64_t iterations) {
// L2-length framing: deterministic boundaries for any message type
// (header-based framing would silently drop frames for variable-length
// messages, so the benchmark checks the parsed count).
std::vector<std::pair<const uint8_t*, size_t>> one{ {singleMsg.data(), singleMsg.size()} };
auto data = buildL2Data(one, iterations);
SpanByteSource src(data);
auto start = std::chrono::high_resolution_clock::now();
struct CounterHandler : public FrameHandler {
uint64_t count{0};
void onFrame(const ParsedMessage&, const ExtractedFrame&) override { count++; }
void onError(const ParseError&, std::span<const uint8_t>) override {}
};
CounterHandler handler;
FrameConfig fcfg;
fcfg.useL2Length = true;
L3StreamProcessor proc(src, {}, fcfg);
proc.processUntilEOF(handler);
auto end = std::chrono::high_resolution_clock::now();
double secs = std::chrono::duration<double>(end - start).count();
uint64_t perSec = secs > 0 ? static_cast<uint64_t>(handler.count / secs) : 0;
printf(" %-40s %" PRIu64 " msgs %8.4f s %" PRIu64 " msg/s\n",
label, handler.count, secs, perSec);
return handler.count;
}
static uint64_t runZeroCopyBenchmark(const char* label, std::span<const uint8_t> data) {
auto start = std::chrono::high_resolution_clock::now();
ZeroCopyStreamProcessor proc(data, true);
uint64_t count = 0;
while (auto msg = proc.nextMessage()) {
(void)msg;
++count;
}
auto end = std::chrono::high_resolution_clock::now();
double secs = std::chrono::duration<double>(end - start).count();
uint64_t perSec = secs > 0 ? static_cast<uint64_t>(count / secs) : 0;
printf(" %-40s %" PRIu64 " msgs %8.4f s %" PRIu64 " msg/s (ok=%" PRIu64 " err=%" PRIu64 ")\n",
label, count + proc.stats().parseErrors, secs, perSec,
proc.stats().parsedOk, proc.stats().parseErrors);
// Count includes parse errors so the caller can verify that every
// framed message was observed.
return count + proc.stats().parseErrors;
}
int main() {
printf("=== libgsml3parser Benchmark (13 PD Domains) ===\n");
// Attribute results to the machine: performance depends on CPU/RAM/OS.
printf("Hardware: %s\n\n", benchmark::hardwareId().c_str());
// Representative messages for each PD domain.
// RR: Channel Release (3 bytes) - PD=0x6, MTI=0x0D
uint8_t rrMsg[] = {0x06, 0x0D, 0x00};
// MM: CM Service Accept (2 bytes) - PD=0x5, MTI=0x21
uint8_t mmMsg[] = {0x05, 0x21};
// CC: Disconnect — built wire-exact via the message builder
// the stream benchmark now uses L2-length framing, so every
// vector must be a valid complete message; the builder guarantees
// that).
auto ccBuilt = writeL3Bytes(ParsedMessage{CCM{L3Disconnect::builder().ti(3).build()}});
std::vector<uint8_t> ccMsg = *ccBuilt;
// SS: SupServ Facility (2 bytes) - PD=0xB, MTI=0x3A
uint8_t ssMsg[] = {0x0B, 0x3A};
// GMM: GMM Status (3 bytes) - PD=0x8, MTI=0x20
uint8_t gmmMsg[] = {0x08, 0x20, 0x05};
// SM: SM Status (4 bytes) - PD=0xA, MTI=0x55
uint8_t smMsg[] = {0x0A, 0x55, 0x32, 0x01};
// SMS: CP Ack (2 bytes) — CP-ACK has no body (TS 24.011 8.1.3;
// a 4-byte vector here would not be a valid CP-ACK and would parse as
// a HandoverAccess via the 4-byte short-message path).
uint8_t smsMsg[] = {0x09, 0x04};
// BCC: Setup (2 bytes) - PD=0x1, MTI=0x01
uint8_t bccMsg[] = {0x01, 0x01};
// GCC: Setup (3 bytes) - PD=0x0, MTI=0x01
uint8_t gccMsg[] = {0x00, 0x01, 0x02};
// LS: LocationServiceRequest (2 bytes) - PD=0x0c, MTI=0x01
uint8_t lsMsg[] = {0x0C, 0x01};
// PDSS1: PDSS1Message (3 bytes) - PD=0x0d, MTI=0x42
uint8_t pdss1Msg[] = {0x0D, 0x42, 0x11};
// EXT: ExtendedMessage (2 bytes) - PD=0x0e, MTI=0x01
uint8_t extMsg[] = {0x0E, 0x01};
// TST: TestProcedureMessage (2 bytes) - PD=0x0f, MTI=0x01
uint8_t tstMsg[] = {0x0F, 0x01};
uint64_t iterations = 500000;
printf("--- parseL3() Benchmark (%" PRIu64 " iterations each) ---\n", iterations);
runParseBenchmark("RR ChannelRelease", rrMsg, iterations);
runParseBenchmark("MM CMServiceAccept", mmMsg, iterations);
runParseBenchmark("CC Disconnect", ccMsg, iterations);
runParseBenchmark("SS SupServFacility", ssMsg, iterations);
runParseBenchmark("GMM GMMStatus", gmmMsg, iterations);
runParseBenchmark("SM SMStatus", smMsg, iterations);
runParseBenchmark("SMS CPAck", smsMsg, iterations);
runParseBenchmark("BCC Setup", bccMsg, iterations);
runParseBenchmark("GCC Setup", gccMsg, iterations);
runParseBenchmark("LS LocationServiceRequest", lsMsg, iterations);
runParseBenchmark("PDSS1 PDSS1Message", pdss1Msg, iterations);
runParseBenchmark("EXT ExtendedMessage", extMsg, iterations);
runParseBenchmark("TST TestProcedureMessage", tstMsg, iterations);
printf("\n--- L3StreamProcessor Benchmark (%" PRIu64 " iterations each) ---\n", iterations);
uint64_t streamCount;
streamCount = runStreamBenchmark("RR ChannelRelease", rrMsg, iterations);
if (streamCount != iterations) { printf("FAIL: RR stream lost frames (%" PRIu64 "/%" PRIu64 ")\n", streamCount, iterations); return 1; }
streamCount = runStreamBenchmark("MM CMServiceAccept", mmMsg, iterations);
if (streamCount != iterations) { printf("FAIL: MM stream lost frames (%" PRIu64 "/%" PRIu64 ")\n", streamCount, iterations); return 1; }
streamCount = runStreamBenchmark("CC Disconnect", ccMsg, iterations);
if (streamCount != iterations) { printf("FAIL: CC stream lost frames (%" PRIu64 "/%" PRIu64 ")\n", streamCount, iterations); return 1; }
streamCount = runStreamBenchmark("SS SupServFacility", ssMsg, iterations);
if (streamCount != iterations) { printf("FAIL: SS stream lost frames (%" PRIu64 "/%" PRIu64 ")\n", streamCount, iterations); return 1; }
streamCount = runStreamBenchmark("GMM GMMStatus", gmmMsg, iterations);
if (streamCount != iterations) { printf("FAIL: GMM stream lost frames (%" PRIu64 "/%" PRIu64 ")\n", streamCount, iterations); return 1; }
streamCount = runStreamBenchmark("SM SMStatus", smMsg, iterations);
if (streamCount != iterations) { printf("FAIL: SM stream lost frames (%" PRIu64 "/%" PRIu64 ")\n", streamCount, iterations); return 1; }
streamCount = runStreamBenchmark("SMS CPAck", smsMsg, iterations);
if (streamCount != iterations) { printf("FAIL: SMS stream lost frames (%" PRIu64 "/%" PRIu64 ")\n", streamCount, iterations); return 1; }
streamCount = runStreamBenchmark("BCC Setup", bccMsg, iterations);
if (streamCount != iterations) { printf("FAIL: BCC stream lost frames (%" PRIu64 "/%" PRIu64 ")\n", streamCount, iterations); return 1; }
streamCount = runStreamBenchmark("GCC Setup", gccMsg, iterations);
if (streamCount != iterations) { printf("FAIL: GCC stream lost frames (%" PRIu64 "/%" PRIu64 ")\n", streamCount, iterations); return 1; }
streamCount = runStreamBenchmark("LS LocationServiceRequest", lsMsg, iterations);
if (streamCount != iterations) { printf("FAIL: LS stream lost frames (%" PRIu64 "/%" PRIu64 ")\n", streamCount, iterations); return 1; }
streamCount = runStreamBenchmark("PDSS1 PDSS1Message", pdss1Msg, iterations);
if (streamCount != iterations) { printf("FAIL: PDSS1 stream lost frames (%" PRIu64 "/%" PRIu64 ")\n", streamCount, iterations); return 1; }
streamCount = runStreamBenchmark("EXT ExtendedMessage", extMsg, iterations);
if (streamCount != iterations) { printf("FAIL: EXT stream lost frames (%" PRIu64 "/%" PRIu64 ")\n", streamCount, iterations); return 1; }
streamCount = runStreamBenchmark("TST TestProcedureMessage", tstMsg, iterations);
if (streamCount != iterations) { printf("FAIL: TST stream lost frames (%" PRIu64 "/%" PRIu64 ")\n", streamCount, iterations); return 1; }
printf("\n--- Mixed stream Benchmark (All 13 PD Domains) ---\n");
// Build a mixed stream with all 13 message types interleaved
// (L2-length framing).
std::vector<std::pair<const uint8_t*, size_t>> allMsgs{
{rrMsg, sizeof(rrMsg)}, {mmMsg, sizeof(mmMsg)}, {ccMsg.data(), ccMsg.size()},
{ssMsg, sizeof(ssMsg)}, {gmmMsg, sizeof(gmmMsg)}, {smMsg, sizeof(smMsg)},
{smsMsg, sizeof(smsMsg)}, {bccMsg, sizeof(bccMsg)}, {gccMsg, sizeof(gccMsg)},
{lsMsg, sizeof(lsMsg)}, {pdss1Msg, sizeof(pdss1Msg)}, {extMsg, sizeof(extMsg)},
{tstMsg, sizeof(tstMsg)},
};
{
uint64_t mixedIters = iterations / 13;
auto data = buildL2Data(allMsgs, mixedIters);
SpanByteSource src(data);
auto start = std::chrono::high_resolution_clock::now();
struct CounterHandler : public FrameHandler {
uint64_t count{0};
void onFrame(const ParsedMessage&, const ExtractedFrame&) override { count++; }
void onError(const ParseError&, std::span<const uint8_t>) override {}
};
CounterHandler handler;
FrameConfig fcfg;
fcfg.useL2Length = true;
L3StreamProcessor proc(src, {}, fcfg);
proc.processUntilEOF(handler);
auto end = std::chrono::high_resolution_clock::now();
double secs = std::chrono::duration<double>(end - start).count();
uint64_t perSec = secs > 0 ? static_cast<uint64_t>(handler.count / secs) : 0;
printf(" %-40s %" PRIu64 " msgs %8.4f s %" PRIu64 " msg/s\n",
"Mixed (all 13 PD domains)", handler.count, secs, perSec);
if (handler.count != mixedIters * 13) {
printf("FAIL: mixed stream lost frames (%" PRIu64 "/%" PRIu64 ")\n",
handler.count, mixedIters * 13);
return 1;
}
}
// Zero-copy benchmark: compare against L3StreamProcessor for mixed stream.
printf("\n--- ZeroCopyStreamProcessor Benchmark (All 13 PD Domains) ---\n");
{
uint64_t mixedIters = iterations / 13;
auto l2Data = buildL2Data(allMsgs, mixedIters);
uint64_t zcCount = runZeroCopyBenchmark("Zero-copy (all 13 PD domains)", std::span<const uint8_t>(l2Data));
if (zcCount != mixedIters * 13) { printf("FAIL: zero-copy stream lost frames\n"); return 1; }
}
printf("\n=== Benchmark complete ===\n");
return 0;
}