(ws cobra subscriber) use a Json::StreamWriter to write to std::cout, and save one std::string allocation for each message printed

This commit is contained in:
Benjamin Sergeant 2020-03-29 15:24:46 -07:00
parent 40c619c1ec
commit cfa5718e40
3 changed files with 23 additions and 8 deletions

View File

@ -1,6 +1,10 @@
# Changelog
All changes to this project will be documented in this file.
## [9.1.6] - 2020-03-29
(ws cobra subscriber) use a Json::StreamWriter to write to std::cout, and save one std::string allocation for each message printed
## [9.1.5] - 2020-03-29
(docker) trim down docker image (300M -> 12M) / binary built without symbol and size optimization, and source code not copied over

View File

@ -6,4 +6,4 @@
#pragma once
#define IX_WEBSOCKET_VERSION "9.1.5"
#define IX_WEBSOCKET_VERSION "9.1.6"

View File

@ -14,8 +14,19 @@
namespace ix
{
using StreamWriterPtr = std::unique_ptr<Json::StreamWriter>;
StreamWriterPtr makeStreamWriter()
{
Json::StreamWriterBuilder builder;
builder["commentStyle"] = "None";
builder["indentation"] = ""; // will make the JSON object compact
std::unique_ptr<Json::StreamWriter> jsonWriter(builder.newStreamWriter());
return jsonWriter;
}
void writeToStdout(bool fluentd,
Json::FastWriter& jsonWriter,
const StreamWriterPtr& jsonWriter,
const Json::Value& msg,
const std::string& position)
{
@ -29,12 +40,15 @@ namespace ix
msgWithPosition["position"] = position;
enveloppe["message"] = msgWithPosition;
std::cout << jsonWriter.write(enveloppe);
jsonWriter->write(enveloppe, &std::cout);
std::cout << std::endl; // add lf and flush
}
else
{
enveloppe = msg;
std::cout << position << " " << jsonWriter.write(enveloppe);
std::cout << position << " ";
jsonWriter->write(enveloppe, &std::cout);
std::cout << std::endl;
}
}
@ -49,13 +63,10 @@ namespace ix
conn.configure(config);
conn.connect();
Json::FastWriter jsonWriter;
// Display incoming messages
std::atomic<int> msgPerSeconds(0);
std::atomic<int> msgCount(0);
std::atomic<bool> fatalCobraError(false);
auto jsonWriter = makeStreamWriter();
auto timer = [&msgPerSeconds, &msgCount, &fatalCobraError] {
while (!fatalCobraError)