File: raw_bench.cpp

package info (click to toggle)
flatbuffers 2.0.8%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 14,308 kB
  • sloc: cpp: 44,808; python: 6,544; cs: 4,852; java: 4,389; ansic: 1,615; php: 1,455; xml: 973; javascript: 938; sh: 806; makefile: 35
file content (109 lines) | stat: -rw-r--r-- 2,770 bytes parent folder | download | duplicates (12)
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
#include "benchmarks/cpp/raw/raw_bench.h"

#include <cstdint>
#include <cstring>
#include <memory>

#include "benchmarks/cpp/bench.h"

namespace {
const int64_t kStringLength = 32;
const int64_t kVectorLength = 3;

enum Enum { Apples, Pears, Bananas };

struct Foo {
  int64_t id;
  short count;
  char prefix;
  int length;
};

struct Bar {
  Foo parent;
  int time;
  float ratio;
  unsigned short size;
};

struct FooBar {
  Bar sibling;
  // We have to stick this in, otherwise strlen() will make it slower than
  // FlatBuffers:
  int name_len;
  char name[kStringLength];
  double rating;
  unsigned char postfix;
};

struct FooBarContainer {
  FooBar list[kVectorLength];  // 3 copies of the above
  bool initialized;
  Enum fruit;
  int location_len;
  char location[kStringLength];
};

struct RawBench : Bench {
  uint8_t *Encode(void *buf, int64_t &len) override {
    FooBarContainer *fbc = new (buf) FooBarContainer;
    strcpy(fbc->location, "http://google.com/flatbuffers/");  // Unsafe eek!
    fbc->location_len = (int)strlen(fbc->location);
    fbc->fruit = Bananas;
    fbc->initialized = true;
    for (int i = 0; i < kVectorLength; i++) {
      // We add + i to not make these identical copies for a more realistic
      // compression test.
      auto &foobar = fbc->list[i];
      foobar.rating = 3.1415432432445543543 + i;
      foobar.postfix = '!' + i;
      strcpy(foobar.name, "Hello, World!");
      foobar.name_len = (int)strlen(foobar.name);
      auto &bar = foobar.sibling;
      bar.ratio = 3.14159f + i;
      bar.size = 10000 + i;
      bar.time = 123456 + i;
      auto &foo = bar.parent;
      foo.id = 0xABADCAFEABADCAFE + i;
      foo.count = 10000 + i;
      foo.length = 1000000 + i;
      foo.prefix = '@' + i;
    }

    len = sizeof(FooBarContainer);
    return reinterpret_cast<uint8_t *>(fbc);
  };

  int64_t Use(void *decoded) override {
    auto foobarcontainer = reinterpret_cast<FooBarContainer *>(decoded);
    sum = 0;
    Add(foobarcontainer->initialized);
    Add(foobarcontainer->location_len);
    Add(foobarcontainer->fruit);
    for (unsigned int i = 0; i < kVectorLength; i++) {
      auto foobar = &foobarcontainer->list[i];
      Add(foobar->name_len);
      Add(foobar->postfix);
      Add(static_cast<int64_t>(foobar->rating));
      auto bar = &foobar->sibling;
      Add(static_cast<int64_t>(bar->ratio));
      Add(bar->size);
      Add(bar->time);
      auto &foo = bar->parent;
      Add(foo.count);
      Add(foo.id);
      Add(foo.length);
      Add(foo.prefix);
    }
    return sum;
  }

  void *Decode(void *buf, int64_t) override { return buf; }
  void Dealloc(void *) override{};
};

}  // namespace

std::unique_ptr<Bench> NewRawBench() {
  return std::unique_ptr<RawBench>(new RawBench());
}