James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 1 | #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 | |
| 9 | namespace { |
| 10 | const int64_t kStringLength = 32; |
| 11 | const int64_t kVectorLength = 3; |
| 12 | |
| 13 | enum Enum { Apples, Pears, Bananas }; |
| 14 | |
| 15 | struct Foo { |
| 16 | int64_t id; |
| 17 | short count; |
| 18 | char prefix; |
| 19 | int length; |
| 20 | }; |
| 21 | |
| 22 | struct Bar { |
| 23 | Foo parent; |
| 24 | int time; |
| 25 | float ratio; |
| 26 | unsigned short size; |
| 27 | }; |
| 28 | |
| 29 | struct 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 | |
| 39 | struct 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 | |
| 47 | struct RawBench : Bench { |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame^] | 48 | uint8_t *Encode(void *buf, int64_t &len) override { |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 49 | 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 Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame^] | 77 | int64_t Use(void *decoded) override { |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 78 | 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 Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame^] | 101 | void *Decode(void *buf, int64_t) override { return buf; } |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 102 | void Dealloc(void *) override{}; |
| 103 | }; |
| 104 | |
| 105 | } // namespace |
| 106 | |
| 107 | std::unique_ptr<Bench> NewRawBench() { |
| 108 | return std::unique_ptr<RawBench>(new RawBench()); |
| 109 | } |