Skip to content

Commit

Permalink
[OMON-374] Use ostringstream in Stdout backend for atomic writes (#212)
Browse files Browse the repository at this point in the history
  • Loading branch information
awegrzyn authored Oct 9, 2020
1 parent e7d1e58 commit 9a7a258
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/Backends/StdOut.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ inline unsigned long StdOut::convertTimestamp(const std::chrono::time_point<std:
.count();
}

StdOut::StdOut(const std::string& prefix) : mStream(std::cout), mPrefix(prefix)
StdOut::StdOut(const std::string& prefix) : mStream(), mPrefix(prefix)
{
setVerbosisty(Verbosity::Debug);
MonLogger::Get() << "StdOut backend initialized" << MonLogger::End();
Expand Down Expand Up @@ -83,6 +83,7 @@ void StdOut::send(const Metric& metric)
mStream << ',' << tags::TAG_KEY[key] << "=" << tags::GetValue(value);
}
mStream << '\n';
std::cout << mStream.str();
}

} // namespace backends
Expand Down
3 changes: 2 additions & 1 deletion src/Backends/StdOut.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#include "Monitoring/Backend.h"
#include <string>
#include <sstream>

namespace o2
{
Expand Down Expand Up @@ -53,7 +54,7 @@ class StdOut final : public Backend

private:
/// Metric stream
std::ostream& mStream;
std::ostringstream mStream;

/// Converts timestamp to unsigned long (miliseconds from epoch)
/// \param timestamp timestamp in std::chrono::time_point format
Expand Down
40 changes: 40 additions & 0 deletions test/testThreads.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include "../include/Monitoring/MonitoringFactory.h"

#define BOOST_TEST_MODULE Test Monitoring Threads
#define BOOST_TEST_DYN_LINK
#include <boost/test/unit_test.hpp>
#include <thread>
#include <array>

namespace o2
{
namespace monitoring
{
namespace Test
{

using namespace o2::monitoring;

BOOST_AUTO_TEST_CASE(retrieveOtherParams)
{
std::array<std::thread, 4> threads;
for (auto& thread : threads) {
thread = std::thread([] {
for (int i = 0; i < 20; i++) {
auto monitoring = MonitoringFactory::Get("stdout://");
monitoring->addGlobalTag("name", "Readout");
monitoring->addGlobalTag(tags::Key::Name, tags::Value::Readout);
monitoring->send({1, "myCrazyMetricI"});
monitoring->send({13.33, "myCrazyMetricS"});
}
});
}

for (auto& thread : threads) {
thread.join();
}
}

} // namespace Test
} // namespace monitoring
} // namespace o2

0 comments on commit 9a7a258

Please sign in to comment.