-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmem_tests.cpp
More file actions
146 lines (117 loc) · 4.33 KB
/
mem_tests.cpp
File metadata and controls
146 lines (117 loc) · 4.33 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
138
139
140
141
142
143
144
145
146
#include <sys/types.h>
#include <sys/socket.h>
#include <iostream>
#include <boost/pool/pool.hpp>
#include <boost/pool/object_pool.hpp>
#include <boost/chrono.hpp>
#include "e2sarHeaders.hpp"
using namespace e2sar;
inline u_int64_t getMicros()
{
auto nowT = boost::chrono::system_clock::now();
auto nowUsec = boost::chrono::duration_cast<boost::chrono::microseconds>(nowT.time_since_epoch()).count();
return static_cast<u_int64_t>(nowUsec);
}
const size_t eventSize = 1000000;
const size_t maxPldLen = 8192;
const size_t numBuffers = (eventSize + maxPldLen - 1)/ maxPldLen;
// pool of LB+RE headers for sending
//boost::object_pool<LBREHdr> lbreHdrPool;
boost::pool<> lbreHdrPool{sizeof(LBREHdr)};
// pool of iovec items (we grab two at a time)
boost::pool<> iovecPool{sizeof(struct iovec)*2};
LBREHdr* hdrs[numBuffers];
struct iovec* iovecs[numBuffers];
void usePools()
{
for(size_t i = 0; i < numBuffers; i++)
{
hdrs[i] = static_cast<LBREHdr*>(lbreHdrPool.malloc());
iovecs[i] = static_cast<struct iovec*>(iovecPool.malloc());
}
for(size_t i = 0; i < numBuffers; i++)
{
lbreHdrPool.free(hdrs[i]);
iovecPool.free(iovecs[i]);
}
}
void useMallocs()
{
for(size_t i = 0; i < numBuffers; i++)
{
void *space = malloc(sizeof(LBREHdr));
hdrs[i] = new (space) LBREHdr();
iovecs[i] = static_cast<struct iovec*>(calloc(2, sizeof(struct iovec)));
}
for(size_t i = 0; i < numBuffers; i++)
{
free(hdrs[i]);
free(iovecs[i]);
}
}
void useNew()
{
for(size_t i = 0; i < numBuffers; i++)
{
hdrs[i] = new LBREHdr();
iovecs[i] = new struct iovec[2];
}
for(size_t i = 0; i < numBuffers; i++)
{
delete hdrs[i];
delete[] iovecs[i];
}
}
void doIters(void (*func)(), size_t iters)
{
for(size_t i = 0; i < iters; i++)
{
func();
}
}
int main(int argc, char **argv)
{
size_t numIters = 10000;
u_int64_t start, end;
// allocation test
void *hdrspace = malloc(sizeof(LBREHdr));
memset(hdrspace, 0, sizeof(LBREHdr));
// placement-new to construct the headers
auto hdr = new (hdrspace) LBREHdr(lbhdrVersion3);
u_int8_t* hdrbuf = reinterpret_cast<u_int8_t*>(hdr);
std::cout << "LB Header Version2 Check: " << hdr->lbu.lb2.check_version() << std::endl;
std::cout << "LB Header Version3 Check: " << hdr->lbu.lb3.check_version() << std::endl;
std::cout << "LB Header Preamble: " << reinterpret_cast<char*>(hdrbuf)[0] << reinterpret_cast<char*>(hdrbuf)[1] << std::endl;
std::cout << "LB Header Version: " << static_cast<int>(hdr->lbu.lb2.get_version()) << std::endl;
std::cout << "RE Header Version: " << static_cast<int>(hdr->re.get_HeaderVersion()) << std::endl;
hdr->lbu.lb3.set(1, 2, 3);
hdr->re.set(4, 5, 6, 7);
LBREHdr *newhdr{nullptr};
newhdr = reinterpret_cast<LBREHdr*>(hdrbuf);
std::cout << "Decoded LB Header Preamble: " << reinterpret_cast<char*>(newhdr)[0] << reinterpret_cast<char*>(newhdr)[1] << std::endl;
std::cout << "Decoded LB Header Version: " << static_cast<int>(newhdr->lbu.lb2.get_version()) << std::endl;
std::cout << "Decoded LB Fields: " <<
static_cast<int>(newhdr->lbu.lb3.get_slotSelect()) << " " <<
static_cast<int>(newhdr->lbu.lb3.get_portSelect()) << " " <<
newhdr->lbu.lb3.get_tick() << std::endl;
REHdr *rehdr{nullptr};
rehdr = reinterpret_cast<REHdr*>(hdrbuf + sizeof(LBHdrU));
std::cout << "Decoded RE Header Version: " << static_cast<int>(rehdr->get_HeaderVersion()) << std::endl;
std::cout << "Decoded RE Header Fields: " <<
static_cast<int>(rehdr->get_dataId()) << " " <<
static_cast<int>(rehdr->get_bufferOffset()) << " " <<
static_cast<int>(rehdr->get_bufferLength()) << " " <<
static_cast<int>(rehdr->get_eventNum()) << std::endl;
start = getMicros();
doIters(useNew, numIters);
end = getMicros();
std::cout << "New took " << end - start << " microseconds" << std::endl;
start = getMicros();
doIters(useMallocs, numIters);
end = getMicros();
std::cout << "Mallocs took " << end - start << " microseconds" << std::endl;
start = getMicros();
doIters(usePools, numIters);
end = getMicros();
std::cout << "Pools took " << end - start << " microseconds" << std::endl;
}