cs-codex-dist-tests/Tests/CodexTests/BasicTests/LogHelperTests.cs

80 lines
2.6 KiB
C#
Raw Normal View History

2024-04-25 08:25:12 +00:00
using CodexPlugin;
using NUnit.Framework;
using Utils;
namespace CodexTests.BasicTests
{
[TestFixture]
public class LogHelperTests : AutoBootstrapDistTest
2024-04-25 08:25:12 +00:00
{
[Test]
public void FindMostCommonLogMessages()
{
2024-04-25 13:04:02 +00:00
var uploader = AddCodex(s => s.WithName("uploader").WithLogLevel(CodexLogLevel.Trace));
var downloader = AddCodex(s => s.WithName("downloader").WithLogLevel(CodexLogLevel.Trace));
2024-04-25 08:25:12 +00:00
2024-04-25 13:04:02 +00:00
var cid = uploader.UploadFile(GenerateTestFile(100.MB()));
2024-04-25 08:25:12 +00:00
Thread.Sleep(1000);
var logStartUtc = DateTime.UtcNow;
Thread.Sleep(1000);
2024-04-25 08:25:12 +00:00
downloader.DownloadContent(cid);
2024-04-25 13:04:02 +00:00
var map = GetLogMap(downloader, logStartUtc).OrderByDescending(p => p.Value);
Log("Downloader - Receive");
2024-04-25 08:25:12 +00:00
foreach (var entry in map)
{
2024-04-25 13:04:02 +00:00
if (entry.Value > 9)
{
Log($"'{entry.Key}' = {entry.Value}");
}
2024-04-25 08:25:12 +00:00
}
}
private Dictionary<string, int> GetLogMap(ICodexNode node, DateTime? startUtc = null)
2024-04-25 08:25:12 +00:00
{
var log = Ci.DownloadLog(node);
var map = new Dictionary<string, int>();
log.IterateLines(line =>
{
if (string.IsNullOrEmpty(line) ||
!line.Contains(" ") ||
!line.Contains("=") ||
line.Length < 34 ||
line[33] != ' '
) return;
if (startUtc.HasValue)
{
var timestampLine = line.Substring(4, 23);
var timestamp = DateTime.Parse(timestampLine);
if (timestamp < startUtc) return;
}
2024-04-25 08:25:12 +00:00
// "INF 2024-04-14 10:40:50.042+00:00 Creating a private key and saving it tid=1 count=2"
var start = 34;
var msg = line.Substring(start);
// "Creating a private key and saving it tid=1 count=2"
var firstEqualSign = msg.IndexOf("=");
msg = msg.Substring(0, firstEqualSign);
// "Creating a private key and saving it tid"
var lastSpace = msg.LastIndexOf(" ");
msg = msg.Substring(0, lastSpace);
// "Creating a private key and saving it "
msg = msg.Trim();
// "Creating a private key and saving it"
if (map.ContainsKey(msg)) map[msg] += 1;
else map.Add(msg, 1);
});
return map;
}
}
}