简体   繁体   English

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

[英]Implementation of a simple unit test for lock-free queue

I am trying to compare performance of different lock-free queues, therefore, I want to create a unit test - which includes pushing/poping user-defined pre-built objects to and from the queue. 我正在尝试比较不同的无锁队列的性能,因此,我想创建一个单元测试-包括将用户定义的预构建对象与队列进行推送/弹出。 Therefore, I want to ask you couple of questions:- 1) How to create pre-built objects in a simple manner. 因此,我想问您几个问题:-1)如何以简单的方式创建预建对象。 Does creating an array like I did would fulfill the purpose. 像我那样创建数组是否可以达到目的。 2) I am getting an error "terminate called after throwing an instance of 'std::system_error' what(): Invalid argument Aborted (core dumped)". 2)我收到一个错误“抛出'std :: system_error'what()实例后终止调用:无效参数终止(核心转储)”。

Thanx in advance. 提前感谢。

#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;
}

Just illustrating how to use Data elements in the benchmark, though this does add a cout within the measured time which isn't ideal but probably isn't significant either. 只是说明如何使用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];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM