HelloAurora/Tests/Public/10. Hello Logger/Main.cpp
2022-04-01 05:14:58 +01:00

162 lines
4.9 KiB
C++

/***
Copyright (C) 2022 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: Main.cpp
Date: 2022-2-18
Author: Reece
Note: Run the registry template in the ./Aurora/Runtime/Media directory
***/
#include <AuroraRuntime.hpp>
#include <gtest/gtest.h>
TEST(Echo, HelloReplacedGlobalLogger)
{
auto logger = AuLog::NewLoggerShared(AuList<AuSPtr<AuLog::IBasicSink>>{
AuLog::NewOSNamedEventDirectorySinkShared("Aurora Demo"),
AuLog::NewStdSinkShared()
});
ASSERT_TRUE(logger);
AuLog::SetGlobalLogger(logger);
AuLogInfo("Hello event log and stdout from the global AuLogInfo shorthand");
try
{
AU_THROW_STRING("Crinkled and delivered with many of seethes, hello event log");
}
catch (...)
{
}
AuLog::SetGlobalLogger({});
}
TEST(Echo, HelloEventDir)
{
auto logger = AuLog::NewLoggerShared(AuList<AuSPtr<AuLog::IBasicSink>>{AuLog::NewOSNamedEventDirectorySinkShared("Aurora Demo")});
ASSERT_TRUE(logger);
logger->LogInfo("hello 2+2={}", 2 + 2);
}
TEST(Echo, TextFile)
{
{
auto logger = AuLog::NewLoggerShared(AuList<AuSPtr<AuLog::IBasicSink>>{AuLog::NewFileSinkShared("~/TestLog.txt")});
ASSERT_TRUE(logger);
logger->LogInfo("hello text file 2+2={}", 2 + 2);
}
AuString log;
ASSERT_TRUE(AuIOFS::ReadString("~/TestLog.txt", log));
ASSERT_TRUE(AuStringContains(log, "hello text file"));
}
TEST(Echo, RingBuffer)
{
AuList<AuSPtr<AuLog::IBasicSink>> sinks;
auto rb = AuLog::NewRingLoggerShared(10'000);
auto cpy = rb;
ASSERT_TRUE(rb);
ASSERT_TRUE(AuListFromArgs(sinks, AuReference(rb)));
auto logger = AuLog::NewLoggerShared(sinks);
ASSERT_TRUE(logger);
for (int i = 0; i < 20'000; i++)
{
logger->LogInfo("hello text file 2+2*n={}", 2 + (2 * i));
}
auto list = cpy->Export();
ASSERT_TRUE(list.size());
logger.reset(); // ensure flush
AuLogInfo("RB contains: {} ({}-{}-{}...)", list.size(), list[0].line, list[1].line, list[2].line);
}
/**
* Test 1) @ 1'000'000 LogInfo with format
* [2022-03-31 03:14:15] [Debug] | [Benchmark] Push a boat-load of messages to a ring buffer took 225.05837ms
* [2022-03-31 13:05:02] [Debug] | [Benchmark] Push a boat-load of messages to a ring buffer took 227.09539ms
* [2022-03-31 13:05:19] [Debug] | [Benchmark] Push a boat-load of messages to a ring buffer took 228.08899ms
* [2022-03-31 13:05:32] [Debug] | [Benchmark] Push a boat-load of messages to a ring buffer took 224.04797ms
* [2022-03-31 13:05:43] [Debug] | [Benchmark] Push a boat-load of messages to a ring buffer took 228.00956ms
* 225.05837 / 1000000 = 0.00022505837 ms/tick
* 1000 / 0.00022505837 = 4'443'291 entries per second
* at 225ns per formatted LogInfo
*/
TEST(Echo, BenchmarckRingbuffer)
{
AuList<AuSPtr<AuLog::IBasicSink>> sinks;
auto rb = AuLog::NewRingLoggerShared(10'000);
auto cpy = rb;
ASSERT_TRUE(rb);
ASSERT_TRUE(AuListFromArgs(sinks, AuReference(rb)));
auto logger = AuLog::NewLoggerShared(sinks);
ASSERT_TRUE(logger);
{
SysBenchmark("Push a boat-load of messages to a ring buffer");
for (int i = 0; i < 1'000'000; i++)
{
logger->LogInfo("hello text file 2+2*n={}", 2 + (2 * i));
}
}
}
// Enabling this test will bog down deinit, on-crash, or a dtor somewhere
// One does not simply buffer ~400-500MB of data and expect the notoriously slow
// terminal hosts, character devices, and so on to keep up
// Benchmark data is unreliable, though the relevant profiled user-code seems to be limited to a spinlock
#if 0
TEST(Echo, BenchmarckMain)
{
{
SysBenchmark("Push a boat-load of messages to a stdout");
for (int i = 0; i < 1'000'000; i++)
{
AuLogInfo("hello main 2+2*n={}", 2 + (2 * i));
}
}
}
#endif
/**
* Test 1) @ 10'000 AuLogInfo with format
* [2022-03-31 13:01:09] [Debug] | [Benchmark] Push a few messages to a stdout took 2.06834ms
* [2022-03-31 13:06:54] [Debug] | [Benchmark] Push a few messages to a stdout took 2.06753ms
* [2022-03-31 13:07:23] [Debug] | [Benchmark] Push a few messages to a stdout took 3.00249ms
* [2022-03-31 13:07:32] [Debug] | [Benchmark] Push a few messages to a stdout took 2.05887ms
* 2 / 10000 = 0.000206834 ms/tick
* 1000 / 0.000206834 = 4'834'795 entries per second
* at 206.834ns per formatted AuLogInfo
*/
TEST(Echo, BenchmarckLite)
{
{
SysBenchmark("Push a few messages to a stdout and friends");
for (int i = 0; i < 10'000; i++)
{
AuLogInfo("hello global logger 2+2*n={}", 2 + (2 * i));
}
}
}
void RunTests()
{
Aurora::RuntimeStartInfo info;
info.console.fio.enableLogging = false;
info.console.asyncVSLog = true;
Aurora::RuntimeStart(info);
AuLogInfo("Hello global logger");
}