/*
- * Copyright 2015 Facebook, Inc.
+ * Copyright 2016 Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
#include <folly/io/IOBuf.h>
-#include <gflags/gflags.h>
-#include <boost/random.hpp>
-#include <gtest/gtest.h>
-#include <folly/Benchmark.h>
#include <folly/Format.h>
#include <folly/Range.h>
#include <folly/io/Cursor.h>
-#include <folly/io/Cursor-defs.h>
-DECLARE_bool(benchmark);
+#include <gtest/gtest.h>
using folly::ByteRange;
using folly::format;
std::string toString(const IOBuf& buf) {
std::string str;
Cursor cursor(&buf);
- std::pair<const uint8_t*, size_t> p;
- while ((p = cursor.peek()).second) {
- str.append(reinterpret_cast<const char*>(p.first), p.second);
- cursor.skip(p.second);
+ ByteRange b;
+ while (!(b = cursor.peekBytes()).empty()) {
+ str.append(reinterpret_cast<const char*>(b.data()), b.size());
+ cursor.skip(b.size());
}
return str;
}
{
RWPrivateCursor cursor(iobuf1.get());
- auto p = cursor.peek();
- EXPECT_EQ("he", std::string(reinterpret_cast<const char*>(p.first),
- p.second));
- cursor.skip(p.second);
- p = cursor.peek();
- EXPECT_EQ("llo ", std::string(reinterpret_cast<const char*>(p.first),
- p.second));
- cursor.skip(p.second);
- p = cursor.peek();
- EXPECT_EQ("world", std::string(reinterpret_cast<const char*>(p.first),
- p.second));
- cursor.skip(p.second);
+ auto b = cursor.peekBytes();
+ EXPECT_EQ("he", StringPiece(b));
+ cursor.skip(b.size());
+ b = cursor.peekBytes();
+ EXPECT_EQ("llo ", StringPiece(b));
+ cursor.skip(b.size());
+ b = cursor.peekBytes();
+ EXPECT_EQ("world", StringPiece(b));
+ cursor.skip(b.size());
EXPECT_EQ(3, iobuf1->countChainElements());
EXPECT_EQ(11, iobuf1->computeChainDataLength());
}
{
RWPrivateCursor cursor(iobuf1.get());
cursor.gather(11);
- auto p = cursor.peek();
- EXPECT_EQ("hello world", std::string(reinterpret_cast<const
- char*>(p.first), p.second));
+ auto b = cursor.peekBytes();
+ EXPECT_EQ("hello world", StringPiece(b));
EXPECT_EQ(1, iobuf1->countChainElements());
EXPECT_EQ(11, iobuf1->computeChainDataLength());
}
EXPECT_EQ(7, iobuf1->countChainElements());
EXPECT_EQ(14, iobuf1->computeChainDataLength());
// Check that nextBuf got set correctly to the buffer with 1 byte left
- EXPECT_EQ(1, cursor.peek().second);
+ EXPECT_EQ(1, cursor.peekBytes().size());
cursor.read<uint8_t>();
}
c.skip(5);
EXPECT_TRUE(c.isAtEnd());
}
+
+ // Test canAdvance with a chain of items
+ {
+ auto chain = IOBuf::create(10);
+ chain->append(10);
+ chain->appendChain(chain->clone());
+ EXPECT_EQ(2, chain->countChainElements());
+ EXPECT_EQ(20, chain->computeChainDataLength());
+
+ Cursor c(chain.get());
+ for (size_t i = 0; i <= 20; ++i) {
+ EXPECT_TRUE(c.canAdvance(i));
+ }
+ EXPECT_FALSE(c.canAdvance(21));
+ c.skip(10);
+ EXPECT_TRUE(c.canAdvance(10));
+ EXPECT_FALSE(c.canAdvance(11));
+ }
}
TEST(IOBuf, StringOperations) {
}
}
-int benchmark_size = 1000;
-unique_ptr<IOBuf> iobuf_benchmark;
-
-unique_ptr<IOBuf> iobuf_read_benchmark;
+TEST(IOBuf, ReadWhileTrue) {
+ auto isAlpha = [](uint8_t ch) {
+ return (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z');
+ };
+ auto isDigit = [](uint8_t ch) { return (ch >= '0' && ch <= '9'); };
-template <class CursClass>
-void runBenchmark() {
- CursClass c(iobuf_benchmark.get());
+ // Test reading alternating alphabetic and numeric strings
+ {
+ std::unique_ptr<IOBuf> chain(IOBuf::create(32));
+ Appender app(chain.get(), 0);
+ app.push(StringPiece("hello123world456"));
- for(int i = 0; i < benchmark_size; i++) {
- c.write((uint8_t)0);
+ Cursor curs(chain.get());
+ EXPECT_STREQ("hello", curs.readWhile(isAlpha).c_str());
+ EXPECT_STREQ("123", curs.readWhile(isDigit).c_str());
+ EXPECT_STREQ("world", curs.readWhile(isAlpha).c_str());
+ EXPECT_STREQ("456", curs.readWhile(isDigit).c_str());
+ EXPECT_TRUE(curs.isAtEnd());
}
-}
-BENCHMARK(rwPrivateCursorBenchmark, iters) {
- while (iters--) {
- runBenchmark<RWPrivateCursor>();
- }
-}
+ // The same, but also use skipWhile()
+ {
+ std::unique_ptr<IOBuf> chain(IOBuf::create(16));
+ Appender app(chain.get(), 0);
+ app.push(StringPiece("hello123world456"));
-BENCHMARK(rwUnshareCursorBenchmark, iters) {
- while (iters--) {
- runBenchmark<RWUnshareCursor>();
+ Cursor curs(chain.get());
+ EXPECT_STREQ("hello", curs.readWhile(isAlpha).c_str());
+ curs.skipWhile(isDigit);
+ curs.skipWhile(isAlpha);
+ EXPECT_STREQ("456", curs.readWhile(isDigit).c_str());
+ EXPECT_TRUE(curs.isAtEnd());
}
-}
+ // Test readWhile() using data split across multiple buffers,
+ // including some empty buffers in the middle of the chain.
+ {
+ std::unique_ptr<IOBuf> chain;
-BENCHMARK(cursorBenchmark, iters) {
- while (iters--) {
- Cursor c(iobuf_read_benchmark.get());
- for(int i = 0; i < benchmark_size ; i++) {
- c.read<uint8_t>();
- }
- }
-}
+ // First element in the chain has "he"
+ auto buf = IOBuf::create(40);
+ Appender app(buf.get(), 0);
+ app.push(StringPiece("he"));
+ chain = std::move(buf);
+
+ // The second element has "ll", after 10 bytes of headroom
+ buf = IOBuf::create(40);
+ buf->advance(10);
+ app = Appender{buf.get(), 0};
+ app.push(StringPiece("ll"));
+ chain->prependChain(std::move(buf));
-BENCHMARK(skipBenchmark, iters) {
- while (iters--) {
- Cursor c(iobuf_read_benchmark.get());
- for(int i = 0; i < benchmark_size ; i++) {
- c.peek();
- c.skip(1);
- }
- }
-}
+ // The third element is empty
+ buf = IOBuf::create(40);
+ buf->advance(15);
+ chain->prependChain(std::move(buf));
-// fbmake opt
-// _bin/folly/experimental/io/test/iobuf_cursor_test -benchmark
-//
-// Benchmark Iters Total t t/iter iter/sec
-// ---------------------------------------------------------------------------
-// rwPrivateCursorBenchmark 100000 142.9 ms 1.429 us 683.5 k
-// rwUnshareCursorBenchmark 100000 309.3 ms 3.093 us 315.7 k
-// cursorBenchmark 100000 741.4 ms 7.414 us 131.7 k
-// skipBenchmark 100000 738.9 ms 7.389 us 132.2 k
-//
-// uname -a:
-//
-// Linux dev2159.snc6.facebook.com 2.6.33-7_fbk15_104e4d0 #1 SMP
-// Tue Oct 19 22:40:30 PDT 2010 x86_64 x86_64 x86_64 GNU/Linux
-//
-// 72GB RAM, 2 CPUs (Intel(R) Xeon(R) CPU L5630 @ 2.13GHz)
-// hyperthreading disabled
-
-int main(int argc, char** argv) {
- testing::InitGoogleTest(&argc, argv);
- gflags::ParseCommandLineFlags(&argc, &argv, true);
-
- auto ret = RUN_ALL_TESTS();
-
- if (ret == 0 && FLAGS_benchmark) {
- iobuf_benchmark = IOBuf::create(benchmark_size);
- iobuf_benchmark->append(benchmark_size);
-
- iobuf_read_benchmark = IOBuf::create(1);
- for (int i = 0; i < benchmark_size; i++) {
- unique_ptr<IOBuf> iobuf2(IOBuf::create(1));
- iobuf2->append(1);
- iobuf_read_benchmark->prependChain(std::move(iobuf2));
- }
+ // The fourth element has "o12"
+ buf = IOBuf::create(40);
+ buf->advance(37);
+ app = Appender{buf.get(), 0};
+ app.push(StringPiece("o12"));
+ chain->prependChain(std::move(buf));
- folly::runBenchmarks();
- }
+ // The fifth element has "3"
+ buf = IOBuf::create(40);
+ app = Appender{buf.get(), 0};
+ app.push(StringPiece("3"));
+ chain->prependChain(std::move(buf));
- return ret;
+ // The sixth element is empty
+ buf = IOBuf::create(40);
+ chain->prependChain(std::move(buf));
+
+ // The seventh element has "world456"
+ buf = IOBuf::create(40);
+ app = Appender{buf.get(), 0};
+ app.push(StringPiece("world456"));
+ chain->prependChain(std::move(buf));
+
+ // The eighth element is empty
+ buf = IOBuf::create(40);
+ chain->prependChain(std::move(buf));
+
+ Cursor curs(chain.get());
+ EXPECT_STREQ("hello", curs.readWhile(isAlpha).c_str());
+ EXPECT_STREQ("123", curs.readWhile(isDigit).c_str());
+ EXPECT_STREQ("world", curs.readWhile(isAlpha).c_str());
+ EXPECT_STREQ("456", curs.readWhile(isDigit).c_str());
+ EXPECT_TRUE(curs.isAtEnd());
+ }
}