-
Notifications
You must be signed in to change notification settings - Fork 0
/
configuration.cpp
328 lines (281 loc) · 11.1 KB
/
configuration.cpp
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
//
// Created by Rakesh on 12/01/2022.
//
#include <catch2/catch_test_macros.hpp>
#include <chrono>
#include <filesystem>
#include <fstream>
#include <sstream>
#include <vector>
#include "../../src/common/model/configuration.hpp"
namespace spt::configdb::unittest::pconf
{
std::string file;
}
using spt::configdb::model::Configuration;
using namespace std::string_literals;
using namespace std::string_view_literals;
namespace pc = spt::configdb::unittest::pconf;
SCENARIO( "Configuration test", "config" )
{
GIVEN( "A JSON representation of configuration" )
{
const auto json = R"({
"encryption": {
"salt": "asalt",
"key": "akey",
"iv": "aniv",
"secret": "a secret",
"rounds": 1000
},
"logging": {
"level": "debug",
"dir": "/tmp/",
"console": false
},
"ssl": {
"caCertificate": "/opt/spt/certs/ca.crt",
"serverCertificate": "/opt/spt/certs/server.crt",
"serverKey": "/opt/spt/certs/server.key",
"enable": false
},
"services": {
"http": "6006",
"tcp": 2022
},
"storage": {
"dbpath": "/tmp/config-db",
"blockCacheSizeMb": 16
},
"threads": 8,
"enableCache": true
})"sv;
WHEN( "Writing the config to a file" )
{
auto now = std::chrono::high_resolution_clock::now();
auto ns = std::chrono::duration_cast<std::chrono::nanoseconds>( now.time_since_epoch() ).count();
std::ostringstream ss;
ss << "/tmp/" << ns << ".json";
pc::file = ss.str();
std::ofstream f( pc::file );
f.write( json.data(), json.size() );
f.close();
auto p = std::filesystem::path{ pc::file };
REQUIRE( std::filesystem::file_size( p ) > 0 );
}
AND_WHEN( "Creating configuration from file" )
{
Configuration::loadFromFile( pc::file );
auto& cfg = Configuration::instance();
REQUIRE( cfg.encryption.salt == "asalt"s );
REQUIRE( cfg.encryption.key == "akey"s );
REQUIRE( cfg.encryption.iv == "aniv"s );
REQUIRE( cfg.encryption.secret == "a secret"s );
REQUIRE( cfg.encryption.rounds == 1000 );
REQUIRE( cfg.logging.level == "debug"s );
REQUIRE( cfg.logging.dir == "/tmp/"s );
REQUIRE_FALSE( cfg.logging.console );
REQUIRE( cfg.ssl.caCertificate == "/opt/spt/certs/ca.crt"s );
REQUIRE( cfg.ssl.certificate == "/opt/spt/certs/server.crt"s );
REQUIRE( cfg.ssl.key == "/opt/spt/certs/server.key"s );
REQUIRE_FALSE( cfg.ssl.enable );
REQUIRE( cfg.services.http == "6006"s );
REQUIRE( cfg.services.tcp == 2022 );
REQUIRE( cfg.storage.dbpath == "/tmp/config-db"s );
REQUIRE( cfg.storage.blockCacheSizeMb == 16 );
REQUIRE( cfg.threads == 8 );
REQUIRE( cfg.enableCache );
}
AND_WHEN( "Removing the file" )
{
auto p = std::filesystem::path{ pc::file };
REQUIRE( std::filesystem::remove( p ) );
Configuration::reset();
}
}
}
/*
SCENARIO( "Serialisation test" )
{
GIVEN( "A set of strings" )
{
std::set<std::string, std::less<>> names;
for ( auto i = 0; i < 10000; ++i )
{
std::string name;
name.reserve( 12 );
name.append( "name" ).append( std::to_string( i ) );
names.insert( name );
}
WHEN( "Comparing flatbuffers to boost serialization" )
{
const auto tofbuf = [&names]
{
auto st = std::chrono::high_resolution_clock::now();
auto fb = flatbuffers::FlatBufferBuilder{ 128 };
auto children = std::vector<flatbuffers::Offset<flatbuffers::String>>{};
children.reserve( names.size() );
for ( auto&& n : names ) children.push_back( fb.CreateString( n ) );
auto nc = fb.CreateVector( children );
auto n = spt::configdb::model::CreateNode( fb, nc );
fb.Finish( n );
std::string buf{ reinterpret_cast<const char*>( fb.GetBufferPointer() ), fb.GetSize() };
std::ostringstream ss;
ss << buf;
ss.str();
auto et = std::chrono::high_resolution_clock::now();
auto delta = std::chrono::duration_cast<std::chrono::nanoseconds>( et - st );
return std::tuple{ ss.str(), delta.count() };
};
const auto fromfbuf = [&names]( std::string_view str )
{
auto st = std::chrono::high_resolution_clock::now();
const auto d = reinterpret_cast<const uint8_t*>( str.data() );
auto response = spt::configdb::model::GetNode( d );
auto vec = std::vector<std::string>{};
vec.reserve( response->children()->size() + 1 );
for ( auto&& item : *response->children() ) vec.push_back( item->str() );
REQUIRE( vec.size() == names.size() );
auto et = std::chrono::high_resolution_clock::now();
auto delta = std::chrono::duration_cast<std::chrono::nanoseconds>( et - st );
return delta.count();
};
const auto tocppser = [&names]()
{
auto st = std::chrono::high_resolution_clock::now();
auto buf = buffer{};
store( buf, names );
auto str = std::string{ reinterpret_cast<const char*>( buf.data() ), buf.size() };
auto et = std::chrono::high_resolution_clock::now();
auto delta = std::chrono::duration_cast<std::chrono::nanoseconds>( et - st );
return std::tuple{ str, delta.count() };
};
const auto fromcppser = [&names]( std::string_view str )
{
auto st = std::chrono::high_resolution_clock::now();
auto par = parser{};
par.init( reinterpret_cast<const uint8_t*>( str.data() ), str.size() );
std::set<std::string> n;
parse( par, n );
REQUIRE( n.size() == names.size() );
auto et = std::chrono::high_resolution_clock::now();
auto delta = std::chrono::duration_cast<std::chrono::nanoseconds>( et - st );
return delta.count();
};
std::vector<uint64_t> tofbufs;
tofbufs.reserve( 100 );
std::vector<uint64_t> fromfbufs;
fromfbufs.reserve( 100 );
std::vector<uint64_t> tocppsers;
tocppsers.reserve( 100 );
std::vector<uint64_t> fromcppsers;
fromcppsers.reserve( 100 );
for ( auto i = 0; i < 100; ++i )
{
const auto [str,tfbuft] = tofbuf();
tofbufs.push_back( tfbuft );
const auto ffbuft = fromfbuf( str );
fromfbufs.push_back( ffbuft );
const auto [bstr,tcppser] = tocppser();
tocppsers.push_back( tcppser );
const auto fcppser = fromcppser( bstr );
fromcppsers.push_back( fcppser );
}
std::cout << "Average time to produce Flatbuffer from set " << std::reduce( tofbufs.begin(), tofbufs.end() ) / tofbufs.size() << std::endl;
std::cout << "Average time to consume set from Flatbuffer " << std::reduce( fromfbufs.begin(), fromfbufs.end() ) / fromfbufs.size() << std::endl;
std::cout << "Average time to produce cppser serialisation from set " << std::reduce( fromcppsers.begin(), fromcppsers.end() ) / fromcppsers.size() << std::endl;
std::cout << "Average time to consume set from cppser serialisation " << std::reduce( tocppsers.begin(), tocppsers.end() ) / tocppsers.size() << std::endl;
}
}
GIVEN( "A vector of strings" )
{
std::vector<std::string> names;
for ( auto i = 0; i < 10000; ++i )
{
std::string name;
name.reserve( 12 );
name.append( "name" ).append( std::to_string( i ) );
names.push_back( name );
}
WHEN( "Comparing flatbuffers to boost serialization" )
{
const auto tofbuf = [&names]
{
auto st = std::chrono::high_resolution_clock::now();
auto fb = flatbuffers::FlatBufferBuilder{ 128 };
auto children = std::vector<flatbuffers::Offset<flatbuffers::String>>{};
children.reserve( names.size() );
for ( auto&& n : names ) children.push_back( fb.CreateString( n ) );
auto nc = fb.CreateVector( children );
auto n = spt::configdb::model::CreateNode( fb, nc );
fb.Finish( n );
std::string buf{ reinterpret_cast<const char*>( fb.GetBufferPointer() ), fb.GetSize() };
std::ostringstream ss;
ss << buf;
ss.str();
auto et = std::chrono::high_resolution_clock::now();
auto delta = std::chrono::duration_cast<std::chrono::nanoseconds>( et - st );
return std::tuple{ ss.str(), delta.count() };
};
const auto fromfbuf = [&names]( std::string_view str )
{
auto st = std::chrono::high_resolution_clock::now();
const auto d = reinterpret_cast<const uint8_t*>( str.data() );
auto response = spt::configdb::model::GetNode( d );
auto vec = std::vector<std::string>{};
vec.reserve( response->children()->size() + 1 );
for ( auto&& item : *response->children() ) vec.push_back( item->str() );
REQUIRE( vec.size() == names.size() );
auto et = std::chrono::high_resolution_clock::now();
auto delta = std::chrono::duration_cast<std::chrono::nanoseconds>( et - st );
return delta.count();
};
const auto tocppser = [&names]()
{
auto st = std::chrono::high_resolution_clock::now();
auto buf = buffer{};
store( buf, names );
auto str = std::string{ reinterpret_cast<const char*>( buf.data() ), buf.size() };
auto et = std::chrono::high_resolution_clock::now();
auto delta = std::chrono::duration_cast<std::chrono::nanoseconds>( et - st );
return std::tuple{ str, delta.count() };
};
const auto fromcppser = [&names]( std::string_view str )
{
auto st = std::chrono::high_resolution_clock::now();
auto par = parser{};
par.init( reinterpret_cast<const uint8_t*>( str.data() ), str.size() );
std::vector<std::string> n;
parse( par, n );
REQUIRE( n.size() == names.size() );
auto et = std::chrono::high_resolution_clock::now();
auto delta = std::chrono::duration_cast<std::chrono::nanoseconds>( et - st );
return delta.count();
};
std::vector<uint64_t> tofbufs;
tofbufs.reserve( 100 );
std::vector<uint64_t> fromfbufs;
fromfbufs.reserve( 100 );
std::vector<uint64_t> tocppsers;
tocppsers.reserve( 100 );
std::vector<uint64_t> fromcppsers;
fromcppsers.reserve( 100 );
for ( auto i = 0; i < 100; ++i )
{
const auto [str,tfbuft] = tofbuf();
tofbufs.push_back( tfbuft );
const auto ffbuft = fromfbuf( str );
fromfbufs.push_back( ffbuft );
const auto [bstr,tcppser] = tocppser();
tocppsers.push_back( tcppser );
const auto fcppser = fromcppser( bstr );
fromcppsers.push_back( fcppser );
}
std::cout << "Average time to produce Flatbuffer from vector " << std::reduce( tofbufs.begin(), tofbufs.end() ) / tofbufs.size() << std::endl;
std::cout << "Average time to consume vector from Flatbuffer " << std::reduce( fromfbufs.begin(), fromfbufs.end() ) / fromfbufs.size() << std::endl;
std::cout << "Average time to produce cppser serialisation from vetor " << std::reduce( fromcppsers.begin(), fromcppsers.end() ) / fromcppsers.size() << std::endl;
std::cout << "Average time to consume vector from cppser serialisation " << std::reduce( tocppsers.begin(), tocppsers.end() ) / tocppsers.size() << std::endl;
}
}
}
*/