Commit 0ac0f114 authored by Xiao Shi's avatar Xiao Shi Committed by Facebook Github Bot

refactor fbvector test and benchmark, enable test, DRY

Summary:
Previously, there are a number of tests in `FBVectorTestBenchmarks.cpp.h` that
are not used anywhere. This diff separates out the tests from the benchmarks
and actually include them in `FBVectorTest.cpp`.

Reviewed By: yfeldblum

Differential Revision: D7103453

fbshipit-source-id: 9427bb1ddcebefb2bbd2c91707fdd572890dc42e
parent d2bc1134
...@@ -460,7 +460,8 @@ nobase_follyinclude_HEADERS = \ ...@@ -460,7 +460,8 @@ nobase_follyinclude_HEADERS = \
Synchronized.h \ Synchronized.h \
SynchronizedPtr.h \ SynchronizedPtr.h \
test/FBStringTestBenchmarks.cpp.h \ test/FBStringTestBenchmarks.cpp.h \
test/FBVectorTestBenchmarks.cpp.h \ test/FBVectorBenchmarks.cpp.h \
test/FBVectorTests.cpp.h \
test/function_benchmark/benchmark_impl.h \ test/function_benchmark/benchmark_impl.h \
test/function_benchmark/test_functions.h \ test/function_benchmark/test_functions.h \
test/SynchronizedTestLib.h \ test/SynchronizedTestLib.h \
......
...@@ -22,72 +22,15 @@ ...@@ -22,72 +22,15 @@
#include <boost/random.hpp> #include <boost/random.hpp>
#include <folly/Benchmark.h>
#include <folly/FBString.h>
#include <folly/FBVector.h> #include <folly/FBVector.h>
#include <folly/Random.h>
#include <folly/Traits.h> #include <folly/Traits.h>
#include <folly/container/Foreach.h> #include <folly/container/Foreach.h>
#include <folly/portability/GFlags.h> #include <folly/portability/GFlags.h>
#include <folly/portability/GTest.h> #include <folly/test/FBVectorTestUtil.h>
using namespace std; using namespace std;
using namespace folly; using namespace folly;
using namespace folly::test::detail;
auto static const seed = randomNumberSeed();
typedef boost::mt19937 RandomT;
static RandomT rng(seed);
template <class Integral1, class Integral2>
Integral2 random(Integral1 low, Integral2 up) {
boost::uniform_int<> range(low, up);
return range(rng);
}
template <class String>
void randomString(String* toFill, unsigned int maxSize = 1000) {
assert(toFill);
toFill->resize(random(0, maxSize));
for (auto& c : *toFill) {
c = random('a', 'z');
}
}
template <class String, class Integral>
void Num2String(String& str, Integral /* n */) {
str.resize(10, '\0');
sprintf(&str[0], "%ul", 10);
str.resize(strlen(str.c_str()));
}
std::list<char> RandomList(unsigned int maxSize) {
std::list<char> lst(random(0u, maxSize));
std::list<char>::iterator i = lst.begin();
for (; i != lst.end(); ++i) {
*i = random('a', 'z');
}
return lst;
}
template <class T>
T randomObject();
template <>
int randomObject<int>() {
return random(0, 1024);
}
template <>
folly::fbstring randomObject<folly::fbstring>() {
folly::fbstring result;
randomString(&result);
return result;
}
#define CONCAT(A, B) CONCAT_HELPER(A, B)
#define CONCAT_HELPER(A, B) A##B
#define BENCHFUN(F) CONCAT(CONCAT(BM_, F), CONCAT(_, VECTOR))
#define TESTFUN(F) TEST(fbvector, CONCAT(F, VECTOR))
typedef vector<int> IntVector; typedef vector<int> IntVector;
typedef fbvector<int> IntFBVector; typedef fbvector<int> IntFBVector;
...@@ -95,16 +38,16 @@ typedef vector<folly::fbstring> FBStringVector; ...@@ -95,16 +38,16 @@ typedef vector<folly::fbstring> FBStringVector;
typedef fbvector<folly::fbstring> FBStringFBVector; typedef fbvector<folly::fbstring> FBStringFBVector;
#define VECTOR IntVector #define VECTOR IntVector
#include <folly/test/FBVectorTestBenchmarks.cpp.h> // nolint #include <folly/test/FBVectorBenchmarks.cpp.h> // nolint
#undef VECTOR #undef VECTOR
#define VECTOR IntFBVector #define VECTOR IntFBVector
#include <folly/test/FBVectorTestBenchmarks.cpp.h> // nolint #include <folly/test/FBVectorBenchmarks.cpp.h> // nolint
#undef VECTOR #undef VECTOR
#define VECTOR FBStringVector #define VECTOR FBStringVector
#include <folly/test/FBVectorTestBenchmarks.cpp.h> // nolint #include <folly/test/FBVectorBenchmarks.cpp.h> // nolint
#undef VECTOR #undef VECTOR
#define VECTOR FBStringFBVector #define VECTOR FBStringFBVector
#include <folly/test/FBVectorTestBenchmarks.cpp.h> // nolint #include <folly/test/FBVectorBenchmarks.cpp.h> // nolint
#undef VECTOR #undef VECTOR
int main(int argc, char** argv) { int main(int argc, char** argv) {
......
/*
* Copyright 2011-present Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* This file is supposed to be included from within
* FBVectorBenchmark. Do not use otherwise.
*/
BENCHMARK(BENCHFUN(zzInitRNG)) {
// LOG(INFO) << "\nTesting with type " << typeid(VECTOR).name() << "\n";
srand(seed);
}
BENCHMARK(BENCHFUN(defaultCtor), iters) {
FOR_EACH_RANGE (i, 0, iters) {
VECTOR v[4096];
doNotOptimizeAway(&v);
}
}
void BENCHFUN(sizeCtor)(int iters, int size) {
FOR_EACH_RANGE (i, 0, iters) {
VECTOR v(size);
doNotOptimizeAway(&v);
}
}
BENCHMARK_PARAM(BENCHFUN(sizeCtor), 128);
BENCHMARK_PARAM(BENCHFUN(sizeCtor), 1024);
BENCHMARK_PARAM(BENCHFUN(sizeCtor), 1048576);
void BENCHFUN(fillCtor)(int iters, int size) {
FOR_EACH_RANGE (i, 0, iters) {
VECTOR v(size_t(size), randomObject<VECTOR::value_type>());
doNotOptimizeAway(&v);
}
}
BENCHMARK_PARAM(BENCHFUN(fillCtor), 128);
BENCHMARK_PARAM(BENCHFUN(fillCtor), 1024);
BENCHMARK_PARAM(BENCHFUN(fillCtor), 10240);
void BENCHFUN(pushBack)(int iters, int size) {
auto const obj = randomObject<VECTOR::value_type>();
FOR_EACH_RANGE (i, 0, iters) {
VECTOR v;
FOR_EACH_RANGE (j, 0, size) { v.push_back(obj); }
}
}
BENCHMARK_PARAM(BENCHFUN(pushBack), 128);
BENCHMARK_PARAM(BENCHFUN(pushBack), 1024);
BENCHMARK_PARAM(BENCHFUN(pushBack), 10240);
BENCHMARK_PARAM(BENCHFUN(pushBack), 102400);
BENCHMARK_PARAM(BENCHFUN(pushBack), 512000);
void BENCHFUN(reserve)(int iters, int /* size */) {
auto const obj = randomObject<VECTOR::value_type>();
VECTOR v(random(0U, 10000U), obj);
FOR_EACH_RANGE (i, 0, iters) { v.reserve(random(0U, 100000U)); }
}
BENCHMARK_PARAM(BENCHFUN(reserve), 128);
BENCHMARK_PARAM(BENCHFUN(reserve), 1024);
BENCHMARK_PARAM(BENCHFUN(reserve), 10240);
void BENCHFUN(insert)(int iters, int /* size */) {
auto const obj1 = randomObject<VECTOR::value_type>();
auto const obj2 = randomObject<VECTOR::value_type>();
VECTOR v(random(0U, 1U), obj1);
FOR_EACH_RANGE (i, 0, iters / 100) { v.insert(v.begin(), obj2); }
}
BENCHMARK_PARAM(BENCHFUN(insert), 100);
void BENCHFUN(erase)(int iters, int /* size */) {
auto const obj1 = randomObject<VECTOR::value_type>();
VECTOR v(random(0U, 100U), obj1);
FOR_EACH_RANGE (i, 0, iters) {
if (v.empty()) {
continue;
}
v.erase(v.begin());
}
}
BENCHMARK_PARAM(BENCHFUN(erase), 1024);
...@@ -30,48 +30,21 @@ ...@@ -30,48 +30,21 @@
#include <folly/Traits.h> #include <folly/Traits.h>
#include <folly/container/Foreach.h> #include <folly/container/Foreach.h>
#include <folly/portability/GTest.h> #include <folly/portability/GTest.h>
#include <folly/test/FBVectorTestUtil.h>
using namespace std; using namespace std;
using namespace folly; using namespace folly;
using namespace folly::test::detail;
namespace { using IntFBVector = fbvector<int>;
using FBStringFBVector = fbvector<fbstring>;
auto static const seed = randomNumberSeed();
typedef boost::mt19937 RandomT;
static RandomT rng(seed);
template <class Integral1, class Integral2>
Integral2 random(Integral1 low, Integral2 up) {
boost::uniform_int<> range(low, up);
return range(rng);
}
template <class String>
void randomString(String* toFill, unsigned int maxSize = 1000) {
assert(toFill);
toFill->resize(random(0, maxSize));
FOR_EACH (i, *toFill) {
*i = random('a', 'z');
}
}
template <class String, class Integral>
void Num2String(String& str, Integral /* n */) {
str.resize(10, '\0');
sprintf(&str[0], "%ul", 10);
str.resize(strlen(str.c_str()));
}
template <class T> T randomObject();
template <> int randomObject<int>() {
return random(0, 1024);
}
} // namespace
//////////////////////////////////////////////////////////////////////////////// #define VECTOR IntFBVector
// Tests begin here #include <folly/test/FBVectorTests.cpp.h> // nolint
//////////////////////////////////////////////////////////////////////////////// #undef VECTOR
#define VECTOR FBStringFBVector
#include <folly/test/FBVectorTests.cpp.h> // nolint
#undef VECTOR
TEST(fbvector, clause_23_3_6_1_3_ambiguity) { TEST(fbvector, clause_23_3_6_1_3_ambiguity) {
fbvector<int> v(10, 20); fbvector<int> v(10, 20);
......
/*
* Copyright 2012-present Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
//
// Author: andrei.alexandrescu@fb.com
#include <folly/Benchmark.h>
#include <folly/FBString.h>
#include <folly/Random.h>
#include <folly/portability/GTest.h>
namespace folly {
namespace test {
namespace detail {
auto static const seed = randomNumberSeed();
typedef boost::mt19937 RandomT;
static RandomT rng(seed);
template <class Integral1, class Integral2>
Integral2 random(Integral1 low, Integral2 up) {
boost::uniform_int<> range(low, up);
return range(rng);
}
template <class String>
void randomString(String* toFill, unsigned int maxSize = 1000) {
assert(toFill);
toFill->resize(random(0, maxSize));
for (auto& c : *toFill) {
c = random('a', 'z');
}
}
template <class String, class Integral>
void Num2String(String& str, Integral /* n */) {
str.resize(10, '\0');
sprintf(&str[0], "%ul", 10);
str.resize(strlen(str.c_str()));
}
std::list<char> RandomList(unsigned int maxSize) {
std::list<char> lst(random(0u, maxSize));
std::list<char>::iterator i = lst.begin();
for (; i != lst.end(); ++i) {
*i = random('a', 'z');
}
return lst;
}
template <class T>
T randomObject();
template <>
int randomObject<int>() {
return random(0, 1024);
}
template <>
folly::fbstring randomObject<folly::fbstring>() {
folly::fbstring result;
randomString(&result);
return result;
}
#define CONCAT(A, B) CONCAT_HELPER(A, B)
#define CONCAT_HELPER(A, B) A##B
#define BENCHFUN(F) CONCAT(CONCAT(BM_, F), CONCAT(_, VECTOR))
#define TESTFUN(F) TEST(fbvector, CONCAT(F, VECTOR))
} // namespace detail
} // namespace test
} // namespace folly
...@@ -272,77 +272,3 @@ TESTFUN(clause_23_3_6_4_clear) { ...@@ -272,77 +272,3 @@ TESTFUN(clause_23_3_6_4_clear) {
EXPECT_TRUE(v.empty()); EXPECT_TRUE(v.empty());
EXPECT_EQ(v.capacity(), c); EXPECT_EQ(v.capacity(), c);
} }
BENCHMARK(BENCHFUN(zzInitRNG)) {
// LOG(INFO) << "\nTesting with type " << typeid(VECTOR).name() << "\n";
srand(seed);
}
BENCHMARK(BENCHFUN(defaultCtor), iters) {
FOR_EACH_RANGE (i, 0, iters) {
VECTOR v[4096];
doNotOptimizeAway(&v);
}
}
void BENCHFUN(sizeCtor)(int iters, int size) {
FOR_EACH_RANGE (i, 0, iters) {
VECTOR v(size);
doNotOptimizeAway(&v);
}
}
BENCHMARK_PARAM(BENCHFUN(sizeCtor), 128);
BENCHMARK_PARAM(BENCHFUN(sizeCtor), 1024);
BENCHMARK_PARAM(BENCHFUN(sizeCtor), 1048576);
void BENCHFUN(fillCtor)(int iters, int size) {
FOR_EACH_RANGE (i, 0, iters) {
VECTOR v(size_t(size), randomObject<VECTOR::value_type>());
doNotOptimizeAway(&v);
}
}
BENCHMARK_PARAM(BENCHFUN(fillCtor), 128);
BENCHMARK_PARAM(BENCHFUN(fillCtor), 1024);
BENCHMARK_PARAM(BENCHFUN(fillCtor), 10240);
void BENCHFUN(pushBack)(int iters, int size) {
auto const obj = randomObject<VECTOR::value_type>();
FOR_EACH_RANGE (i, 0, iters) {
VECTOR v;
FOR_EACH_RANGE (j, 0, size) { v.push_back(obj); }
}
}
BENCHMARK_PARAM(BENCHFUN(pushBack), 128);
BENCHMARK_PARAM(BENCHFUN(pushBack), 1024);
BENCHMARK_PARAM(BENCHFUN(pushBack), 10240);
BENCHMARK_PARAM(BENCHFUN(pushBack), 102400);
BENCHMARK_PARAM(BENCHFUN(pushBack), 512000);
void BENCHFUN(reserve)(int iters, int /* size */) {
auto const obj = randomObject<VECTOR::value_type>();
VECTOR v(random(0U, 10000U), obj);
FOR_EACH_RANGE (i, 0, iters) { v.reserve(random(0U, 100000U)); }
}
BENCHMARK_PARAM(BENCHFUN(reserve), 128);
BENCHMARK_PARAM(BENCHFUN(reserve), 1024);
BENCHMARK_PARAM(BENCHFUN(reserve), 10240);
void BENCHFUN(insert)(int iters, int /* size */) {
auto const obj1 = randomObject<VECTOR::value_type>();
auto const obj2 = randomObject<VECTOR::value_type>();
VECTOR v(random(0U, 1U), obj1);
FOR_EACH_RANGE (i, 0, iters / 100) { v.insert(v.begin(), obj2); }
}
BENCHMARK_PARAM(BENCHFUN(insert), 100);
void BENCHFUN(erase)(int iters, int /* size */) {
auto const obj1 = randomObject<VECTOR::value_type>();
VECTOR v(random(0U, 100U), obj1);
FOR_EACH_RANGE (i, 0, iters) {
if (v.empty()) {
continue;
}
v.erase(v.begin());
}
}
BENCHMARK_PARAM(BENCHFUN(erase), 1024);
...@@ -39,7 +39,8 @@ thread_local_test_lib_la_LDFLAGS = -module -rpath /force_shared ...@@ -39,7 +39,8 @@ thread_local_test_lib_la_LDFLAGS = -module -rpath /force_shared
thread_local_test_lib_la_LIBADD = $(top_builddir)/libfolly.la thread_local_test_lib_la_LIBADD = $(top_builddir)/libfolly.la
noinst_HEADERS = FBStringTestBenchmarks.cpp.h \ noinst_HEADERS = FBStringTestBenchmarks.cpp.h \
FBVectorTestBenchmarks.cpp.h FBVectorBenchmarks.cpp.h \
FBVectorTests.cpp.h
spin_lock_test_SOURCES = SpinLockTest.cpp spin_lock_test_SOURCES = SpinLockTest.cpp
spin_lock_test_LDADD = libfollytestmain.la spin_lock_test_LDADD = libfollytestmain.la
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment