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

62 lines
1.9 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 : CodexDistTest
{
[Test]
public void FindMostCommonLogMessages()
{
var node = AddCodex(s => s.WithLogLevel(CodexLogLevel.Trace));
node.UploadFile(GenerateTestFile(1.GB()));
var map = GetLogMap(node).OrderByDescending(p => p.Value);
foreach (var entry in map)
{
Log($"'{entry.Key}' = {entry.Value}");
}
}
private Dictionary<string, int> GetLogMap(ICodexNode node)
{
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;
// "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;
}
}
}