<commit_before>#include <gtest/gtest.h>
#include <iostream>
#include <atomic>
#include <vector>
#include <memory>
#include <thread>
#include <chrono>

#include "testing_helpers.h"
#include "g2logmessage.hpp"
#include "g2logworker.hpp"

using namespace testing_helpers;
using namespace std;
TEST(Sink, OneSink) {
using namespace g2;
   AtomicBoolPtr flag = make_shared < atomic<bool >> (false);
   AtomicIntPtr count = make_shared < atomic<int >> (0);
   {
      auto worker = g2::LogWorker::createWithNoSink();
      auto handle = worker->addSink(std2::make_unique<ScopedSetTrue>(flag, count), &ScopedSetTrue::ReceiveMsg);
      EXPECT_FALSE(flag->load());
      EXPECT_TRUE(0 == count->load());
      //worker->save("this message should trigger an atomic increment at the sink");
      LogMessagePtr message{std2::make_unique<LogMessage>("test", 0, "test", DEBUG)};
      message.get()->stream() << "this message should trigger an atomic increment at the sink";
      worker->save(message);
   }
   EXPECT_TRUE(flag->load());
   EXPECT_TRUE(1 == count->load());
}


namespace {
   typedef std::shared_ptr<std::atomic<bool >> AtomicBoolPtr;
   typedef std::shared_ptr<std::atomic<int >> AtomicIntPtr;
   typedef vector<AtomicBoolPtr> BoolList;
   typedef vector<AtomicIntPtr> IntVector;
}

TEST(ConceptSink, OneHundredSinks) {
   using namespace g2;
   BoolList flags;
   IntVector counts;

   size_t NumberOfItems = 2;
   for (size_t index = 0; index < NumberOfItems; ++index) {
      flags.push_back(make_shared < atomic<bool >> (false));
      counts.push_back(make_shared < atomic<int >> (0));
   }

   {
      RestoreFileLogger logger{"/tmp"};
      g2::LogWorker* worker = logger._scope->get(); //g2LogWorker::createWithNoSink();
      size_t index = 0;
      for (auto& flag : flags) {
         auto& count = counts[index++];
         // ignore the handle
         worker->addSink(std2::make_unique<ScopedSetTrue>(flag, count), &ScopedSetTrue::ReceiveMsg);
      }
      LOG(DEBUG) << "start message";
      LogMessagePtr message1{std2::make_unique<LogMessage>("test", 0, "test", DEBUG)};
      LogMessagePtr message2{std2::make_unique<LogMessage>("test", 0, "test", DEBUG)};
      auto& stream1 = message1.get()->stream();
      stream1 << "Hello to 100 receivers :)";
      worker->save(message1);
      
      auto& stream2 = message1.get()->stream();
      stream2 << "Hello to 100 receivers :)";
      worker->save(message2);<commit_msg>All unit test working. Next step is to increase performance even more<commit_after>#include <gtest/gtest.h>
#include <iostream>
#include <atomic>
#include <vector>
#include <memory>
#include <thread>
#include <chrono>

#include "testing_helpers.h"
#include "g2logmessage.hpp"
#include "g2logworker.hpp"

using namespace testing_helpers;
using namespace std;
TEST(Sink, OneSink) {
using namespace g2;
   AtomicBoolPtr flag = make_shared < atomic<bool >> (false);
   AtomicIntPtr count = make_shared < atomic<int >> (0);
   {
      auto worker = g2::LogWorker::createWithNoSink();
      auto handle = worker->addSink(std2::make_unique<ScopedSetTrue>(flag, count), &ScopedSetTrue::ReceiveMsg);
      EXPECT_FALSE(flag->load());
      EXPECT_TRUE(0 == count->load());
      //worker->save("this message should trigger an atomic increment at the sink");
      LogMessagePtr message{std2::make_unique<LogMessage>("test", 0, "test", DEBUG)};
      message.get()->stream() << "this message should trigger an atomic increment at the sink";
      worker->save(message);
   }
   EXPECT_TRUE(flag->load());
   EXPECT_TRUE(1 == count->load());
}


namespace {
   typedef std::shared_ptr<std::atomic<bool >> AtomicBoolPtr;
   typedef std::shared_ptr<std::atomic<int >> AtomicIntPtr;
   typedef vector<AtomicBoolPtr> BoolList;
   typedef vector<AtomicIntPtr> IntVector;
}

TEST(ConceptSink, OneHundredSinks) {
   using namespace g2;
   BoolList flags;
   IntVector counts;

   size_t NumberOfItems = 100;
   for (size_t index = 0; index < NumberOfItems; ++index) {
      flags.push_back(make_shared < atomic<bool >> (false));
      counts.push_back(make_shared < atomic<int >> (0));
   }

   {
      RestoreFileLogger logger{"/tmp"};
      g2::LogWorker* worker = logger._scope->get(); //g2LogWorker::createWithNoSink();
      size_t index = 0;
      for (auto& flag : flags) {
         auto& count = counts[index++];
         // ignore the handle
         worker->addSink(std2::make_unique<ScopedSetTrue>(flag, count), &ScopedSetTrue::ReceiveMsg);
      }
      LOG(DEBUG) << "start message";
      LogMessagePtr message1{std2::make_unique<LogMessage>("test", 0, "test", DEBUG)};
      LogMessagePtr message2{std2::make_unique<LogMessage>("test", 0, "test", DEBUG)};
      auto& stream1 = message1.get()->stream();
      stream1 << "Hello to 100 receivers :)";
      worker->save(message1);
      
      auto& stream2 = message2.get()->stream();
      stream2 << "Hello to 100 receivers :)";
      worker->save(message2);
{'fraction_non_alphanumeric': 0.1487086359967716, 'fraction_numerical': 0.015738498789346248, 'mean_word_length': 4.0530071355759425, 'pattern_counts': {'":': 0, '<': 73, '<?xml version=': 0, '>': 83, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '899832', 'n_tokens_mistral': 1587, 'n_tokens_neox': 1570, 'n_words': 356}