为无锁队列实现简单的单元测试

时间:2014-08-29 07:00:23

标签: c++ unit-testing

我试图比较不同无锁队列的性能,因此,我想创建一个单元测试 - 包括向队列推送/弹出用户定义的预构建对象。因此,我想问你几个问题: - 1)如何以简单的方式创建预构建的对象。像我这样创建一个数组是否能达到目的。 2)我收到一个错误“在抛出'std :: system_error'的实例后调用终止what():无效的参数Aborted(core dumped)”。

提前完成。

#include <cstdlib>
#include <stdio.h>
#include <string>
#include <chrono>
#include <iostream>
#include <ctime>
#include <atomic>
#include <thread>
#include <boost/lockfree/queue.hpp>

using namespace std;

const long NUM_DATA = 10;
const int NUM_PROD_THREAD = 2;
const int NUM_CONSUM_THREAD = 2;
const long NUM_ITEM = 1000000;


class Data
{
public:
    Data(){}
    void dataPrint() {cout << "Hello";}
private:
    long i;
    double j;
};


Data *DataArray = new Data[NUM_DATA];
boost::lockfree::queue<Data*> BoostQueue(1000);

struct Producer
{
    void operator()()
    {
        for(long i=0; i<1000000; i++)
            BoostQueue.push( DataArray );
    }
};


struct Consumer
{
    Data *pData;
    void operator()()
    {
        while (  BoostQueue.pop( pData ) ) ;
    }
};


int main(int argc, char** argv)
{
    std::thread thrd [NUM_PROD_THREAD + NUM_CONSUM_THREAD];

    std::chrono::duration<double> elapsed_seconds;

    auto start = std::chrono::high_resolution_clock::now();
    for ( int i = 0; i < NUM_PROD_THREAD;  i++ )
    {
        thrd[i] = std::thread{ Producer() };
    }

    for ( int i = 0; i < NUM_CONSUM_THREAD; i++ )
    {
        thrd[NUM_PROD_THREAD+i] = std::thread{Consumer()};
    }

    for ( int i = 0; i < NUM_CONSUM_THREAD; i++ )
    {
        thrd[i].join();
    }

    auto end = std::chrono::high_resolution_clock::now();
    elapsed_seconds = end - start;
    std::cout << "Enqueue and Dequeue 1 million item in:" << elapsed_seconds.count() << std::endl;

    for ( int i = 0; i < NUM_PROD_THREAD; i++ )
    {
        thrd[i].join();
    }

    return 0;
}

1 个答案:

答案 0 :(得分:0)

仅举例说明如何在基准测试中使用Data元素,但这会在测量时间内添加cout,这不是理想的,但可能也不重要。

class Data
{
public:
    Data(long i) : i_(i) {}
    void dataPrint() {cout << "Hello";}
private:
    long i_;
    double j;
};


Data* dataArray[1000000];
for (int i = 0; i < NUM_DATA; ++i) dataArray[i] = new Data(i);

boost::lockfree::queue<Data*> BoostQueue(1000);

struct Producer
{
    void operator()()
    {
        for(long i=0; i<1000000; i++)
            BoostQueue.push( dataArray[i] );
    }
};


struct Consumer
{
    Data *pData;
    long result_;
    void operator()()
    {
        result_ = 0;
        while (  BoostQueue.pop( pData ) )
            result_ += pData->i_;
        std::cout << result_ << '\n';
    }
};


int main(int argc, char** argv)
{
    std::thread thrd [NUM_PROD_THREAD + NUM_CONSUM_THREAD];

    std::chrono::duration<double> elapsed_seconds;

    auto start = std::chrono::high_resolution_clock::now();
    for ( int i = 0; i < NUM_PROD_THREAD;  i++ )
        thrd[i] = std::thread{ Producer() };

    for ( int i = 0; i < NUM_CONSUM_THREAD; i++ )
        thrd[NUM_PROD_THREAD+i] = std::thread{Consumer()};

    for ( int i = 0; i < NUM_CONSUM_THREAD; i++ )
        thrd[NUM_PROD_THREAD+i].join();

    auto end = std::chrono::high_resolution_clock::now();
    elapsed_seconds = end - start;
    std::cout << "Enqueue and Dequeue 1 million item in:"
        << elapsed_seconds.count() << std::endl;

    for ( int i = 0; i < NUM_PROD_THREAD; i++ )
        thrd[i].join();
    for (int i = 0; i < 1000000; ++i)
        delete dataArray[i];
}