-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
209 lines (176 loc) · 5.2 KB
/
main.cpp
File metadata and controls
209 lines (176 loc) · 5.2 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#include <iostream>
#include <vector>
#include <string>
#include <random>
#include <inttypes.h>
#include <boost/asio.hpp>
#include <boost/date_time/posix_time/ptime.hpp>
using namespace std;
struct Connection {
boost::asio::io_service& io_;
boost::asio::ip::tcp::endpoint ep_;
boost::asio::ip::tcp::socket socket_;
Connection(boost::asio::io_service& io, boost::asio::ip::tcp::endpoint ep)
: io_(io)
, ep_(ep)
, socket_(io_)
{
}
//! Connect synchronously
void connect() {
socket_.connect(ep_);
}
template<class DataSource>
void send(DataSource& source) {
socket_.send(source.get_buffers());
}
};
//! Generate time-series from random walk
struct RandomWalk {
std::random_device randdev_;
std::mt19937 generator_;
std::normal_distribution<double> distribution_;
std::vector<double> values;
RandomWalk(size_t N, double start, double mean, double stddev)
: generator_(randdev_())
, distribution_(mean, stddev)
, values(N, start)
{
}
double next(size_t ix) {
values.at(ix) += distribution_(generator_);
return values.at(ix);
}
};
struct TimestampGenerator {
std::uint64_t current_;
std::uint64_t step_;
TimestampGenerator() = default;
TimestampGenerator(TimestampGenerator const&) = default;
TimestampGenerator& operator = (TimestampGenerator const&) = default;
TimestampGenerator(uint64_t origin, uint64_t step)
: current_(origin)
, step_(step)
{
}
uint64_t next() {
current_ += step_;
return current_;
}
};
//! Protocol data unit that contains many values
struct PDU {
std::string const& series;
uint64_t timestamp;
std::vector<double> const& values;
};
struct RowGenerator {
std::string series; //< This should be a compound series name - "cpu|mem|iops tag=val"
TimestampGenerator tsgen;
RandomWalk xsgen;
RowGenerator(std::string sname, TimestampGenerator const& ts, size_t N, double start, double mean, double stddev)
: series(sname)
, tsgen(ts)
, xsgen(N, start, mean, stddev)
{
}
void step() {
for (auto ix = 0u; ix < xsgen.values.size(); ix++) {
xsgen.next(ix);
}
}
PDU next() {
step();
return {
series,
tsgen.next(),
xsgen.values,
};
}
};
struct ProtocolStream {
enum {
BUFFER_SIZE = 24*1024,
};
RowGenerator gen;
std::vector<char> buffer;
int pos;
ProtocolStream(std::string sname, TimestampGenerator const& ts, size_t N, double start, double mean, double stddev)
: gen(sname, ts, N, start, mean, stddev)
, pos(0)
{
buffer.resize(BUFFER_SIZE);
}
bool add_one(std::vector<boost::asio::const_buffer>* list) {
PDU pdu = gen.next();
// Static part
size_t original_size = list->size();
list->push_back(boost::asio::const_buffer("+", 1));
list->push_back(boost::asio::const_buffer(pdu.series.data(), pdu.series.size()));
list->push_back(boost::asio::const_buffer("\r\n", 2));
// Dynamic part
int sz = static_cast<int>(buffer.size()) - pos;
char* ptr = buffer.data() + pos;
const char* pbegin = ptr;
// Timestamp
int res = snprintf(ptr, static_cast<size_t>(sz), "+%" PRIu64 "\r\n", pdu.timestamp);
if (res < 0 || res > sz) {
list->resize(original_size);
return false;
}
sz -= res;
ptr += res;
pos += res;
// Num values
res = snprintf(ptr, static_cast<size_t>(sz), "*%d\r\n", static_cast<int>(pdu.values.size()));
if (res < 0 || res > sz) {
list->resize(original_size);
return false;
}
sz -= res;
ptr += res;
pos += res;
// Values
for (auto val: pdu.values) {
res = snprintf(ptr, static_cast<size_t>(sz), "+%.17g\r\n", val);
if (res < 0 || res > sz) {
list->resize(original_size);
return false;
}
sz -= res;
ptr += res;
pos += res;
}
size_t size = static_cast<size_t>(ptr - pbegin);
list->push_back(boost::asio::const_buffer(pbegin, size));
return true;
}
void reset() {
pos = 0;
}
};
int main(int argc, char *argv[])
{
struct S {
ProtocolStream stream;
S()
: stream("foo|bar tag=value", TimestampGenerator(10000000000, 100000), 2, 10.0, 0.0, 0.001)
{
}
std::vector<boost::asio::const_buffer> get_buffers() {
std::vector<boost::asio::const_buffer> buffers;
stream.reset();
while(stream.add_one(&buffers));
return buffers;
}
};
boost::asio::io_service io;
boost::asio::ip::tcp::endpoint ep(boost::asio::ip::address::from_string("127.0.0.1"), 8282);
Connection connection(io, ep);
connection.connect();
S s;
for (int i = 0; i < 1000000; i++) {
connection.send(s);
}
return 0;
}