test.cpp
3.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#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();
}