2016-02-10 00:12:00 +01:00
|
|
|
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
|
2017-07-16 01:03:42 +02:00
|
|
|
// This source code is licensed under both the GPLv2 (found in the
|
|
|
|
// COPYING file in the root directory) and Apache 2.0 License
|
|
|
|
// (found in the LICENSE.Apache file in the root directory).
|
2013-12-13 00:32:56 +01:00
|
|
|
|
|
|
|
#include <atomic>
|
|
|
|
#include <iostream>
|
2016-05-20 16:42:18 +02:00
|
|
|
#include <string>
|
2014-11-06 20:14:28 +01:00
|
|
|
#include <utility>
|
2013-12-13 00:32:56 +01:00
|
|
|
|
|
|
|
#include "rocksdb/env.h"
|
2019-05-30 20:21:38 +02:00
|
|
|
#include "test_util/testharness.h"
|
|
|
|
#include "test_util/testutil.h"
|
2019-05-31 02:39:43 +02:00
|
|
|
#include "util/autovector.h"
|
|
|
|
#include "util/string_util.h"
|
2013-12-13 00:32:56 +01:00
|
|
|
|
2016-05-20 16:42:18 +02:00
|
|
|
using std::cout;
|
|
|
|
using std::endl;
|
2013-12-13 00:32:56 +01:00
|
|
|
|
2020-02-20 21:07:53 +01:00
|
|
|
namespace ROCKSDB_NAMESPACE {
|
2013-12-13 00:32:56 +01:00
|
|
|
|
2015-03-17 22:08:00 +01:00
|
|
|
class AutoVectorTest : public testing::Test {};
|
2014-01-06 20:11:19 +01:00
|
|
|
const unsigned long kSize = 8;
|
2015-06-19 00:58:00 +02:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
template <class T>
|
|
|
|
void AssertAutoVectorOnlyInStack(autovector<T, kSize>* vec, bool result) {
|
|
|
|
#ifndef ROCKSDB_LITE
|
|
|
|
ASSERT_EQ(vec->only_in_stack(), result);
|
2018-04-16 02:19:57 +02:00
|
|
|
#else
|
|
|
|
(void) vec;
|
|
|
|
(void) result;
|
2015-06-19 00:58:00 +02:00
|
|
|
#endif // !ROCKSDB_LITE
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2015-03-17 22:08:00 +01:00
|
|
|
TEST_F(AutoVectorTest, PushBackAndPopBack) {
|
2013-12-13 00:32:56 +01:00
|
|
|
autovector<size_t, kSize> vec;
|
|
|
|
ASSERT_TRUE(vec.empty());
|
|
|
|
ASSERT_EQ(0ul, vec.size());
|
|
|
|
|
|
|
|
for (size_t i = 0; i < 1000 * kSize; ++i) {
|
|
|
|
vec.push_back(i);
|
|
|
|
ASSERT_TRUE(!vec.empty());
|
|
|
|
if (i < kSize) {
|
2015-06-19 00:58:00 +02:00
|
|
|
AssertAutoVectorOnlyInStack(&vec, true);
|
2013-12-13 00:32:56 +01:00
|
|
|
} else {
|
2015-06-19 00:58:00 +02:00
|
|
|
AssertAutoVectorOnlyInStack(&vec, false);
|
2013-12-13 00:32:56 +01:00
|
|
|
}
|
|
|
|
ASSERT_EQ(i + 1, vec.size());
|
|
|
|
ASSERT_EQ(i, vec[i]);
|
|
|
|
ASSERT_EQ(i, vec.at(i));
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t size = vec.size();
|
|
|
|
while (size != 0) {
|
|
|
|
vec.pop_back();
|
|
|
|
// will always be in heap
|
2015-06-19 00:58:00 +02:00
|
|
|
AssertAutoVectorOnlyInStack(&vec, false);
|
2013-12-13 00:32:56 +01:00
|
|
|
ASSERT_EQ(--size, vec.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
ASSERT_TRUE(vec.empty());
|
|
|
|
}
|
|
|
|
|
2015-03-17 22:08:00 +01:00
|
|
|
TEST_F(AutoVectorTest, EmplaceBack) {
|
2014-11-06 20:14:28 +01:00
|
|
|
typedef std::pair<size_t, std::string> ValType;
|
|
|
|
autovector<ValType, kSize> vec;
|
2013-12-13 00:32:56 +01:00
|
|
|
|
|
|
|
for (size_t i = 0; i < 1000 * kSize; ++i) {
|
2014-11-25 05:44:49 +01:00
|
|
|
vec.emplace_back(i, ToString(i + 123));
|
2013-12-13 00:32:56 +01:00
|
|
|
ASSERT_TRUE(!vec.empty());
|
|
|
|
if (i < kSize) {
|
2015-06-19 00:58:00 +02:00
|
|
|
AssertAutoVectorOnlyInStack(&vec, true);
|
2013-12-13 00:32:56 +01:00
|
|
|
} else {
|
2015-06-19 00:58:00 +02:00
|
|
|
AssertAutoVectorOnlyInStack(&vec, false);
|
2013-12-13 00:32:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ASSERT_EQ(i + 1, vec.size());
|
|
|
|
ASSERT_EQ(i, vec[i].first);
|
2014-11-25 05:44:49 +01:00
|
|
|
ASSERT_EQ(ToString(i + 123), vec[i].second);
|
2013-12-13 00:32:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
vec.clear();
|
|
|
|
ASSERT_TRUE(vec.empty());
|
2015-06-19 00:58:00 +02:00
|
|
|
AssertAutoVectorOnlyInStack(&vec, false);
|
2013-12-13 00:32:56 +01:00
|
|
|
}
|
|
|
|
|
2015-03-17 22:08:00 +01:00
|
|
|
TEST_F(AutoVectorTest, Resize) {
|
2014-05-08 22:50:49 +02:00
|
|
|
autovector<size_t, kSize> vec;
|
|
|
|
|
|
|
|
vec.resize(kSize);
|
2015-06-19 00:58:00 +02:00
|
|
|
AssertAutoVectorOnlyInStack(&vec, true);
|
2014-05-08 22:50:49 +02:00
|
|
|
for (size_t i = 0; i < kSize; ++i) {
|
|
|
|
vec[i] = i;
|
|
|
|
}
|
|
|
|
|
|
|
|
vec.resize(kSize * 2);
|
2015-06-19 00:58:00 +02:00
|
|
|
AssertAutoVectorOnlyInStack(&vec, false);
|
2014-05-08 22:50:49 +02:00
|
|
|
for (size_t i = 0; i < kSize; ++i) {
|
|
|
|
ASSERT_EQ(vec[i], i);
|
|
|
|
}
|
|
|
|
for (size_t i = 0; i < kSize; ++i) {
|
|
|
|
vec[i + kSize] = i;
|
|
|
|
}
|
|
|
|
|
|
|
|
vec.resize(1);
|
|
|
|
ASSERT_EQ(1U, vec.size());
|
|
|
|
}
|
|
|
|
|
2014-04-10 06:17:14 +02:00
|
|
|
namespace {
|
2013-12-13 00:32:56 +01:00
|
|
|
void AssertEqual(
|
|
|
|
const autovector<size_t, kSize>& a, const autovector<size_t, kSize>& b) {
|
|
|
|
ASSERT_EQ(a.size(), b.size());
|
|
|
|
ASSERT_EQ(a.empty(), b.empty());
|
2015-06-19 00:58:00 +02:00
|
|
|
#ifndef ROCKSDB_LITE
|
2013-12-13 00:32:56 +01:00
|
|
|
ASSERT_EQ(a.only_in_stack(), b.only_in_stack());
|
2015-06-19 00:58:00 +02:00
|
|
|
#endif // !ROCKSDB_LITE
|
2013-12-13 00:32:56 +01:00
|
|
|
for (size_t i = 0; i < a.size(); ++i) {
|
|
|
|
ASSERT_EQ(a[i], b[i]);
|
|
|
|
}
|
|
|
|
}
|
2014-04-10 06:17:14 +02:00
|
|
|
} // namespace
|
2013-12-13 00:32:56 +01:00
|
|
|
|
2015-03-17 22:08:00 +01:00
|
|
|
TEST_F(AutoVectorTest, CopyAndAssignment) {
|
2013-12-13 00:32:56 +01:00
|
|
|
// Test both heap-allocated and stack-allocated cases.
|
|
|
|
for (auto size : { kSize / 2, kSize * 1000 }) {
|
|
|
|
autovector<size_t, kSize> vec;
|
|
|
|
for (size_t i = 0; i < size; ++i) {
|
|
|
|
vec.push_back(i);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
autovector<size_t, kSize> other;
|
|
|
|
other = vec;
|
|
|
|
AssertEqual(other, vec);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
autovector<size_t, kSize> other(vec);
|
|
|
|
AssertEqual(other, vec);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-17 22:08:00 +01:00
|
|
|
TEST_F(AutoVectorTest, Iterators) {
|
2013-12-13 00:32:56 +01:00
|
|
|
autovector<std::string, kSize> vec;
|
|
|
|
for (size_t i = 0; i < kSize * 1000; ++i) {
|
2014-11-25 05:44:49 +01:00
|
|
|
vec.push_back(ToString(i));
|
2013-12-13 00:32:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// basic operator test
|
|
|
|
ASSERT_EQ(vec.front(), *vec.begin());
|
|
|
|
ASSERT_EQ(vec.back(), *(vec.end() - 1));
|
|
|
|
ASSERT_TRUE(vec.begin() < vec.end());
|
|
|
|
|
|
|
|
// non-const iterator
|
|
|
|
size_t index = 0;
|
|
|
|
for (const auto& item : vec) {
|
|
|
|
ASSERT_EQ(vec[index++], item);
|
|
|
|
}
|
|
|
|
|
|
|
|
index = vec.size() - 1;
|
|
|
|
for (auto pos = vec.rbegin(); pos != vec.rend(); ++pos) {
|
|
|
|
ASSERT_EQ(vec[index--], *pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
// const iterator
|
|
|
|
const auto& cvec = vec;
|
|
|
|
index = 0;
|
|
|
|
for (const auto& item : cvec) {
|
|
|
|
ASSERT_EQ(cvec[index++], item);
|
|
|
|
}
|
|
|
|
|
|
|
|
index = vec.size() - 1;
|
|
|
|
for (auto pos = cvec.rbegin(); pos != cvec.rend(); ++pos) {
|
|
|
|
ASSERT_EQ(cvec[index--], *pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
// forward and backward
|
|
|
|
auto pos = vec.begin();
|
|
|
|
while (pos != vec.end()) {
|
|
|
|
auto old_val = *pos;
|
|
|
|
auto old = pos++;
|
|
|
|
// HACK: make sure -> works
|
|
|
|
ASSERT_TRUE(!old->empty());
|
|
|
|
ASSERT_EQ(old_val, *old);
|
2013-12-27 00:56:20 +01:00
|
|
|
ASSERT_TRUE(pos == vec.end() || old_val != *pos);
|
2013-12-13 00:32:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pos = vec.begin();
|
2013-12-27 00:56:20 +01:00
|
|
|
for (size_t i = 0; i < vec.size(); i += 2) {
|
2013-12-13 00:32:56 +01:00
|
|
|
// Cannot use ASSERT_EQ since that macro depends on iostream serialization
|
|
|
|
ASSERT_TRUE(pos + 2 - 2 == pos);
|
|
|
|
pos += 2;
|
|
|
|
ASSERT_TRUE(pos >= vec.begin());
|
|
|
|
ASSERT_TRUE(pos <= vec.end());
|
2013-12-27 00:56:20 +01:00
|
|
|
|
|
|
|
size_t diff = static_cast<size_t>(pos - vec.begin());
|
|
|
|
ASSERT_EQ(i + 2, diff);
|
2013-12-13 00:32:56 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-10 06:17:14 +02:00
|
|
|
namespace {
|
2016-05-20 16:42:18 +02:00
|
|
|
std::vector<std::string> GetTestKeys(size_t size) {
|
|
|
|
std::vector<std::string> keys;
|
2013-12-13 00:32:56 +01:00
|
|
|
keys.resize(size);
|
|
|
|
|
|
|
|
int index = 0;
|
|
|
|
for (auto& key : keys) {
|
2020-02-20 21:07:53 +01:00
|
|
|
key = "item-" + ROCKSDB_NAMESPACE::ToString(index++);
|
2013-12-13 00:32:56 +01:00
|
|
|
}
|
|
|
|
return keys;
|
|
|
|
}
|
2014-04-10 06:17:14 +02:00
|
|
|
} // namespace
|
2013-12-13 00:32:56 +01:00
|
|
|
|
2016-05-20 16:42:18 +02:00
|
|
|
template <class TVector>
|
2013-12-13 00:32:56 +01:00
|
|
|
void BenchmarkVectorCreationAndInsertion(
|
2016-05-20 16:42:18 +02:00
|
|
|
std::string name, size_t ops, size_t item_size,
|
2013-12-13 00:32:56 +01:00
|
|
|
const std::vector<typename TVector::value_type>& items) {
|
|
|
|
auto env = Env::Default();
|
|
|
|
|
|
|
|
int index = 0;
|
|
|
|
auto start_time = env->NowNanos();
|
|
|
|
auto ops_remaining = ops;
|
|
|
|
while(ops_remaining--) {
|
|
|
|
TVector v;
|
|
|
|
for (size_t i = 0; i < item_size; ++i) {
|
|
|
|
v.push_back(items[index++]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
auto elapsed = env->NowNanos() - start_time;
|
|
|
|
cout << "created " << ops << " " << name << " instances:\n\t"
|
|
|
|
<< "each was inserted with " << item_size << " elements\n\t"
|
|
|
|
<< "total time elapsed: " << elapsed << " (ns)" << endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class TVector>
|
2016-05-20 16:42:18 +02:00
|
|
|
size_t BenchmarkSequenceAccess(std::string name, size_t ops, size_t elem_size) {
|
2013-12-13 00:32:56 +01:00
|
|
|
TVector v;
|
|
|
|
for (const auto& item : GetTestKeys(elem_size)) {
|
|
|
|
v.push_back(item);
|
|
|
|
}
|
|
|
|
auto env = Env::Default();
|
|
|
|
|
|
|
|
auto ops_remaining = ops;
|
|
|
|
auto start_time = env->NowNanos();
|
|
|
|
size_t total = 0;
|
|
|
|
while (ops_remaining--) {
|
|
|
|
auto end = v.end();
|
|
|
|
for (auto pos = v.begin(); pos != end; ++pos) {
|
|
|
|
total += pos->size();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
auto elapsed = env->NowNanos() - start_time;
|
|
|
|
cout << "performed " << ops << " sequence access against " << name << "\n\t"
|
|
|
|
<< "size: " << elem_size << "\n\t"
|
|
|
|
<< "total time elapsed: " << elapsed << " (ns)" << endl;
|
2013-12-27 00:56:20 +01:00
|
|
|
// HACK avoid compiler's optimization to ignore total
|
|
|
|
return total;
|
2013-12-13 00:32:56 +01:00
|
|
|
}
|
|
|
|
|
2016-05-20 16:42:18 +02:00
|
|
|
// This test case only reports the performance between std::vector<std::string>
|
|
|
|
// and autovector<std::string>. We chose string for comparison because in most
|
|
|
|
// of our use cases we used std::vector<std::string>.
|
2015-03-17 22:08:00 +01:00
|
|
|
TEST_F(AutoVectorTest, PerfBench) {
|
2013-12-13 00:32:56 +01:00
|
|
|
// We run same operations for kOps times in order to get a more fair result.
|
|
|
|
size_t kOps = 100000;
|
|
|
|
|
|
|
|
// Creation and insertion test
|
|
|
|
// Test the case when there is:
|
|
|
|
// * no element inserted: internal array of std::vector may not really get
|
|
|
|
// initialize.
|
|
|
|
// * one element inserted: internal array of std::vector must have
|
|
|
|
// initialized.
|
|
|
|
// * kSize elements inserted. This shows the most time we'll spend if we
|
|
|
|
// keep everything in stack.
|
|
|
|
// * 2 * kSize elements inserted. The internal vector of
|
|
|
|
// autovector must have been initialized.
|
|
|
|
cout << "=====================================================" << endl;
|
|
|
|
cout << "Creation and Insertion Test (value type: std::string)" << endl;
|
|
|
|
cout << "=====================================================" << endl;
|
|
|
|
|
|
|
|
// pre-generated unique keys
|
|
|
|
auto string_keys = GetTestKeys(kOps * 2 * kSize);
|
|
|
|
for (auto insertions : { 0ul, 1ul, kSize / 2, kSize, 2 * kSize }) {
|
2016-05-20 16:42:18 +02:00
|
|
|
BenchmarkVectorCreationAndInsertion<std::vector<std::string>>(
|
|
|
|
"std::vector<std::string>", kOps, insertions, string_keys);
|
|
|
|
BenchmarkVectorCreationAndInsertion<autovector<std::string, kSize>>(
|
|
|
|
"autovector<std::string>", kOps, insertions, string_keys);
|
2013-12-13 00:32:56 +01:00
|
|
|
cout << "-----------------------------------" << endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
cout << "=====================================================" << endl;
|
|
|
|
cout << "Creation and Insertion Test (value type: uint64_t)" << endl;
|
|
|
|
cout << "=====================================================" << endl;
|
|
|
|
|
|
|
|
// pre-generated unique keys
|
2016-05-20 16:42:18 +02:00
|
|
|
std::vector<uint64_t> int_keys(kOps * 2 * kSize);
|
2013-12-13 00:32:56 +01:00
|
|
|
for (size_t i = 0; i < kOps * 2 * kSize; ++i) {
|
|
|
|
int_keys[i] = i;
|
|
|
|
}
|
|
|
|
for (auto insertions : { 0ul, 1ul, kSize / 2, kSize, 2 * kSize }) {
|
2016-05-20 16:42:18 +02:00
|
|
|
BenchmarkVectorCreationAndInsertion<std::vector<uint64_t>>(
|
|
|
|
"std::vector<uint64_t>", kOps, insertions, int_keys);
|
2013-12-13 00:32:56 +01:00
|
|
|
BenchmarkVectorCreationAndInsertion<autovector<uint64_t, kSize>>(
|
|
|
|
"autovector<uint64_t>", kOps, insertions, int_keys
|
|
|
|
);
|
|
|
|
cout << "-----------------------------------" << endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sequence Access Test
|
|
|
|
cout << "=====================================================" << endl;
|
|
|
|
cout << "Sequence Access Test" << endl;
|
|
|
|
cout << "=====================================================" << endl;
|
|
|
|
for (auto elem_size : { kSize / 2, kSize, 2 * kSize }) {
|
2016-05-20 16:42:18 +02:00
|
|
|
BenchmarkSequenceAccess<std::vector<std::string>>("std::vector", kOps,
|
|
|
|
elem_size);
|
|
|
|
BenchmarkSequenceAccess<autovector<std::string, kSize>>("autovector", kOps,
|
|
|
|
elem_size);
|
2013-12-13 00:32:56 +01:00
|
|
|
cout << "-----------------------------------" << endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-20 21:07:53 +01:00
|
|
|
} // namespace ROCKSDB_NAMESPACE
|
2013-12-13 00:32:56 +01:00
|
|
|
|
|
|
|
int main(int argc, char** argv) {
|
2015-03-17 22:08:00 +01:00
|
|
|
::testing::InitGoogleTest(&argc, argv);
|
|
|
|
return RUN_ALL_TESTS();
|
2013-12-13 00:32:56 +01:00
|
|
|
}
|