2 * Copyright 2014 Facebook, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include <folly/io/IOBuf.h>
19 #include <gflags/gflags.h>
20 #include <boost/random.hpp>
21 #include <gtest/gtest.h>
22 #include <folly/Benchmark.h>
23 #include <folly/Format.h>
24 #include <folly/Range.h>
25 #include <folly/io/Cursor.h>
26 #include <folly/io/Cursor-defs.h>
28 DECLARE_bool(benchmark);
30 using folly::ByteRange;
33 using folly::StringPiece;
34 using std::unique_ptr;
35 using namespace folly::io;
37 TEST(IOBuf, RWCursor) {
38 unique_ptr<IOBuf> iobuf1(IOBuf::create(20));
40 unique_ptr<IOBuf> iobuf2(IOBuf::create(20));
43 IOBuf* iob2ptr = iobuf2.get();
44 iobuf1->prependChain(std::move(iobuf2));
46 EXPECT_TRUE(iobuf1->isChained());
48 RWPrivateCursor wcursor(iobuf1.get());
49 Cursor rcursor(iobuf1.get());
50 wcursor.writeLE((uint64_t)1);
51 wcursor.writeLE((uint64_t)1);
52 wcursor.writeLE((uint64_t)1);
53 wcursor.write((uint8_t)1);
55 EXPECT_EQ(1, rcursor.readLE<uint64_t>());
57 EXPECT_EQ(1, rcursor.readLE<uint32_t>());
59 EXPECT_EQ(0, rcursor.read<uint8_t>());
60 EXPECT_EQ(0, rcursor.read<uint8_t>());
61 EXPECT_EQ(0, rcursor.read<uint8_t>());
62 EXPECT_EQ(0, rcursor.read<uint8_t>());
63 EXPECT_EQ(1, rcursor.read<uint8_t>());
67 unique_ptr<IOBuf> iobuf1(IOBuf::create(20));
69 RWPrivateCursor wcursor(iobuf1.get());
70 wcursor.write((uint8_t)1);
71 wcursor.write((uint8_t)2);
72 Cursor cursor(iobuf1.get());
74 EXPECT_EQ(2, cursor.read<uint8_t>());
78 unique_ptr<IOBuf> iobuf1(IOBuf::create(20));
80 RWPrivateCursor wcursor(iobuf1.get());
81 wcursor.write((uint8_t)1);
82 wcursor.write((uint8_t)2);
83 wcursor.reset(iobuf1.get());
84 EXPECT_EQ(1, wcursor.read<uint8_t>());
87 TEST(IOBuf, copy_assign_convert) {
88 unique_ptr<IOBuf> iobuf1(IOBuf::create(20));
90 RWPrivateCursor wcursor(iobuf1.get());
91 RWPrivateCursor cursor2(wcursor);
92 RWPrivateCursor cursor3(iobuf1.get());
94 wcursor.write((uint8_t)1);
96 wcursor.write((uint8_t)2);
97 Cursor cursor4(wcursor);
98 RWPrivateCursor cursor5(wcursor);
99 wcursor.write((uint8_t)3);
101 EXPECT_EQ(1, cursor2.read<uint8_t>());
102 EXPECT_EQ(2, cursor3.read<uint8_t>());
103 EXPECT_EQ(3, cursor4.read<uint8_t>());
106 TEST(IOBuf, arithmetic) {
107 IOBuf iobuf1(IOBuf::CREATE, 20);
109 RWPrivateCursor wcursor(&iobuf1);
111 wcursor.write((uint8_t)1);
112 Cursor cursor(&iobuf1);
114 EXPECT_EQ(1, cursor.read<uint8_t>());
116 Cursor start(&iobuf1);
117 Cursor cursor2 = start + 9;
118 EXPECT_EQ(7, cursor2 - cursor);
119 EXPECT_NE(cursor, cursor2);
121 cursor2 = cursor2 + 1;
122 EXPECT_EQ(cursor, cursor2);
125 TEST(IOBuf, endian) {
126 unique_ptr<IOBuf> iobuf1(IOBuf::create(20));
128 RWPrivateCursor wcursor(iobuf1.get());
129 Cursor rcursor(iobuf1.get());
134 // Try a couple combinations to ensure they were generated correctly
139 EXPECT_EQ(v, rcursor.readBE<uint16_t>());
142 TEST(IOBuf, Cursor) {
143 unique_ptr<IOBuf> iobuf1(IOBuf::create(1));
145 RWPrivateCursor c(iobuf1.get());
146 c.write((uint8_t)40); // OK
148 c.write((uint8_t)10); // Bad write, checked should except.
149 EXPECT_EQ(true, false);
154 TEST(IOBuf, UnshareCursor) {
156 unique_ptr<IOBuf> iobuf1(IOBuf::wrapBuffer(&buf, 1));
157 unique_ptr<IOBuf> iobuf2(IOBuf::wrapBuffer(&buf, 1));
158 RWUnshareCursor c1(iobuf1.get());
159 RWUnshareCursor c2(iobuf2.get());
161 c1.write((uint8_t)10); // This should duplicate the two buffers.
162 uint8_t t = c2.read<uint8_t>();
165 iobuf1 = IOBuf::wrapBuffer(&buf, 1);
166 iobuf2 = IOBuf::wrapBuffer(&buf, 1);
167 RWPrivateCursor c3(iobuf1.get());
168 RWPrivateCursor c4(iobuf2.get());
170 c3.write((uint8_t)10); // This should _not_ duplicate the two buffers.
171 t = c4.read<uint8_t>();
176 void append(std::unique_ptr<IOBuf>& buf, folly::StringPiece data) {
177 EXPECT_LE(data.size(), buf->tailroom());
178 memcpy(buf->writableData(), data.data(), data.size());
179 buf->append(data.size());
182 void append(Appender& appender, StringPiece data) {
183 appender.push(ByteRange(data));
186 std::string toString(const IOBuf& buf) {
189 std::pair<const uint8_t*, size_t> p;
190 while ((p = cursor.peek()).second) {
191 str.append(reinterpret_cast<const char*>(p.first), p.second);
192 cursor.skip(p.second);
199 TEST(IOBuf, PullAndPeek) {
200 std::unique_ptr<IOBuf> iobuf1(IOBuf::create(10));
201 append(iobuf1, "he");
202 std::unique_ptr<IOBuf> iobuf2(IOBuf::create(10));
203 append(iobuf2, "llo ");
204 std::unique_ptr<IOBuf> iobuf3(IOBuf::create(10));
205 append(iobuf3, "world");
206 iobuf1->prependChain(std::move(iobuf2));
207 iobuf1->prependChain(std::move(iobuf3));
208 EXPECT_EQ(3, iobuf1->countChainElements());
209 EXPECT_EQ(11, iobuf1->computeChainDataLength());
212 memset(buf, 0, sizeof(buf));
213 Cursor(iobuf1.get()).pull(buf, 11);
214 EXPECT_EQ("hello world", std::string(buf));
216 memset(buf, 0, sizeof(buf));
217 EXPECT_EQ(11, Cursor(iobuf1.get()).pullAtMost(buf, 20));
218 EXPECT_EQ("hello world", std::string(buf));
220 EXPECT_THROW({Cursor(iobuf1.get()).pull(buf, 20);},
224 RWPrivateCursor cursor(iobuf1.get());
225 auto p = cursor.peek();
226 EXPECT_EQ("he", std::string(reinterpret_cast<const char*>(p.first),
228 cursor.skip(p.second);
230 EXPECT_EQ("llo ", std::string(reinterpret_cast<const char*>(p.first),
232 cursor.skip(p.second);
234 EXPECT_EQ("world", std::string(reinterpret_cast<const char*>(p.first),
236 cursor.skip(p.second);
237 EXPECT_EQ(3, iobuf1->countChainElements());
238 EXPECT_EQ(11, iobuf1->computeChainDataLength());
242 RWPrivateCursor cursor(iobuf1.get());
244 auto p = cursor.peek();
245 EXPECT_EQ("hello world", std::string(reinterpret_cast<const
246 char*>(p.first), p.second));
247 EXPECT_EQ(1, iobuf1->countChainElements());
248 EXPECT_EQ(11, iobuf1->computeChainDataLength());
252 TEST(IOBuf, pushCursorData) {
253 unique_ptr<IOBuf> iobuf1(IOBuf::create(20));
255 iobuf1->trimStart(5);
256 unique_ptr<IOBuf> iobuf2(IOBuf::create(10));
257 unique_ptr<IOBuf> iobuf3(IOBuf::create(10));
260 iobuf1->prependChain(std::move(iobuf2));
261 iobuf1->prependChain(std::move(iobuf3));
262 EXPECT_TRUE(iobuf1->isChained());
264 //write 20 bytes to the buffer chain
265 RWPrivateCursor wcursor(iobuf1.get());
266 wcursor.writeBE<uint64_t>(1);
267 wcursor.writeBE<uint64_t>(10);
268 wcursor.writeBE<uint32_t>(20);
270 // create a read buffer for the buffer chain
271 Cursor rcursor(iobuf1.get());
272 EXPECT_EQ(1, rcursor.readBE<uint64_t>());
273 EXPECT_EQ(10, rcursor.readBE<uint64_t>());
274 EXPECT_EQ(20, rcursor.readBE<uint32_t>());
275 EXPECT_EQ(0, rcursor.totalLength());
276 rcursor.reset(iobuf1.get());
277 EXPECT_EQ(20, rcursor.totalLength());
279 // create another write buffer
280 unique_ptr<IOBuf> iobuf4(IOBuf::create(30));
282 RWPrivateCursor wcursor2(iobuf4.get());
283 // write buffer chain data into it, now wcursor2 should only
284 // have 10 bytes writable space
285 wcursor2.push(rcursor, 20);
286 EXPECT_EQ(wcursor2.totalLength(), 10);
287 // write again with not enough space in rcursor
288 EXPECT_THROW(wcursor2.push(rcursor, 20), std::out_of_range);
290 // create a read cursor to check iobuf3 data back
291 Cursor rcursor2(iobuf4.get());
292 EXPECT_EQ(1, rcursor2.readBE<uint64_t>());
293 EXPECT_EQ(10, rcursor2.readBE<uint64_t>());
294 EXPECT_EQ(20, rcursor2.readBE<uint32_t>());
298 TEST(IOBuf, Gather) {
299 std::unique_ptr<IOBuf> iobuf1(IOBuf::create(10));
300 append(iobuf1, "he");
301 std::unique_ptr<IOBuf> iobuf2(IOBuf::create(10));
302 append(iobuf2, "llo ");
303 std::unique_ptr<IOBuf> iobuf3(IOBuf::create(10));
304 append(iobuf3, "world");
305 iobuf1->prependChain(std::move(iobuf2));
306 iobuf1->prependChain(std::move(iobuf3));
307 EXPECT_EQ(3, iobuf1->countChainElements());
308 EXPECT_EQ(11, iobuf1->computeChainDataLength());
310 // Attempting to gather() more data than available in the chain should fail.
311 // Try from the very beginning of the chain.
312 RWPrivateCursor cursor(iobuf1.get());
313 EXPECT_THROW(cursor.gather(15), std::overflow_error);
314 // Now try from the middle of the chain
316 EXPECT_THROW(cursor.gather(10), std::overflow_error);
318 // Calling gatherAtMost() should succeed, however, and just gather
320 cursor.gatherAtMost(10);
321 EXPECT_EQ(8, cursor.length());
322 EXPECT_EQ(8, cursor.totalLength());
323 EXPECT_EQ("lo world",
324 folly::StringPiece(reinterpret_cast<const char*>(cursor.data()),
326 EXPECT_EQ(2, iobuf1->countChainElements());
327 EXPECT_EQ(11, iobuf1->computeChainDataLength());
329 // Now try gather again on the chain head
330 cursor = RWPrivateCursor(iobuf1.get());
332 // Since gather() doesn't split buffers, everything should be collapsed into
333 // a single buffer now.
334 EXPECT_EQ(1, iobuf1->countChainElements());
335 EXPECT_EQ(11, iobuf1->computeChainDataLength());
336 EXPECT_EQ(11, cursor.length());
337 EXPECT_EQ(11, cursor.totalLength());
340 TEST(IOBuf, cloneAndInsert) {
341 std::unique_ptr<IOBuf> iobuf1(IOBuf::create(10));
342 append(iobuf1, "he");
343 std::unique_ptr<IOBuf> iobuf2(IOBuf::create(10));
344 append(iobuf2, "llo ");
345 std::unique_ptr<IOBuf> iobuf3(IOBuf::create(10));
346 append(iobuf3, "world");
347 iobuf1->prependChain(std::move(iobuf2));
348 iobuf1->prependChain(std::move(iobuf3));
349 EXPECT_EQ(3, iobuf1->countChainElements());
350 EXPECT_EQ(11, iobuf1->computeChainDataLength());
352 std::unique_ptr<IOBuf> cloned;
354 Cursor(iobuf1.get()).clone(cloned, 3);
355 EXPECT_EQ(2, cloned->countChainElements());
356 EXPECT_EQ(3, cloned->computeChainDataLength());
359 EXPECT_EQ(11, Cursor(iobuf1.get()).cloneAtMost(cloned, 20));
360 EXPECT_EQ(3, cloned->countChainElements());
361 EXPECT_EQ(11, cloned->computeChainDataLength());
364 EXPECT_THROW({Cursor(iobuf1.get()).clone(cloned, 20);},
368 // Check that inserting in the middle of an iobuf splits
369 RWPrivateCursor cursor(iobuf1.get());
370 Cursor(iobuf1.get()).clone(cloned, 3);
371 EXPECT_EQ(2, cloned->countChainElements());
372 EXPECT_EQ(3, cloned->computeChainDataLength());
376 cursor.insert(std::move(cloned));
377 cursor.insert(folly::IOBuf::create(0));
378 EXPECT_EQ(7, iobuf1->countChainElements());
379 EXPECT_EQ(14, iobuf1->computeChainDataLength());
380 // Check that nextBuf got set correctly to the buffer with 1 byte left
381 EXPECT_EQ(1, cursor.peek().second);
382 cursor.read<uint8_t>();
386 // Check that inserting at the end doesn't create empty buf
387 RWPrivateCursor cursor(iobuf1.get());
388 Cursor(iobuf1.get()).clone(cloned, 1);
389 EXPECT_EQ(1, cloned->countChainElements());
390 EXPECT_EQ(1, cloned->computeChainDataLength());
394 cursor.insert(std::move(cloned));
395 EXPECT_EQ(8, iobuf1->countChainElements());
396 EXPECT_EQ(15, iobuf1->computeChainDataLength());
397 // Check that nextBuf got set correctly
398 cursor.read<uint8_t>();
401 // Check that inserting at the beginning doesn't create empty buf
402 RWPrivateCursor cursor(iobuf1.get());
403 Cursor(iobuf1.get()).clone(cloned, 1);
404 EXPECT_EQ(1, cloned->countChainElements());
405 EXPECT_EQ(1, cloned->computeChainDataLength());
407 cursor.insert(std::move(cloned));
408 EXPECT_EQ(9, iobuf1->countChainElements());
409 EXPECT_EQ(16, iobuf1->computeChainDataLength());
410 // Check that nextBuf got set correctly
411 cursor.read<uint8_t>();
415 TEST(IOBuf, Appender) {
416 std::unique_ptr<IOBuf> head(IOBuf::create(10));
417 append(head, "hello");
419 Appender app(head.get(), 10);
420 uint32_t cap = head->capacity();
421 uint32_t len1 = app.length();
422 EXPECT_EQ(cap - 5, len1);
423 app.ensure(len1); // won't grow
424 EXPECT_EQ(len1, app.length());
425 app.ensure(len1 + 1); // will grow
426 EXPECT_LE(len1 + 1, app.length());
428 append(app, " world");
429 EXPECT_EQ("hello world", toString(*head));
432 TEST(IOBuf, Printf) {
433 IOBuf head(IOBuf::CREATE, 24);
434 Appender app(&head, 32);
436 app.printf("%s", "test");
437 EXPECT_EQ(head.length(), 4);
438 EXPECT_EQ(0, memcmp(head.data(), "test\0", 5));
440 app.printf("%d%s %s%s %#x", 32, "this string is",
441 "longer than our original allocation size,",
442 "and will therefore require a new allocation", 0x12345678);
443 // The tailroom should start with a nul byte now.
444 EXPECT_GE(head.prev()->tailroom(), 1);
445 EXPECT_EQ(0, *head.prev()->tail());
447 EXPECT_EQ("test32this string is longer than our original "
448 "allocation size,and will therefore require a "
449 "new allocation 0x12345678",
450 head.moveToFbString().toStdString());
453 TEST(IOBuf, Format) {
454 IOBuf head(IOBuf::CREATE, 24);
455 Appender app(&head, 32);
457 format("{}", "test")(app);
458 EXPECT_EQ(head.length(), 4);
459 EXPECT_EQ(0, memcmp(head.data(), "test", 4));
461 auto fmt = format("{}{} {}{} {:#x}",
462 32, "this string is",
463 "longer than our original allocation size,",
464 "and will therefore require a new allocation",
467 EXPECT_EQ("test32this string is longer than our original "
468 "allocation size,and will therefore require a "
469 "new allocation 0x12345678",
470 head.moveToFbString().toStdString());
473 TEST(IOBuf, QueueAppender) {
474 folly::IOBufQueue queue;
476 // Allocate 100 bytes at once, but don't grow past 1024
477 QueueAppender app(&queue, 100);
478 size_t n = 1024 / sizeof(uint32_t);
479 for (uint32_t i = 0; i < n; ++i) {
483 // There must be a goodMallocSize between 100 and 1024...
484 EXPECT_LT(1, queue.front()->countChainElements());
485 const IOBuf* buf = queue.front();
487 EXPECT_LE(100, buf->capacity());
489 } while (buf != queue.front());
491 Cursor cursor(queue.front());
492 for (uint32_t i = 0; i < n; ++i) {
493 EXPECT_EQ(i, cursor.readBE<uint32_t>());
496 EXPECT_THROW({cursor.readBE<uint32_t>();}, std::out_of_range);
499 TEST(IOBuf, CursorOperators) {
500 // Test operators on a single-item chain
502 std::unique_ptr<IOBuf> chain1(IOBuf::create(20));
505 Cursor curs1(chain1.get());
506 EXPECT_EQ(0, curs1 - chain1.get());
508 EXPECT_EQ(3, curs1 - chain1.get());
510 EXPECT_EQ(10, curs1 - chain1.get());
512 Cursor curs2(chain1.get());
513 EXPECT_EQ(0, curs2 - chain1.get());
514 EXPECT_EQ(10, curs1 - curs2);
515 EXPECT_THROW(curs2 - curs1, std::out_of_range);
518 // Test cross-chain operations
520 std::unique_ptr<IOBuf> chain1(IOBuf::create(20));
522 std::unique_ptr<IOBuf> chain2 = chain1->clone();
524 Cursor curs1(chain1.get());
525 Cursor curs2(chain2.get());
526 EXPECT_THROW(curs1 - curs2, std::out_of_range);
527 EXPECT_THROW(curs1 - chain2.get(), std::out_of_range);
530 // Test operations on multi-item chains
532 std::unique_ptr<IOBuf> chain(IOBuf::create(20));
534 chain->appendChain(chain->clone());
535 EXPECT_EQ(20, chain->computeChainDataLength());
537 Cursor curs1(chain.get());
539 Cursor curs2(chain.get());
541 EXPECT_EQ(2, curs1 - curs2);
542 EXPECT_EQ(5, curs1 - chain.get());
543 EXPECT_THROW(curs2 - curs1, std::out_of_range);
546 EXPECT_EQ(9, curs1 - curs2);
547 EXPECT_EQ(12, curs1 - chain.get());
548 EXPECT_THROW(curs2 - curs1, std::out_of_range);
551 EXPECT_EQ(2, curs1 - curs2);
552 EXPECT_THROW(curs2 - curs1, std::out_of_range);
556 TEST(IOBuf, StringOperations) {
557 // Test a single buffer with two null-terminated strings and an extra uint8_t
560 std::unique_ptr<IOBuf> chain(IOBuf::create(16));
561 Appender app(chain.get(), 0);
562 app.push(reinterpret_cast<const uint8_t*>("hello\0world\0\x01"), 13);
564 Cursor curs(chain.get());
565 EXPECT_STREQ("hello", curs.readTerminatedString().c_str());
566 EXPECT_STREQ("world", curs.readTerminatedString().c_str());
567 EXPECT_EQ(1, curs.read<uint8_t>());
570 // Test multiple buffers where the first is empty and the string starts in
571 // the second buffer.
573 std::unique_ptr<IOBuf> chain(IOBuf::create(8));
574 chain->prependChain(IOBuf::create(12));
575 Appender app(chain.get(), 0);
576 app.push(reinterpret_cast<const uint8_t*>("hello world\0"), 12);
578 Cursor curs(chain.get());
579 EXPECT_STREQ("hello world", curs.readTerminatedString().c_str());
582 // Test multiple buffers with a single null-terminated string spanning them
584 std::unique_ptr<IOBuf> chain(IOBuf::create(8));
585 chain->prependChain(IOBuf::create(8));
587 chain->next()->append(4);
588 RWPrivateCursor rwc(chain.get());
589 rwc.push(reinterpret_cast<const uint8_t*>("hello world\0"), 12);
591 Cursor curs(chain.get());
592 EXPECT_STREQ("hello world", curs.readTerminatedString().c_str());
595 // Test a reading a null-terminated string that's longer than the maximum
598 std::unique_ptr<IOBuf> chain(IOBuf::create(16));
599 Appender app(chain.get(), 0);
600 app.push(reinterpret_cast<const uint8_t*>("hello world\0"), 12);
602 Cursor curs(chain.get());
603 EXPECT_THROW(curs.readTerminatedString('\0', 5), std::length_error);
606 // Test reading a null-terminated string from a chain with an empty buffer at
609 std::unique_ptr<IOBuf> buf(IOBuf::create(8));
610 Appender app(buf.get(), 0);
611 app.push(reinterpret_cast<const uint8_t*>("hello\0"), 6);
612 std::unique_ptr<IOBuf> chain(IOBuf::create(8));
613 chain->prependChain(std::move(buf));
615 Cursor curs(chain.get());
616 EXPECT_STREQ("hello", curs.readTerminatedString().c_str());
619 // Test reading a two fixed-length strings from a single buffer with an extra
620 // uint8_t at the end
622 std::unique_ptr<IOBuf> chain(IOBuf::create(16));
623 Appender app(chain.get(), 0);
624 app.push(reinterpret_cast<const uint8_t*>("helloworld\x01"), 11);
626 Cursor curs(chain.get());
627 EXPECT_STREQ("hello", curs.readFixedString(5).c_str());
628 EXPECT_STREQ("world", curs.readFixedString(5).c_str());
629 EXPECT_EQ(1, curs.read<uint8_t>());
632 // Test multiple buffers where the first is empty and a fixed-length string
633 // starts in the second buffer.
635 std::unique_ptr<IOBuf> chain(IOBuf::create(8));
636 chain->prependChain(IOBuf::create(16));
637 Appender app(chain.get(), 0);
638 app.push(reinterpret_cast<const uint8_t*>("hello world"), 11);
640 Cursor curs(chain.get());
641 EXPECT_STREQ("hello world", curs.readFixedString(11).c_str());
644 // Test multiple buffers with a single fixed-length string spanning them
646 std::unique_ptr<IOBuf> chain(IOBuf::create(8));
647 chain->prependChain(IOBuf::create(8));
649 chain->next()->append(4);
650 RWPrivateCursor rwc(chain.get());
651 rwc.push(reinterpret_cast<const uint8_t*>("hello world"), 11);
653 Cursor curs(chain.get());
654 EXPECT_STREQ("hello world", curs.readFixedString(11).c_str());
657 // Test reading a fixed-length string from a chain with an empty buffer at
660 std::unique_ptr<IOBuf> buf(IOBuf::create(8));
661 Appender app(buf.get(), 0);
662 app.push(reinterpret_cast<const uint8_t*>("hello"), 5);
663 std::unique_ptr<IOBuf> chain(IOBuf::create(8));
664 chain->prependChain(std::move(buf));
666 Cursor curs(chain.get());
667 EXPECT_STREQ("hello", curs.readFixedString(5).c_str());
671 int benchmark_size = 1000;
672 unique_ptr<IOBuf> iobuf_benchmark;
674 unique_ptr<IOBuf> iobuf_read_benchmark;
676 template <class CursClass>
677 void runBenchmark() {
678 CursClass c(iobuf_benchmark.get());
680 for(int i = 0; i < benchmark_size; i++) {
685 BENCHMARK(rwPrivateCursorBenchmark, iters) {
687 runBenchmark<RWPrivateCursor>();
691 BENCHMARK(rwUnshareCursorBenchmark, iters) {
693 runBenchmark<RWUnshareCursor>();
698 BENCHMARK(cursorBenchmark, iters) {
700 Cursor c(iobuf_read_benchmark.get());
701 for(int i = 0; i < benchmark_size ; i++) {
707 BENCHMARK(skipBenchmark, iters) {
710 Cursor c(iobuf_read_benchmark.get());
711 for(int i = 0; i < benchmark_size ; i++) {
719 // _bin/folly/experimental/io/test/iobuf_cursor_test -benchmark
721 // Benchmark Iters Total t t/iter iter/sec
722 // ---------------------------------------------------------------------------
723 // rwPrivateCursorBenchmark 100000 142.9 ms 1.429 us 683.5 k
724 // rwUnshareCursorBenchmark 100000 309.3 ms 3.093 us 315.7 k
725 // cursorBenchmark 100000 741.4 ms 7.414 us 131.7 k
726 // skipBenchmark 100000 738.9 ms 7.389 us 132.2 k
730 // Linux dev2159.snc6.facebook.com 2.6.33-7_fbk15_104e4d0 #1 SMP
731 // Tue Oct 19 22:40:30 PDT 2010 x86_64 x86_64 x86_64 GNU/Linux
733 // 72GB RAM, 2 CPUs (Intel(R) Xeon(R) CPU L5630 @ 2.13GHz)
734 // hyperthreading disabled
736 int main(int argc, char** argv) {
737 testing::InitGoogleTest(&argc, argv);
738 gflags::ParseCommandLineFlags(&argc, &argv, true);
740 auto ret = RUN_ALL_TESTS();
742 if (ret == 0 && FLAGS_benchmark) {
743 iobuf_benchmark = IOBuf::create(benchmark_size);
744 iobuf_benchmark->append(benchmark_size);
746 iobuf_read_benchmark = IOBuf::create(1);
747 for (int i = 0; i < benchmark_size; i++) {
748 unique_ptr<IOBuf> iobuf2(IOBuf::create(1));
750 iobuf_read_benchmark->prependChain(std::move(iobuf2));
753 folly::runBenchmarks();