-
Notifications
You must be signed in to change notification settings - Fork 398
Expand file tree
/
Copy pathbenchmark.cc
More file actions
1238 lines (1089 loc) · 41.3 KB
/
benchmark.cc
File metadata and controls
1238 lines (1089 loc) · 41.3 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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
#include <benchmark/benchmark.h>
#include <cstdint>
#include <msgpack.hpp>
#include <string>
#include <vector>
#include "bench.pb.h"
#include "fory/serialization/context.h"
#include "fory/serialization/fory.h"
#include "fory/serialization/struct_serializer.h"
// ============================================================================
// Fory struct definitions (must match proto messages)
// ============================================================================
struct NumericStruct {
int32_t f1;
int32_t f2;
int32_t f3;
int32_t f4;
int32_t f5;
int32_t f6;
int32_t f7;
int32_t f8;
bool operator==(const NumericStruct &other) const {
return f1 == other.f1 && f2 == other.f2 && f3 == other.f3 &&
f4 == other.f4 && f5 == other.f5 && f6 == other.f6 &&
f7 == other.f7 && f8 == other.f8;
}
MSGPACK_DEFINE_MAP(f1, f2, f3, f4, f5, f6, f7, f8);
};
FORY_STRUCT(NumericStruct, f1, f2, f3, f4, f5, f6, f7, f8);
FORY_FIELD_TAGS(NumericStruct, (f1, 1), (f2, 2), (f3, 3), (f4, 4), (f5, 5),
(f6, 6), (f7, 7), (f8, 8));
struct Sample {
int32_t int_value;
int64_t long_value;
float float_value;
double double_value;
int32_t short_value;
int32_t char_value;
bool boolean_value;
int32_t int_value_boxed;
int64_t long_value_boxed;
float float_value_boxed;
double double_value_boxed;
int32_t short_value_boxed;
int32_t char_value_boxed;
bool boolean_value_boxed;
std::vector<int32_t> int_array;
std::vector<int64_t> long_array;
std::vector<float> float_array;
std::vector<double> double_array;
std::vector<int32_t> short_array;
std::vector<int32_t> char_array;
std::vector<bool> boolean_array;
std::string string;
bool operator==(const Sample &other) const {
return int_value == other.int_value && long_value == other.long_value &&
float_value == other.float_value &&
double_value == other.double_value &&
short_value == other.short_value && char_value == other.char_value &&
boolean_value == other.boolean_value &&
int_value_boxed == other.int_value_boxed &&
long_value_boxed == other.long_value_boxed &&
float_value_boxed == other.float_value_boxed &&
double_value_boxed == other.double_value_boxed &&
short_value_boxed == other.short_value_boxed &&
char_value_boxed == other.char_value_boxed &&
boolean_value_boxed == other.boolean_value_boxed &&
int_array == other.int_array && long_array == other.long_array &&
float_array == other.float_array &&
double_array == other.double_array &&
short_array == other.short_array && char_array == other.char_array &&
boolean_array == other.boolean_array && string == other.string;
}
MSGPACK_DEFINE_MAP(int_value, long_value, float_value, double_value,
short_value, char_value, boolean_value, int_value_boxed,
long_value_boxed, float_value_boxed, double_value_boxed,
short_value_boxed, char_value_boxed, boolean_value_boxed,
int_array, long_array, float_array, double_array,
short_array, char_array, boolean_array, string);
};
FORY_STRUCT(Sample, int_value, long_value, float_value, double_value,
short_value, char_value, boolean_value, int_value_boxed,
long_value_boxed, float_value_boxed, double_value_boxed,
short_value_boxed, char_value_boxed, boolean_value_boxed, int_array,
long_array, float_array, double_array, short_array, char_array,
boolean_array, string);
FORY_FIELD_TAGS(Sample, (int_value, 1), (long_value, 2), (float_value, 3),
(double_value, 4), (short_value, 5), (char_value, 6),
(boolean_value, 7), (int_value_boxed, 8), (long_value_boxed, 9),
(float_value_boxed, 10), (double_value_boxed, 11),
(short_value_boxed, 12), (char_value_boxed, 13),
(boolean_value_boxed, 14), (int_array, 15), (long_array, 16),
(float_array, 17), (double_array, 18), (short_array, 19),
(char_array, 20), (boolean_array, 21), (string, 22));
// Enums for MediaContent benchmark
enum class Player : int32_t { JAVA = 0, FLASH = 1 };
MSGPACK_ADD_ENUM(Player);
enum class Size : int32_t { SMALL = 0, LARGE = 1 };
MSGPACK_ADD_ENUM(Size);
struct Media {
std::string uri;
std::string title; // Can be empty (null equivalent)
int32_t width;
int32_t height;
std::string format;
int64_t duration;
int64_t size;
int32_t bitrate;
bool has_bitrate;
std::vector<std::string> persons;
Player player;
std::string copyright;
bool operator==(const Media &other) const {
return uri == other.uri && title == other.title && width == other.width &&
height == other.height && format == other.format &&
duration == other.duration && size == other.size &&
bitrate == other.bitrate && has_bitrate == other.has_bitrate &&
persons == other.persons && player == other.player &&
copyright == other.copyright;
}
MSGPACK_DEFINE_MAP(uri, title, width, height, format, duration, size, bitrate,
has_bitrate, persons, player, copyright);
};
FORY_STRUCT(Media, uri, title, width, height, format, duration, size, bitrate,
has_bitrate, persons, player, copyright);
FORY_FIELD_TAGS(Media, (uri, 1), (title, 2), (width, 3), (height, 4),
(format, 5), (duration, 6), (size, 7), (bitrate, 8),
(has_bitrate, 9), (persons, 10), (player, 11), (copyright, 12));
struct Image {
std::string uri;
std::string title; // Can be empty (null equivalent)
int32_t width;
int32_t height;
Size size;
bool operator==(const Image &other) const {
return uri == other.uri && title == other.title && width == other.width &&
height == other.height && size == other.size;
}
MSGPACK_DEFINE_MAP(uri, title, width, height, size);
};
FORY_STRUCT(Image, uri, title, width, height, size);
FORY_FIELD_TAGS(Image, (uri, 1), (title, 2), (width, 3), (height, 4),
(size, 5));
struct MediaContent {
Media media;
std::vector<Image> images;
bool operator==(const MediaContent &other) const {
return media == other.media && images == other.images;
}
MSGPACK_DEFINE_MAP(media, images);
};
FORY_STRUCT(MediaContent, media, images);
FORY_FIELD_TAGS(MediaContent, (media, 1), (images, 2));
struct StructList {
std::vector<NumericStruct> struct_list;
bool operator==(const StructList &other) const {
return struct_list == other.struct_list;
}
MSGPACK_DEFINE_MAP(struct_list);
};
FORY_STRUCT(StructList, struct_list);
FORY_FIELD_TAGS(StructList, (struct_list, 1));
struct SampleList {
std::vector<Sample> sample_list;
bool operator==(const SampleList &other) const {
return sample_list == other.sample_list;
}
MSGPACK_DEFINE_MAP(sample_list);
};
FORY_STRUCT(SampleList, sample_list);
FORY_FIELD_TAGS(SampleList, (sample_list, 1));
struct MediaContentList {
std::vector<MediaContent> media_content_list;
bool operator==(const MediaContentList &other) const {
return media_content_list == other.media_content_list;
}
MSGPACK_DEFINE_MAP(media_content_list);
};
FORY_STRUCT(MediaContentList, media_content_list);
FORY_FIELD_TAGS(MediaContentList, (media_content_list, 1));
// ============================================================================
// Test data creation
// ============================================================================
NumericStruct create_numeric_struct() {
// Use mixed positive/negative int32 values for realistic benchmark
return NumericStruct{
-12345, // f1: negative
987654321, // f2: large positive
-31415, // f3: negative
27182818, // f4: positive
-32000, // f5: negative (near int16 min)
1000000, // f6: medium positive
-999999999, // f7: large negative
42 // f8: small positive
};
}
constexpr int kListSize = 5;
// ============================================================================
// Protobuf conversion functions (like Java benchmark's
// buildPBStruct/fromPBObject)
// ============================================================================
/// Convert plain C++ struct to protobuf message (for serialization)
inline protobuf::Struct to_pb_struct(const NumericStruct &obj) {
protobuf::Struct pb;
pb.set_f1(obj.f1);
pb.set_f2(obj.f2);
pb.set_f3(obj.f3);
pb.set_f4(obj.f4);
pb.set_f5(obj.f5);
pb.set_f6(obj.f6);
pb.set_f7(obj.f7);
pb.set_f8(obj.f8);
return pb;
}
/// Convert protobuf message to plain C++ struct (for deserialization)
inline NumericStruct from_pb_struct(const protobuf::Struct &pb) {
NumericStruct obj;
obj.f1 = pb.f1();
obj.f2 = pb.f2();
obj.f3 = pb.f3();
obj.f4 = pb.f4();
obj.f5 = pb.f5();
obj.f6 = pb.f6();
obj.f7 = pb.f7();
obj.f8 = pb.f8();
return obj;
}
protobuf::Struct create_proto_struct() {
return to_pb_struct(create_numeric_struct());
}
Sample create_sample() {
// Consistent with Java Sample.populate() for fair cross-language comparison
Sample sample;
sample.int_value = 123;
sample.long_value = 1230000LL;
sample.float_value = 12.345f;
sample.double_value = 1.234567;
sample.short_value = 12345;
sample.char_value = '!'; // 33
sample.boolean_value = true;
sample.int_value_boxed = 321;
sample.long_value_boxed = 3210000LL;
sample.float_value_boxed = 54.321f;
sample.double_value_boxed = 7.654321;
sample.short_value_boxed = 32100;
sample.char_value_boxed = '$'; // 36
sample.boolean_value_boxed = false;
// Arrays with mixed positive/negative values (same as Java)
sample.int_array = {-1234, -123, -12, -1, 0, 1, 12, 123, 1234};
sample.long_array = {-123400, -12300, -1200, -100, 0,
100, 1200, 12300, 123400};
sample.float_array = {-12.34f, -12.3f, -12.0f, -1.0f, 0.0f,
1.0f, 12.0f, 12.3f, 12.34f};
sample.double_array = {-1.234, -1.23, -12.0, -1.0, 0.0,
1.0, 12.0, 1.23, 1.234};
sample.short_array = {-1234, -123, -12, -1, 0, 1, 12, 123, 1234};
sample.char_array = {'a', 's', 'd', 'f', 'A', 'S', 'D', 'F'}; // "asdfASDF"
sample.boolean_array = {true, false, false, true};
sample.string = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
return sample;
}
inline protobuf::Sample to_pb_sample(const Sample &obj) {
protobuf::Sample sample;
sample.set_int_value(obj.int_value);
sample.set_long_value(obj.long_value);
sample.set_float_value(obj.float_value);
sample.set_double_value(obj.double_value);
sample.set_short_value(obj.short_value);
sample.set_char_value(obj.char_value);
sample.set_boolean_value(obj.boolean_value);
sample.set_int_value_boxed(obj.int_value_boxed);
sample.set_long_value_boxed(obj.long_value_boxed);
sample.set_float_value_boxed(obj.float_value_boxed);
sample.set_double_value_boxed(obj.double_value_boxed);
sample.set_short_value_boxed(obj.short_value_boxed);
sample.set_char_value_boxed(obj.char_value_boxed);
sample.set_boolean_value_boxed(obj.boolean_value_boxed);
for (int32_t v : obj.int_array) {
sample.add_int_array(v);
}
for (int64_t v : obj.long_array) {
sample.add_long_array(v);
}
for (float v : obj.float_array) {
sample.add_float_array(v);
}
for (double v : obj.double_array) {
sample.add_double_array(v);
}
for (int32_t v : obj.short_array) {
sample.add_short_array(v);
}
for (int32_t v : obj.char_array) {
sample.add_char_array(v);
}
for (bool v : obj.boolean_array) {
sample.add_boolean_array(v);
}
sample.set_string(obj.string);
return sample;
}
inline Sample from_pb_sample(const protobuf::Sample &pb) {
Sample sample;
sample.int_value = pb.int_value();
sample.long_value = pb.long_value();
sample.float_value = pb.float_value();
sample.double_value = pb.double_value();
sample.short_value = pb.short_value();
sample.char_value = pb.char_value();
sample.boolean_value = pb.boolean_value();
sample.int_value_boxed = pb.int_value_boxed();
sample.long_value_boxed = pb.long_value_boxed();
sample.float_value_boxed = pb.float_value_boxed();
sample.double_value_boxed = pb.double_value_boxed();
sample.short_value_boxed = pb.short_value_boxed();
sample.char_value_boxed = pb.char_value_boxed();
sample.boolean_value_boxed = pb.boolean_value_boxed();
sample.int_array.assign(pb.int_array().begin(), pb.int_array().end());
sample.long_array.assign(pb.long_array().begin(), pb.long_array().end());
sample.float_array.assign(pb.float_array().begin(), pb.float_array().end());
sample.double_array.assign(pb.double_array().begin(),
pb.double_array().end());
sample.short_array.assign(pb.short_array().begin(), pb.short_array().end());
sample.char_array.assign(pb.char_array().begin(), pb.char_array().end());
sample.boolean_array.assign(pb.boolean_array().begin(),
pb.boolean_array().end());
sample.string = pb.string();
return sample;
}
protobuf::Sample create_proto_sample() {
// Consistent with Java Sample.populate() for fair cross-language comparison
return to_pb_sample(create_sample());
}
MediaContent create_media_content() {
// Matches Java MediaContent.populate(false) - no circular reference
MediaContent content;
// Media fields matching Java populate()
content.media.uri = "http://javaone.com/keynote.ogg";
content.media.title = ""; // null in Java
content.media.width = 641;
content.media.height = 481;
content.media.format = u8"video/theora\u1234"; // UTF-8 encoded unicode
content.media.duration = 18000001;
content.media.size = 58982401;
content.media.bitrate = 0;
content.media.has_bitrate = false;
content.media.persons = {"Bill Gates, Jr.", "Steven Jobs"};
content.media.player = Player::FLASH;
content.media.copyright = "Copyright (c) 2009, Scooby Dooby Doo";
// Images matching Java populate(false) - no circular reference
content.images = {
Image{"http://javaone.com/keynote_huge.jpg", u8"Javaone Keynote\u1234",
32000, 24000, Size::LARGE},
Image{"http://javaone.com/keynote_large.jpg", "", 1024, 768, Size::LARGE},
Image{"http://javaone.com/keynote_small.jpg", "", 320, 240, Size::SMALL}};
return content;
}
/// Convert Image to protobuf
inline protobuf::Image to_pb_image(const Image &img) {
protobuf::Image pb;
pb.set_uri(img.uri);
if (!img.title.empty()) {
pb.set_title(img.title);
}
pb.set_width(img.width);
pb.set_height(img.height);
pb.set_size(static_cast<protobuf::Size>(img.size));
return pb;
}
/// Convert Media to protobuf
inline protobuf::Media to_pb_media(const Media &m) {
protobuf::Media pb;
pb.set_uri(m.uri);
if (!m.title.empty()) {
pb.set_title(m.title);
}
pb.set_width(m.width);
pb.set_height(m.height);
pb.set_format(m.format);
pb.set_duration(m.duration);
pb.set_size(m.size);
pb.set_bitrate(m.bitrate);
pb.set_has_bitrate(m.has_bitrate);
for (const auto &person : m.persons) {
pb.add_persons(person);
}
pb.set_player(static_cast<protobuf::Player>(m.player));
pb.set_copyright(m.copyright);
return pb;
}
/// Convert MediaContent to protobuf
inline protobuf::MediaContent to_pb_mediaContent(const MediaContent &mc) {
protobuf::MediaContent pb;
*pb.mutable_media() = to_pb_media(mc.media);
for (const auto &img : mc.images) {
*pb.add_images() = to_pb_image(img);
}
return pb;
}
/// Convert protobuf to Image
inline Image from_pb_image(const protobuf::Image &pb) {
Image img;
img.uri = pb.uri();
img.title = pb.has_title() ? pb.title() : "";
img.width = pb.width();
img.height = pb.height();
img.size = static_cast<Size>(pb.size());
return img;
}
/// Convert protobuf to Media
inline Media from_pb_media(const protobuf::Media &pb) {
Media m;
m.uri = pb.uri();
m.title = pb.has_title() ? pb.title() : "";
m.width = pb.width();
m.height = pb.height();
m.format = pb.format();
m.duration = pb.duration();
m.size = pb.size();
m.bitrate = pb.bitrate();
m.has_bitrate = pb.has_bitrate();
for (const auto &person : pb.persons()) {
m.persons.push_back(person);
}
m.player = static_cast<Player>(pb.player());
m.copyright = pb.copyright();
return m;
}
/// Convert protobuf to MediaContent
inline MediaContent from_pb_mediaContent(const protobuf::MediaContent &pb) {
MediaContent mc;
mc.media = from_pb_media(pb.media());
for (const auto &img : pb.images()) {
mc.images.push_back(from_pb_image(img));
}
return mc;
}
protobuf::MediaContent create_proto_media_content() {
return to_pb_mediaContent(create_media_content());
}
StructList create_struct_list() {
StructList list;
list.struct_list.reserve(kListSize);
for (int i = 0; i < kListSize; ++i) {
list.struct_list.push_back(create_numeric_struct());
}
return list;
}
SampleList create_sample_list() {
SampleList list;
list.sample_list.reserve(kListSize);
for (int i = 0; i < kListSize; ++i) {
list.sample_list.push_back(create_sample());
}
return list;
}
MediaContentList create_media_content_list() {
MediaContentList list;
list.media_content_list.reserve(kListSize);
for (int i = 0; i < kListSize; ++i) {
list.media_content_list.push_back(create_media_content());
}
return list;
}
inline protobuf::StructList to_pb_struct_list(const StructList &obj) {
protobuf::StructList pb;
for (const auto &item : obj.struct_list) {
*pb.add_struct_list() = to_pb_struct(item);
}
return pb;
}
inline StructList from_pb_struct_list(const protobuf::StructList &pb) {
StructList list;
list.struct_list.reserve(pb.struct_list_size());
for (const auto &item : pb.struct_list()) {
list.struct_list.push_back(from_pb_struct(item));
}
return list;
}
inline protobuf::SampleList to_pb_sample_list(const SampleList &obj) {
protobuf::SampleList pb;
for (const auto &item : obj.sample_list) {
*pb.add_sample_list() = to_pb_sample(item);
}
return pb;
}
inline SampleList from_pb_sample_list(const protobuf::SampleList &pb) {
SampleList list;
list.sample_list.reserve(pb.sample_list_size());
for (const auto &item : pb.sample_list()) {
list.sample_list.push_back(from_pb_sample(item));
}
return list;
}
inline protobuf::MediaContentList
to_pb_media_content_list(const MediaContentList &obj) {
protobuf::MediaContentList pb;
for (const auto &item : obj.media_content_list) {
*pb.add_media_content_list() = to_pb_mediaContent(item);
}
return pb;
}
inline MediaContentList
from_pb_media_content_list(const protobuf::MediaContentList &pb) {
MediaContentList list;
list.media_content_list.reserve(pb.media_content_list_size());
for (const auto &item : pb.media_content_list()) {
list.media_content_list.push_back(from_pb_mediaContent(item));
}
return list;
}
protobuf::StructList create_proto_struct_list() {
return to_pb_struct_list(create_struct_list());
}
protobuf::SampleList create_proto_sample_list() {
return to_pb_sample_list(create_sample_list());
}
protobuf::MediaContentList create_proto_media_content_list() {
return to_pb_media_content_list(create_media_content_list());
}
// ============================================================================
// Helper to configure Fory instance
// ============================================================================
void register_fory_types(fory::serialization::Fory &fory) {
fory.register_struct<NumericStruct>(1);
fory.register_struct<Sample>(2);
fory.register_struct<Media>(3);
fory.register_struct<Image>(4);
fory.register_struct<MediaContent>(5);
fory.register_struct<StructList>(6);
fory.register_struct<SampleList>(7);
fory.register_struct<MediaContentList>(8);
}
template <typename T, typename Factory>
void run_msgpack_serialize_benchmark(benchmark::State &state, Factory factory) {
T obj = factory();
msgpack::sbuffer output;
for (auto _ : state) {
output.clear();
msgpack::pack(output, obj);
benchmark::DoNotOptimize(output.data());
benchmark::DoNotOptimize(output.size());
}
}
template <typename T, typename Factory>
void run_msgpack_deserialize_benchmark(benchmark::State &state,
Factory factory) {
T obj = factory();
msgpack::sbuffer output;
msgpack::pack(output, obj);
for (auto _ : state) {
msgpack::object_handle handle =
msgpack::unpack(output.data(), output.size());
T result;
handle.get().convert(result);
benchmark::DoNotOptimize(result);
}
}
#define DEFINE_MSGPACK_BENCHMARKS(name, type, create_fn) \
static void BM_Msgpack_##name##_Serialize(benchmark::State &state) { \
run_msgpack_serialize_benchmark<type>(state, create_fn); \
} \
BENCHMARK(BM_Msgpack_##name##_Serialize); \
static void BM_Msgpack_##name##_Deserialize(benchmark::State &state) { \
run_msgpack_deserialize_benchmark<type>(state, create_fn); \
} \
BENCHMARK(BM_Msgpack_##name##_Deserialize)
DEFINE_MSGPACK_BENCHMARKS(Struct, NumericStruct, create_numeric_struct);
DEFINE_MSGPACK_BENCHMARKS(Sample, Sample, create_sample);
DEFINE_MSGPACK_BENCHMARKS(MediaContent, MediaContent, create_media_content);
DEFINE_MSGPACK_BENCHMARKS(StructList, StructList, create_struct_list);
DEFINE_MSGPACK_BENCHMARKS(SampleList, SampleList, create_sample_list);
DEFINE_MSGPACK_BENCHMARKS(MediaContentList, MediaContentList,
create_media_content_list);
#undef DEFINE_MSGPACK_BENCHMARKS
// ============================================================================
// Struct benchmarks (simple object with 8 int32 fields)
// ============================================================================
static void BM_Fory_Struct_Serialize(benchmark::State &state) {
auto fory = fory::serialization::Fory::builder()
.xlang(true)
.compatible(true)
.track_ref(false)
.build();
register_fory_types(fory);
NumericStruct obj = create_numeric_struct();
// Reuse internal buffer
fory::Buffer buffer;
buffer.reserve(64);
for (auto _ : state) {
buffer.writer_index(0);
fory.serialize_to(buffer, obj);
benchmark::DoNotOptimize(buffer.data());
}
}
BENCHMARK(BM_Fory_Struct_Serialize);
// Fair comparison: convert plain C++ struct to protobuf, then serialize
// (Same pattern as Java benchmark's buildPBStruct().toByteArray())
static void BM_Protobuf_Struct_Serialize(benchmark::State &state) {
NumericStruct obj = create_numeric_struct();
protobuf::Struct pb = to_pb_struct(obj);
std::vector<uint8_t> output;
output.resize(pb.ByteSizeLong());
for (auto _ : state) {
pb = to_pb_struct(obj);
pb.SerializeToArray(output.data(), static_cast<int>(output.size()));
benchmark::DoNotOptimize(output);
}
}
BENCHMARK(BM_Protobuf_Struct_Serialize);
static void BM_Fory_Struct_Deserialize(benchmark::State &state) {
auto fory = fory::serialization::Fory::builder()
.xlang(true)
.compatible(true)
.track_ref(false)
.build();
register_fory_types(fory);
NumericStruct obj = create_numeric_struct();
auto serialized = fory.serialize(obj);
if (!serialized.ok()) {
state.SkipWithError("Serialization failed");
return;
}
auto &bytes = serialized.value();
// Verify deserialization works first
auto test_result =
fory.deserialize<NumericStruct>(bytes.data(), bytes.size());
if (!test_result.ok()) {
state.SkipWithError("Deserialization test failed");
return;
}
for (auto _ : state) {
auto result = fory.deserialize<NumericStruct>(bytes.data(), bytes.size());
benchmark::DoNotOptimize(result);
}
}
BENCHMARK(BM_Fory_Struct_Deserialize);
// Fair comparison: deserialize and convert protobuf to plain C++ struct
// (Same pattern as Java benchmark's fromPBObject())
static void BM_Protobuf_Struct_Deserialize(benchmark::State &state) {
protobuf::Struct obj = create_proto_struct();
std::string serialized;
obj.SerializeToString(&serialized);
for (auto _ : state) {
protobuf::Struct pb_result;
pb_result.ParseFromString(serialized);
NumericStruct result = from_pb_struct(pb_result);
benchmark::DoNotOptimize(result);
}
}
BENCHMARK(BM_Protobuf_Struct_Deserialize);
// ============================================================================
// Sample benchmarks (complex object with various types and arrays)
// ============================================================================
static void BM_Fory_Sample_Serialize(benchmark::State &state) {
auto fory = fory::serialization::Fory::builder()
.xlang(true)
.compatible(true)
.track_ref(false)
.build();
register_fory_types(fory);
Sample obj = create_sample();
// Pre-allocate buffer (like Protobuf benchmark does)
fory::Buffer buffer;
buffer.reserve(4096);
for (auto _ : state) {
buffer.writer_index(0);
auto result = fory.serialize_to(buffer, obj);
benchmark::DoNotOptimize(result);
benchmark::DoNotOptimize(buffer.data());
}
}
BENCHMARK(BM_Fory_Sample_Serialize);
static void BM_Protobuf_Sample_Serialize(benchmark::State &state) {
protobuf::Sample obj = create_proto_sample();
std::vector<uint8_t> output;
output.resize(obj.ByteSizeLong());
for (auto _ : state) {
obj.SerializeToArray(output.data(), static_cast<int>(output.size()));
benchmark::DoNotOptimize(output);
}
}
BENCHMARK(BM_Protobuf_Sample_Serialize);
static void BM_Fory_Sample_Deserialize(benchmark::State &state) {
auto fory = fory::serialization::Fory::builder()
.xlang(true)
.compatible(true)
.track_ref(false)
.build();
register_fory_types(fory);
Sample obj = create_sample();
auto serialized = fory.serialize(obj);
if (!serialized.ok()) {
state.SkipWithError("Serialization failed");
return;
}
auto &bytes = serialized.value();
// Verify deserialization works first
auto test_result = fory.deserialize<Sample>(bytes.data(), bytes.size());
if (!test_result.ok()) {
state.SkipWithError("Deserialization test failed");
return;
}
for (auto _ : state) {
auto result = fory.deserialize<Sample>(bytes.data(), bytes.size());
benchmark::DoNotOptimize(result);
}
}
BENCHMARK(BM_Fory_Sample_Deserialize);
static void BM_Protobuf_Sample_Deserialize(benchmark::State &state) {
protobuf::Sample obj = create_proto_sample();
std::string serialized;
obj.SerializeToString(&serialized);
for (auto _ : state) {
protobuf::Sample result;
result.ParseFromString(serialized);
benchmark::DoNotOptimize(result);
}
}
BENCHMARK(BM_Protobuf_Sample_Deserialize);
// ============================================================================
// MediaContent benchmarks (nested objects with strings and lists)
// ============================================================================
static void BM_Fory_MediaContent_Serialize(benchmark::State &state) {
auto fory = fory::serialization::Fory::builder()
.xlang(true)
.compatible(true)
.track_ref(false)
.build();
register_fory_types(fory);
MediaContent obj = create_media_content();
// Pre-allocate buffer
fory::Buffer buffer;
buffer.reserve(4096);
for (auto _ : state) {
buffer.writer_index(0);
auto result = fory.serialize_to(buffer, obj);
benchmark::DoNotOptimize(result);
benchmark::DoNotOptimize(buffer.data());
}
}
BENCHMARK(BM_Fory_MediaContent_Serialize);
static void BM_Protobuf_MediaContent_Serialize(benchmark::State &state) {
MediaContent obj = create_media_content();
protobuf::MediaContent pb = to_pb_mediaContent(obj);
std::vector<uint8_t> output;
output.resize(pb.ByteSizeLong());
for (auto _ : state) {
pb = to_pb_mediaContent(obj);
pb.SerializeToArray(output.data(), static_cast<int>(output.size()));
benchmark::DoNotOptimize(output);
}
}
BENCHMARK(BM_Protobuf_MediaContent_Serialize);
static void BM_Fory_MediaContent_Deserialize(benchmark::State &state) {
auto fory = fory::serialization::Fory::builder()
.xlang(true)
.compatible(true)
.track_ref(false)
.build();
register_fory_types(fory);
MediaContent obj = create_media_content();
auto serialized = fory.serialize(obj);
if (!serialized.ok()) {
state.SkipWithError("Serialization failed");
return;
}
auto &bytes = serialized.value();
// Verify deserialization works first
auto test_result = fory.deserialize<MediaContent>(bytes.data(), bytes.size());
if (!test_result.ok()) {
state.SkipWithError("Deserialization test failed");
return;
}
for (auto _ : state) {
auto result = fory.deserialize<MediaContent>(bytes.data(), bytes.size());
benchmark::DoNotOptimize(result);
}
}
BENCHMARK(BM_Fory_MediaContent_Deserialize);
static void BM_Protobuf_MediaContent_Deserialize(benchmark::State &state) {
protobuf::MediaContent obj = create_proto_media_content();
std::string serialized;
obj.SerializeToString(&serialized);
for (auto _ : state) {
protobuf::MediaContent pb_result;
pb_result.ParseFromString(serialized);
MediaContent result = from_pb_mediaContent(pb_result);
benchmark::DoNotOptimize(result);
}
}
BENCHMARK(BM_Protobuf_MediaContent_Deserialize);
// ============================================================================
// List benchmarks (StructList, SampleList, MediaContentList)
// ============================================================================
static void BM_Fory_StructList_Serialize(benchmark::State &state) {
auto fory = fory::serialization::Fory::builder()
.xlang(true)
.compatible(true)
.track_ref(false)
.build();
register_fory_types(fory);
StructList obj = create_struct_list();
fory::Buffer buffer;
buffer.reserve(65536);
for (auto _ : state) {
buffer.writer_index(0);
auto result = fory.serialize_to(buffer, obj);
benchmark::DoNotOptimize(result);
benchmark::DoNotOptimize(buffer.data());
}
}
BENCHMARK(BM_Fory_StructList_Serialize);
static void BM_Protobuf_StructList_Serialize(benchmark::State &state) {
StructList obj = create_struct_list();
protobuf::StructList pb = to_pb_struct_list(obj);
std::vector<uint8_t> output;
output.resize(pb.ByteSizeLong());
for (auto _ : state) {
pb = to_pb_struct_list(obj);
pb.SerializeToArray(output.data(), static_cast<int>(output.size()));
benchmark::DoNotOptimize(output);
}
}
BENCHMARK(BM_Protobuf_StructList_Serialize);
static void BM_Fory_StructList_Deserialize(benchmark::State &state) {
auto fory = fory::serialization::Fory::builder()
.xlang(true)
.compatible(true)
.track_ref(false)
.build();
register_fory_types(fory);
StructList obj = create_struct_list();
auto serialized = fory.serialize(obj);
if (!serialized.ok()) {
state.SkipWithError("Serialization failed");
return;
}
auto &bytes = serialized.value();
auto test_result = fory.deserialize<StructList>(bytes.data(), bytes.size());
if (!test_result.ok()) {
state.SkipWithError("Deserialization test failed");
return;
}
for (auto _ : state) {
auto result = fory.deserialize<StructList>(bytes.data(), bytes.size());
benchmark::DoNotOptimize(result);
}
}
BENCHMARK(BM_Fory_StructList_Deserialize);
static void BM_Protobuf_StructList_Deserialize(benchmark::State &state) {
protobuf::StructList obj = create_proto_struct_list();
std::string serialized;
obj.SerializeToString(&serialized);
for (auto _ : state) {
protobuf::StructList pb_result;
pb_result.ParseFromString(serialized);
StructList result = from_pb_struct_list(pb_result);
benchmark::DoNotOptimize(result);
}
}
BENCHMARK(BM_Protobuf_StructList_Deserialize);
static void BM_Fory_SampleList_Serialize(benchmark::State &state) {
auto fory = fory::serialization::Fory::builder()
.xlang(true)
.compatible(true)