25 #include <glog/logging.h> 45 unsigned int limit()
const {
53 template <
class QueueType,
size_t Size,
bool Pop = false>
55 typedef typename QueueType::value_type
T;
57 explicit PerfTest() : queue_(Size), done_(false) {}
63 std::thread producer([
this] { this->producer(); });
64 std::thread consumer([
this] { this->consumer(); });
72 LOG(INFO) <<
" done: " << duration.count() <<
"ms";
79 for (
auto i = traits_.limit();
i > 0; --
i) {
80 while (!queue_.write(traits_.generate())) {
88 if (queue_.frontPtr()) {
101 std::atomic<bool> done_;
102 TestTraits<T> traits_;
105 template <
class TestType>
106 void doTest(
const char*
name) {
107 LOG(INFO) <<
" testing: " <<
name;
108 std::unique_ptr<TestType>
const t(
new TestType());
112 template <
class T,
bool Pop = false>
113 void perfTestType(
const char*
type) {
114 const size_t size = 0xfffe;
116 LOG(INFO) <<
"Type: " <<
type;
117 doTest<PerfTest<folly::ProducerConsumerQueue<T>,
size, Pop>>(
118 "ProducerConsumerQueue");
121 template <
class QueueType,
size_t Size,
bool Pop>
122 struct CorrectnessTest {
123 typedef typename QueueType::value_type
T;
125 explicit CorrectnessTest() : queue_(Size), done_(false) {
126 const size_t testSize = traits_.limit();
127 testData_.reserve(testSize);
129 testData_.push_back(traits_.generate());
134 std::thread producer([
this] { this->producer(); });
135 std::thread consumer([
this] { this->consumer(); });
143 for (
auto&
data : testData_) {
144 while (!queue_.write(
data)) {
158 for (
auto expect : testData_) {
161 if (!(data = queue_.frontPtr())) {
166 if (!(data = queue_.frontPtr())) {
181 void consumerRead() {
182 for (
auto expect : testData_) {
185 if (!queue_.read(data)) {
190 if (!queue_.read(data)) {
202 std::vector<T> testData_;
204 TestTraits<T> traits_;
205 std::atomic<bool> done_;
208 template <
class T,
bool Pop = false>
210 LOG(INFO) <<
"Type: " <<
type;
211 doTest<CorrectnessTest<folly::ProducerConsumerQueue<T>, 0xfffe, Pop>>(
212 "ProducerConsumerQueue");
216 static unsigned int numInstances;
220 DtorChecker(
const DtorChecker& ) {
228 unsigned int DtorChecker::numInstances = 0;
235 correctnessTestType<std::string, true>(
"string (front+pop)");
236 correctnessTestType<std::string>(
"string");
237 correctnessTestType<int>(
"int");
238 correctnessTestType<unsigned long long>(
"unsigned long long");
242 perfTestType<std::string, true>(
"string (front+pop)");
243 perfTestType<std::string>(
"string");
244 perfTestType<int>(
"int");
245 perfTestType<unsigned long long>(
"unsigned long long");
253 for (
int i = 0;
i < 10; ++
i) {
257 EXPECT_EQ(DtorChecker::numInstances, 10);
274 for (
int i = 0;
i < 3; ++
i) {
static std::atomic< int > testSize(1000)
#define EXPECT_EQ(val1, val2)
std::chrono::steady_clock::time_point now()
TEST(PCQ, QueueCorrectness)
constexpr auto size(C const &c) -> decltype(c.size())
void expect(LineReader &lr, const char *expected)
#define EXPECT_TRUE(condition)
bool write(Args &&...recordArgs)
#define EXPECT_FALSE(condition)
static constexpr uint64_t data[1]