test.cpp 3.05 KB
#include <sched.h>
#include <chrono>
#include <vector>
#include <algorithm>
#include <iostream>
#include <thread>
#include "Consumer.h"
#include "Producer.h"


#define LOOPS 10000000
//0
#define CONSUMERS 2
#define NSPS 1000000000

Producer p("test");

using namespace std;
void testProducerTask();
void testConsumerTask();

std::atomic<bool> stopc(false);
std::atomic<uint32_t> cid(0);

std::vector<double> cltime[CONSUMERS];

int main() {
	double min,max,avg;
	sched_param sch_params;
	sch_params.sched_priority=1;
	if (pthread_setschedparam(pthread_self(),SCHED_FIFO,&sch_params)!=0)
		std::cout<<"Error priority"<<std::endl;

	thread testProducerThread;
	thread testConsumerThread[CONSUMERS];

	p.create(1000000, 20000);

	testProducerThread=thread(testProducerTask);
	for (uint32_t i=0;i<CONSUMERS;i++)
		testConsumerThread[i]=thread(testConsumerTask);
	testProducerThread.join();
	for (uint32_t i=0;i<CONSUMERS;i++)
		testConsumerThread[i].join();

	for (uint32_t i=0;i<CONSUMERS;i++){
		min=2e9;
		max=avg=0;
		sort(cltime[i].begin(),cltime[i].end());
		for (uint64_t t=0;t<LOOPS;t++){
			cltime[i][t]-=157e-9;
			if (cltime[i][t]<min)
				min=cltime[i][t];
			if (cltime[i][t]>max)
				max=cltime[i][t];
			avg+=cltime[i][t];
		}
		cout<<"MIN: "<<min<<endl;
		cout<<"MAX: "<<max<<endl;
		cout<<"AVG: "<<avg/LOOPS<<endl;
		cout<<"99%: "<<cltime[i][(off_t)((double)LOOPS*0.99)]<<endl;
	}
	return 0;
}

void testProducerTask(){
	std::chrono::high_resolution_clock::time_point t[2];
	DataBlock b;

	p.init();

	sleep(2);

	cout<<"VALIDATION/WARMUP"<<endl;
	for (int64_t i=0; i<LOOPS; ++i){
		do {
			p.allocateDataBlock(b, 100);
		} while (!b);
		((uint32_t*)(*b))[0]=i;
		p.commit(b);
	}
	cout<<"TIMINGS"<<endl;
	for (int64_t i=0; i<LOOPS; ++i){
		do {
		    p.allocateDataBlock(b, 100);
		} while (!b);
		((std::chrono::high_resolution_clock::time_point*)(*b))[0]=std::chrono::high_resolution_clock::now();
		p.commit(b);
	}
	cout<<"SPEED"<<endl;
	t[0]=std::chrono::high_resolution_clock::now();
	for (int64_t i=0; i<LOOPS; ++i){
		do {
		    p.allocateDataBlock(b, 100);
		} while (!b);
		p.commit(b);
	}
	t[1]=std::chrono::high_resolution_clock::now();
	p.done();
	double time=std::chrono::duration_cast<std::chrono::duration<double> >(t[1]-t[0]).count();
	cout<<"Time: "<<time<<", OPps: "<<LOOPS/time<<endl;
}

void testConsumerTask(){
	int clientid;
	std::chrono::high_resolution_clock::time_point ts;
	DataBlock b;
	Consumer c("test");
	int64_t i=-LOOPS;
	clientid=cid++;
	std::vector<double>& time=cltime[clientid];
	time.reserve(LOOPS);
	for (int64_t i=0; i<LOOPS; ++i){
		time[i]=0.0;
	}
	c.attach();
	cout<<"Consumer "<<clientid<<endl;
	c.init();
	while(1){
		c.getDataBlock(b);
		if (b.isStop())
			break;
		if (!b)
			continue;
		ts=std::chrono::high_resolution_clock::now();
		if (i<0){
			if (((uint32_t*)(*b))[0]!=i+LOOPS)
				cout<<"MISMATCH "<<((uint32_t*)(*b))[0]<<" "<<i+LOOPS<<endl;
		}
		else if (i>=0 && i<LOOPS){
			time[i]=std::chrono::duration_cast<std::chrono::duration<double> >(ts-((std::chrono::high_resolution_clock::time_point*)(*b))[0]).count();
		}
		c.commit(b);
		++i;
	}
	c.done();
}