aboutsummaryrefslogtreecommitdiffstats
path: root/fnet/src/tests/databuffer/databuffer.cpp
blob: eccef0418db5d48e7803e66958f8aeea2baf2473 (plain) (blame)
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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include <vespa/vespalib/testkit/test_kit.h>
#include <vespa/fnet/databuffer.h>
#include <chrono>

TEST("test resetIfEmpty") {
    FNET_DataBuffer buf(64);
    EXPECT_TRUE(buf.GetData() == buf.GetDead());
    EXPECT_TRUE(buf.GetData() == buf.GetFree());
    buf.WriteInt32(11111111);
    EXPECT_TRUE(buf.GetData() == buf.GetDead());
    EXPECT_FALSE(buf.GetData() == buf.GetFree());
    buf.resetIfEmpty();
    EXPECT_TRUE(buf.GetData() == buf.GetDead());
    EXPECT_FALSE(buf.GetData() == buf.GetFree());
    EXPECT_EQUAL(11111111u, buf.ReadInt32());
    buf.resetIfEmpty();
    EXPECT_TRUE(buf.GetData() == buf.GetDead());
    EXPECT_TRUE(buf.GetData() == buf.GetFree());
}

TEST("testResize") {
    FNET_DataBuffer buf(64);
    uint32_t initialSize = buf.GetBufSize();
    buf.WriteInt32(11111111);
    buf.WriteInt32(22222222);
    buf.WriteInt32(33333333);
    buf.WriteInt32(44444444);
    buf.WriteInt32(55555555);
    EXPECT_TRUE(buf.ReadInt32() == 11111111);
    buf.EnsureFree(initialSize);
    EXPECT_TRUE(buf.GetBufSize() > initialSize);
    EXPECT_TRUE(buf.ReadInt32() == 22222222);
    EXPECT_TRUE(!buf.Shrink(buf.GetBufSize()));
    EXPECT_TRUE(!buf.Shrink(buf.GetBufSize() + 16));
    EXPECT_TRUE(!buf.Shrink(2 * 4));
    EXPECT_TRUE(buf.Shrink(3 * 4));
    EXPECT_TRUE(buf.GetBufSize() == 3 * 4);
    EXPECT_TRUE(buf.ReadInt32() == 33333333);
    buf.WriteInt32(66666666);
    buf.EnsureFree(16);
    EXPECT_TRUE(buf.GetDataLen() == 3 * 4);
    EXPECT_TRUE(buf.GetBufSize() >= 16 + 3 * 4);
    EXPECT_TRUE(buf.ReadInt32() == 44444444);
    EXPECT_TRUE(buf.ReadInt32() == 55555555);
    EXPECT_TRUE(buf.ReadInt32() == 66666666);
    EXPECT_TRUE(buf.Shrink(0));
    EXPECT_TRUE(buf.GetBufSize() == 0);
    buf.WriteInt32(42);
    EXPECT_TRUE(buf.GetBufSize() >= 4);
    EXPECT_TRUE(buf.ReadInt32() == 42);
    EXPECT_TRUE(buf.GetDataLen() == 0);
}

TEST("testSpeed") {
  using clock = std::chrono::steady_clock;
  using ms_double = std::chrono::duration<double, std::milli>;

  FNET_DataBuffer buf0(20000);
  FNET_DataBuffer buf1(20000);
  FNET_DataBuffer buf2(20000);
  clock::time_point start;
  ms_double         ms;

  int        i;
  int        k;

  // fill buf0 with random data
  for (i = 0; i < 16000; i++) {
    buf0.WriteInt8((uint8_t)rand());
  }

  // copy buf0 into buf1
  for (i = 0; i < 16000; i++) {
    buf1.WriteInt8(buf0.ReadInt8());
  }

  // undo read from buf0
  buf0.DeadToData(buf0.GetDeadLen());

  // test encode/decode speed
  start = clock::now();

  for (i = 0; i < 5000; i++) {
    buf2.Clear();
    for (k = 0; k < 500; k++) {
      buf2.WriteInt8(buf1.ReadInt8());
      buf2.WriteInt32(buf1.ReadInt32());
      buf2.WriteInt8(buf1.ReadInt8());
      buf2.WriteInt8(buf1.ReadInt8());
      buf2.WriteInt16(buf1.ReadInt16());
      buf2.WriteInt8(buf1.ReadInt8());
      buf2.WriteInt32(buf1.ReadInt32());
      buf2.WriteInt16(buf1.ReadInt16());
      buf2.WriteInt32(buf1.ReadInt32());
      buf2.WriteInt64(buf1.ReadInt64());
      buf2.WriteInt32(buf1.ReadInt32());
    }
    buf1.Clear();
    for (k = 0; k < 500; k++) {
      buf1.WriteInt8(buf2.ReadInt8());
      buf1.WriteInt16(buf2.ReadInt16());
      buf1.WriteInt8(buf2.ReadInt8());
      buf1.WriteInt32(buf2.ReadInt32());
      buf1.WriteInt32(buf2.ReadInt32());
      buf1.WriteInt8(buf2.ReadInt8());
      buf1.WriteInt64(buf2.ReadInt64());
      buf1.WriteInt32(buf2.ReadInt32());
      buf1.WriteInt8(buf2.ReadInt8());
      buf1.WriteInt16(buf2.ReadInt16());
      buf1.WriteInt32(buf2.ReadInt32());
    }
  }
  buf2.DeadToData(buf2.GetDeadLen());

  ms = (clock::now() - start);
  fprintf(stderr, "encode/decode time (~160MB): %1.2f\n", ms.count());

  EXPECT_TRUE(buf0.Equals(&buf1) && buf0.Equals(&buf2));

  // test encode[fast]/decode speed
  start = clock::now();

  for (i = 0; i < 5000; i++) {
    buf2.Clear();
    for (k = 0; k < 500; k++) {
      buf2.WriteInt8Fast(buf1.ReadInt8());
      buf2.WriteInt32Fast(buf1.ReadInt32());
      buf2.WriteInt8Fast(buf1.ReadInt8());
      buf2.WriteInt8Fast(buf1.ReadInt8());
      buf2.WriteInt16Fast(buf1.ReadInt16());
      buf2.WriteInt8Fast(buf1.ReadInt8());
      buf2.WriteInt32Fast(buf1.ReadInt32());
      buf2.WriteInt16Fast(buf1.ReadInt16());
      buf2.WriteInt32Fast(buf1.ReadInt32());
      buf2.WriteInt64Fast(buf1.ReadInt64());
      buf2.WriteInt32Fast(buf1.ReadInt32());
    }
    buf1.Clear();
    for (k = 0; k < 500; k++) {
      buf1.WriteInt8Fast(buf2.ReadInt8());
      buf1.WriteInt16Fast(buf2.ReadInt16());
      buf1.WriteInt8Fast(buf2.ReadInt8());
      buf1.WriteInt32Fast(buf2.ReadInt32());
      buf1.WriteInt32Fast(buf2.ReadInt32());
      buf1.WriteInt8Fast(buf2.ReadInt8());
      buf1.WriteInt64Fast(buf2.ReadInt64());
      buf1.WriteInt32Fast(buf2.ReadInt32());
      buf1.WriteInt8Fast(buf2.ReadInt8());
      buf1.WriteInt16Fast(buf2.ReadInt16());
      buf1.WriteInt32Fast(buf2.ReadInt32());
    }
  }
  buf2.DeadToData(buf2.GetDeadLen());

  ms = (clock::now() - start);
  fprintf(stderr, "encode[fast]/decode time (~160MB): %1.2f\n", ms.count());

  EXPECT_TRUE(buf0.Equals(&buf1) && buf0.Equals(&buf2));

  // init source table for table streaming test
  uint32_t table[4000];
  for (i = 0; i < 4000; i++) {
    table[i] = i;
  }

  // test byte-swap table encoding speed
  start = clock::now();

  for (i = 0; i < 10000; i++) {
    buf1.Clear();
    for (k = 0; k < 4000; k += 8) {
      buf1.WriteInt32Fast(table[k]);
      buf1.WriteInt32Fast(table[k + 1]);
      buf1.WriteInt32Fast(table[k + 2]);
      buf1.WriteInt32Fast(table[k + 3]);
      buf1.WriteInt32Fast(table[k + 4]);
      buf1.WriteInt32Fast(table[k + 5]);
      buf1.WriteInt32Fast(table[k + 6]);
      buf1.WriteInt32Fast(table[k + 7]);
    }
  }
  ms = (clock::now() - start);
  fprintf(stderr, "byte-swap array encoding[fast] (~160 MB): %1.2f ms\n",
          ms.count());

  // test direct-copy table encoding speed
  start = clock::now();

  for (i = 0; i < 10000; i++) {
    buf2.Clear();
    buf2.EnsureFree(16000);
    memcpy(buf2.GetFree(), table, 16000);
    buf2.FreeToData(16000);
  }
  ms = (clock::now() - start);
  fprintf(stderr, "direct-copy array encoding (~160 MB): %1.2f ms\n",
          ms.count());
}

TEST_MAIN() { TEST_RUN_ALL(); }