|
| 1 | +#include <iostream> |
| 2 | +#include <vector> |
| 3 | +#include <cstdint> |
| 4 | +#include <string> |
| 5 | +#include <utility> |
| 6 | +#include <streambuf> |
| 7 | +#include <istream> |
| 8 | +#include <boost/timer/timer.hpp> |
| 9 | + |
| 10 | +#include <bitsery/bitsery.h> |
| 11 | +#include <bitsery/adapter/buffer.h> |
| 12 | +#include <bitsery/traits/vector.h> |
| 13 | +#include <bitsery/traits/string.h> |
| 14 | + |
| 15 | +#include "../binary-buffer/BinaryBuffer.hpp" |
| 16 | + |
| 17 | +#include <boost/archive/binary_oarchive.hpp> |
| 18 | +#include <boost/archive/binary_iarchive.hpp> |
| 19 | +#include <boost/interprocess/streams/bufferstream.hpp> |
| 20 | +#include <boost/serialization/vector.hpp> |
| 21 | +#include <boost/serialization/string.hpp> |
| 22 | + |
| 23 | +std::pair<std::vector<uint8_t>, size_t> bitsery_serialize() |
| 24 | +{ |
| 25 | + using OutputAdapter = bitsery::OutputBufferAdapter<std::vector<uint8_t>>; |
| 26 | + |
| 27 | + std::vector<uint8_t> buffer; |
| 28 | + bitsery::Serializer<OutputAdapter> ser(buffer); |
| 29 | + |
| 30 | + for (int i = 0; i < 1000000; i++) |
| 31 | + { |
| 32 | + ser.boolValue(true); |
| 33 | + ser.value1b(static_cast<int8_t>(-3)); |
| 34 | + ser.value1b(static_cast<uint8_t>(3)); |
| 35 | + ser.value2b(static_cast<int16_t>(-3)); |
| 36 | + ser.value2b(static_cast<uint16_t>(3)); |
| 37 | + ser.value4b(-3); |
| 38 | + ser.value4b(static_cast<uint32_t>(3)); |
| 39 | + ser.value8b(static_cast<int64_t>(-3)); |
| 40 | + ser.value8b(static_cast<uint64_t>(3)); |
| 41 | + auto res = std::string("foo"); |
| 42 | + ser.text1b(res, res.size()); |
| 43 | + auto res2 = std::vector<uint8_t>{ 0x00, 0x01, 0x02, 0x03, 0x04 }; |
| 44 | + ser.container1b(res2, res2.size()); |
| 45 | + } |
| 46 | + |
| 47 | + return { buffer, ser.adapter().writtenBytesCount() }; |
| 48 | +} |
| 49 | + |
| 50 | +void bitsery_deserialize(std::vector<uint8_t> buffer, size_t written_size) |
| 51 | +{ |
| 52 | + using InputAdapter = bitsery::InputBufferAdapter<std::vector<uint8_t>>; |
| 53 | + |
| 54 | + bitsery::Deserializer<InputAdapter> des(buffer.begin(), written_size); |
| 55 | + |
| 56 | + for (int i = 0; i < 1000000; i++) |
| 57 | + { |
| 58 | + auto res = false; |
| 59 | + des.boolValue(res); |
| 60 | + auto res2 = static_cast<int8_t>(-3); |
| 61 | + des.value1b(res2); |
| 62 | + auto res3 = static_cast<uint8_t>(3); |
| 63 | + des.value1b(res3); |
| 64 | + auto res4 = static_cast<int16_t>(-3); |
| 65 | + des.value2b(res4); |
| 66 | + auto res5 = static_cast<uint16_t>(3); |
| 67 | + des.value2b(res5); |
| 68 | + auto res6 = -3; |
| 69 | + des.value4b(res6); |
| 70 | + auto res7 = static_cast<uint32_t>(3); |
| 71 | + des.value4b(res7); |
| 72 | + auto res8 = static_cast<int64_t>(-3); |
| 73 | + des.value8b(res8); |
| 74 | + auto res9 = static_cast<uint64_t>(3); |
| 75 | + des.value8b(res9); |
| 76 | + auto res10 = std::string("foo"); |
| 77 | + des.text1b(res10, res10.size()); |
| 78 | + auto res11 = std::vector<uint8_t>{ 0x00, 0x01, 0x02, 0x03, 0x04 }; |
| 79 | + des.container1b(res11, res11.size()); |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +std::vector<uint8_t> binary_buffer_serialize() |
| 84 | +{ |
| 85 | + auto ser = BinaryBuffer(); |
| 86 | + |
| 87 | + for (int i = 0; i < 1000000; i++) |
| 88 | + { |
| 89 | + ser.Write(true); |
| 90 | + ser.Write(static_cast<int8_t>(-3)); |
| 91 | + ser.Write(static_cast<uint8_t>(3)); |
| 92 | + ser.Write(static_cast<int16_t>(-3)); |
| 93 | + ser.Write(static_cast<uint16_t>(3)); |
| 94 | + ser.Write(-3); |
| 95 | + ser.Write(static_cast<uint32_t>(3)); |
| 96 | + ser.Write(static_cast<int64_t>(-3)); |
| 97 | + ser.Write(static_cast<uint64_t>(3)); |
| 98 | + auto res = std::string("foo"); |
| 99 | + ser.Write(res); |
| 100 | + auto res2 = std::vector<uint8_t>{ 0x00, 0x01, 0x02, 0x03, 0x04 }; |
| 101 | + ser.Write(res2); |
| 102 | + } |
| 103 | + |
| 104 | + return ser.GetBuffer(); |
| 105 | +} |
| 106 | + |
| 107 | +void binary_buffer_deserialize(std::vector<uint8_t> buffer) |
| 108 | +{ |
| 109 | + auto des = BinaryBuffer(buffer); |
| 110 | + |
| 111 | + for (int i = 0; i < 1000000; i++) |
| 112 | + { |
| 113 | + auto res = false; |
| 114 | + des.Read(res); |
| 115 | + auto res2 = static_cast<int8_t>(-3); |
| 116 | + des.Read(res2); |
| 117 | + auto res3 = static_cast<uint8_t>(3); |
| 118 | + des.Read(res3); |
| 119 | + auto res4 = static_cast<int16_t>(-3); |
| 120 | + des.Read(res4); |
| 121 | + auto res5 = static_cast<uint16_t>(3); |
| 122 | + des.Read(res5); |
| 123 | + auto res6 = -3; |
| 124 | + des.Read(res6); |
| 125 | + auto res7 = static_cast<uint32_t>(3); |
| 126 | + des.Read(res7); |
| 127 | + auto res8 = static_cast<int64_t>(-3); |
| 128 | + des.Read(res8); |
| 129 | + auto res9 = static_cast<uint64_t>(3); |
| 130 | + des.Read(res9); |
| 131 | + auto res10 = std::string("foo"); |
| 132 | + des.Read(res10); |
| 133 | + auto res11 = std::vector<uint8_t>{ 0x00, 0x01, 0x02, 0x03, 0x04 }; |
| 134 | + des.Read(res11); |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | +struct membuf : std::streambuf |
| 139 | +{ |
| 140 | + membuf(const char* base, size_t size) |
| 141 | + { |
| 142 | + auto p(const_cast<char*>(base)); |
| 143 | + this->setg(p, p, p + size); |
| 144 | + } |
| 145 | +}; |
| 146 | + |
| 147 | +struct imemstream : virtual membuf, std::istream |
| 148 | +{ |
| 149 | + imemstream(const char* base, size_t size) |
| 150 | + : membuf(base, size) |
| 151 | + , std::istream(static_cast<std::streambuf*>(this)) |
| 152 | + { |
| 153 | + } |
| 154 | +}; |
| 155 | + |
| 156 | +struct omemstream : virtual membuf, std::ostream |
| 157 | +{ |
| 158 | + omemstream(const char* base, size_t size) |
| 159 | + : membuf(base, size) |
| 160 | + , std::ostream(static_cast<std::streambuf*>(this)) |
| 161 | + { |
| 162 | + } |
| 163 | +}; |
| 164 | + |
| 165 | +std::vector<uint8_t> boost_serialize() |
| 166 | +{ |
| 167 | + std::vector<uint8_t> buffer; |
| 168 | + auto input_stream = imemstream(reinterpret_cast<const char*>(buffer.data()), buffer.size()); |
| 169 | + auto ser = boost::archive::binary_oarchive(input_stream); |
| 170 | + |
| 171 | + for (int i = 0; i < 1000000; i++) |
| 172 | + { |
| 173 | + ser << true; |
| 174 | + ser << static_cast<int8_t>(-3); |
| 175 | + ser << static_cast<uint8_t>(3); |
| 176 | + ser << static_cast<int16_t>(-3); |
| 177 | + ser << static_cast<uint16_t>(3); |
| 178 | + ser << -3; |
| 179 | + ser << static_cast<uint32_t>(3); |
| 180 | + ser << static_cast<int64_t>(-3); |
| 181 | + ser << static_cast<uint64_t>(3); |
| 182 | + auto res = std::string("foo"); |
| 183 | + ser << res; |
| 184 | + auto res2 = std::vector<uint8_t>{ 0x00, 0x01, 0x02, 0x03, 0x04 }; |
| 185 | + ser << res2; |
| 186 | + } |
| 187 | + |
| 188 | + return buffer; |
| 189 | +} |
| 190 | + |
| 191 | +void boost_deserialize(std::vector<uint8_t> buffer) |
| 192 | +{ |
| 193 | + auto output_stream = omemstream(reinterpret_cast<const char*>(buffer.data()), buffer.size()); |
| 194 | + auto des = boost::archive::binary_iarchive(output_stream); |
| 195 | + |
| 196 | + for (int i = 0; i < 1000000; i++) |
| 197 | + { |
| 198 | + auto res = false; |
| 199 | + des >> res; |
| 200 | + auto res2 = static_cast<int8_t>(-3); |
| 201 | + des >> res2; |
| 202 | + auto res3 = static_cast<uint8_t>(3); |
| 203 | + des >> res3; |
| 204 | + auto res4 = static_cast<int16_t>(-3); |
| 205 | + des >> res4; |
| 206 | + auto res5 = static_cast<uint16_t>(3); |
| 207 | + des >> res5; |
| 208 | + auto res6 = -3; |
| 209 | + des >> res6; |
| 210 | + auto res7 = static_cast<uint32_t>(3); |
| 211 | + des >> res7; |
| 212 | + auto res8 = static_cast<int64_t>(-3); |
| 213 | + des >> res8; |
| 214 | + auto res9 = static_cast<uint64_t>(3); |
| 215 | + des >> res9; |
| 216 | + auto res10 = std::string("foo"); |
| 217 | + des >> res10; |
| 218 | + auto res11 = std::vector<uint8_t>{ 0x00, 0x01, 0x02, 0x03, 0x04 }; |
| 219 | + des >> res11; |
| 220 | + } |
| 221 | +} |
| 222 | + |
| 223 | +int main() |
| 224 | +{ |
| 225 | + { |
| 226 | + boost::timer::cpu_timer timer; |
| 227 | + auto [buffer, written_size] = bitsery_serialize(); |
| 228 | + timer.stop(); |
| 229 | + std::cout << "bitsery size: " << buffer.size() << std::endl; |
| 230 | + std::cout << "bitsery serialize: " << timer.format(); |
| 231 | + timer = boost::timer::cpu_timer(); |
| 232 | + bitsery_deserialize(buffer, written_size); |
| 233 | + timer.stop(); |
| 234 | + std::cout << "bitsery deserialize: " << timer.format(); |
| 235 | + } |
| 236 | + |
| 237 | + { |
| 238 | + boost::timer::cpu_timer timer; |
| 239 | + auto buffer = binary_buffer_serialize(); |
| 240 | + timer.stop(); |
| 241 | + std::cout << "binary_buffer size: " << buffer.size() << std::endl; |
| 242 | + std::cout << "binary_buffer serialize: " << timer.format(); |
| 243 | + timer = boost::timer::cpu_timer(); |
| 244 | + binary_buffer_deserialize(buffer); |
| 245 | + timer.stop(); |
| 246 | + std::cout << "binary_buffer deserialize: " << timer.format(); |
| 247 | + } |
| 248 | + |
| 249 | + { |
| 250 | + boost::timer::cpu_timer timer; |
| 251 | + auto buffer = boost_serialize(); |
| 252 | + timer.stop(); |
| 253 | + std::cout << "boost size: " << buffer.size() << std::endl; |
| 254 | + std::cout << "boost serialize: " << timer.format(); |
| 255 | + timer = boost::timer::cpu_timer(); |
| 256 | + boost_deserialize(buffer); |
| 257 | + timer.stop(); |
| 258 | + std::cout << "boost deserialize: " << timer.format(); |
| 259 | + } |
| 260 | + |
| 261 | + return 0; |
| 262 | +} |
0 commit comments