blob: c65bca7f71cceb3b0726f12b57b8504bd83eb837 [file] [log] [blame]
James Kuszmaul8e62b022022-03-22 09:33:25 -07001#include "benchmarks/cpp/raw/raw_bench.h"
2
3#include <cstdint>
4#include <cstring>
5#include <memory>
6
7#include "benchmarks/cpp/bench.h"
8
9namespace {
10const int64_t kStringLength = 32;
11const int64_t kVectorLength = 3;
12
13enum Enum { Apples, Pears, Bananas };
14
15struct Foo {
16 int64_t id;
17 short count;
18 char prefix;
19 int length;
20};
21
22struct Bar {
23 Foo parent;
24 int time;
25 float ratio;
26 unsigned short size;
27};
28
29struct FooBar {
30 Bar sibling;
31 // We have to stick this in, otherwise strlen() will make it slower than
32 // FlatBuffers:
33 int name_len;
34 char name[kStringLength];
35 double rating;
36 unsigned char postfix;
37};
38
39struct FooBarContainer {
40 FooBar list[kVectorLength]; // 3 copies of the above
41 bool initialized;
42 Enum fruit;
43 int location_len;
44 char location[kStringLength];
45};
46
47struct RawBench : Bench {
Austin Schuh2dd86a92022-09-14 21:19:23 -070048 uint8_t *Encode(void *buf, int64_t &len) override {
James Kuszmaul8e62b022022-03-22 09:33:25 -070049 FooBarContainer *fbc = new (buf) FooBarContainer;
50 strcpy(fbc->location, "http://google.com/flatbuffers/"); // Unsafe eek!
51 fbc->location_len = (int)strlen(fbc->location);
52 fbc->fruit = Bananas;
53 fbc->initialized = true;
54 for (int i = 0; i < kVectorLength; i++) {
55 // We add + i to not make these identical copies for a more realistic
56 // compression test.
57 auto &foobar = fbc->list[i];
58 foobar.rating = 3.1415432432445543543 + i;
59 foobar.postfix = '!' + i;
60 strcpy(foobar.name, "Hello, World!");
61 foobar.name_len = (int)strlen(foobar.name);
62 auto &bar = foobar.sibling;
63 bar.ratio = 3.14159f + i;
64 bar.size = 10000 + i;
65 bar.time = 123456 + i;
66 auto &foo = bar.parent;
67 foo.id = 0xABADCAFEABADCAFE + i;
68 foo.count = 10000 + i;
69 foo.length = 1000000 + i;
70 foo.prefix = '@' + i;
71 }
72
73 len = sizeof(FooBarContainer);
74 return reinterpret_cast<uint8_t *>(fbc);
75 };
76
Austin Schuh2dd86a92022-09-14 21:19:23 -070077 int64_t Use(void *decoded) override {
James Kuszmaul8e62b022022-03-22 09:33:25 -070078 auto foobarcontainer = reinterpret_cast<FooBarContainer *>(decoded);
79 sum = 0;
80 Add(foobarcontainer->initialized);
81 Add(foobarcontainer->location_len);
82 Add(foobarcontainer->fruit);
83 for (unsigned int i = 0; i < kVectorLength; i++) {
84 auto foobar = &foobarcontainer->list[i];
85 Add(foobar->name_len);
86 Add(foobar->postfix);
87 Add(static_cast<int64_t>(foobar->rating));
88 auto bar = &foobar->sibling;
89 Add(static_cast<int64_t>(bar->ratio));
90 Add(bar->size);
91 Add(bar->time);
92 auto &foo = bar->parent;
93 Add(foo.count);
94 Add(foo.id);
95 Add(foo.length);
96 Add(foo.prefix);
97 }
98 return sum;
99 }
100
Austin Schuh2dd86a92022-09-14 21:19:23 -0700101 void *Decode(void *buf, int64_t) override { return buf; }
James Kuszmaul8e62b022022-03-22 09:33:25 -0700102 void Dealloc(void *) override{};
103};
104
105} // namespace
106
107std::unique_ptr<Bench> NewRawBench() {
108 return std::unique_ptr<RawBench>(new RawBench());
109}