Skip to content

Commit 83491e4

Browse files
committed
Fix analyzers and introduce logging
1 parent e730178 commit 83491e4

46 files changed

Lines changed: 422 additions & 238 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Locals/Directory.Packages.props

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,10 @@
1515
<PackageVersion Include="Verify.DiffPlex" Version="3.1.2" />
1616
<PackageVersion Include="Verify.Xunit" Version="28.5.0" />
1717
<PackageVersion Include="xunit.extensibility.core" Version="2.9.3" />
18+
<PackageVersion Include="AgileObjects.ReadableExpressions" Version="4.1.3" />
19+
<PackageVersion Include="Microsoft.Extensions.Logging.Abstractions" Version="9.0.5" />
20+
<PackageVersion Include="Serilog.Expressions" Version="5.0.0" />
21+
<PackageVersion Include="Serilog.Extensions.Logging" Version="9.0.1" />
22+
<PackageVersion Include="Serilog.Sinks.File" Version="7.0.0" />
1823
</ItemGroup>
1924
</Project>

Src/FastData.Benchmarks/Benchmarks/AnalyzerBenchmarks.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using Genbox.FastData.Internal.Analysis.Analyzers;
44
using Genbox.FastData.Internal.Analysis.Properties;
55
using Genbox.FastData.InternalShared;
6+
using Microsoft.Extensions.Logging.Abstractions;
67

78
namespace Genbox.FastData.Benchmarks.Benchmarks;
89

@@ -18,7 +19,7 @@ public AnalyzerBenchmarks()
1819
string[] data = Enumerable.Range(1, 100).Select(x => TestHelper.GenerateRandomString(rng, 50)).ToArray();
1920
StringProperties props = DataAnalyzer.GetStringProperties(data);
2021

21-
_analyzer = new GPerfAnalyzer(data, props, new GPerfAnalyzerConfig(), new Simulator(data, new SimulatorConfig()));
22+
_analyzer = new GPerfAnalyzer(data, props, new GPerfAnalyzerConfig(), new Simulator(data, new SimulatorConfig()), NullLogger<GPerfAnalyzer>.Instance);
2223
}
2324

2425
[Benchmark]

Src/FastData.Benchmarks/Benchmarks/SegmentGeneratorsBenchmarks.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ public class SegmentGeneratorsBenchmarks
1111
{
1212
//We start at 8 and go up to 100 to cover as many cases as possible
1313
private readonly StringProperties _props = DataAnalyzer.GetStringProperties(Enumerable.Range(8, 100).Select(x => TestHelper.GenerateRandomString(Random.Shared, x)).ToArray());
14-
private readonly BruteForceGenerator _bfGen = new BruteForceGenerator();
15-
private readonly EdgeGramGenerator _egGen = new EdgeGramGenerator();
14+
private readonly BruteForceGenerator _bfGen = new BruteForceGenerator(8);
15+
private readonly EdgeGramGenerator _egGen = new EdgeGramGenerator(8);
1616
private readonly DeltaGenerator _deltaGen = new DeltaGenerator();
1717
private readonly OffsetGenerator _ofGen = new OffsetGenerator();
1818

Src/FastData.InternalShared/ITestData.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using Genbox.FastData.Abstracts;
2-
using Genbox.FastData.Enums;
32
using Genbox.FastData.Generator.Framework;
43

54
namespace Genbox.FastData.InternalShared;

Src/FastData.InternalShared/TestVector.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using Genbox.FastData.Abstracts;
22
using Genbox.FastData.Enums;
3-
using Genbox.FastData.Generator.Framework;
43
using Xunit.Abstractions;
54

65
namespace Genbox.FastData.InternalShared;

Src/FastData.InternalShared/TestVectorHelper.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
using Genbox.FastData.Enums;
66
using Genbox.FastData.Internal.Abstracts;
77
using Genbox.FastData.Internal.Analysis.Properties;
8-
using Genbox.FastData.Internal.Misc;
98
using Genbox.FastData.Internal.Structures;
109
using Genbox.FastData.Misc;
1110

Src/FastData.Testbed/FastData.Testbed.csproj

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,11 @@
1212
<ProjectReference Include="..\FastData\FastData.csproj" />
1313
</ItemGroup>
1414

15+
<ItemGroup>
16+
<PackageReference Include="AgileObjects.ReadableExpressions" />
17+
<PackageReference Include="Serilog.Expressions" />
18+
<PackageReference Include="Serilog.Extensions.Logging" />
19+
<PackageReference Include="Serilog.Sinks.File" />
20+
</ItemGroup>
21+
1522
</Project>

Src/FastData.Testbed/Program.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,7 @@ internal static class Program
77
private static void Main()
88
{
99
AnalysisTest.TestGeneticAnalyzer();
10+
AnalysisTest.TestBruteForceAnalyzer();
11+
AnalysisTest.TestGPerfAnalyzer();
1012
}
1113
}

Src/FastData.Testbed/Tests/AnalysisTest.cs

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,29 @@
11
using System.Diagnostics;
2-
using System.Linq.Expressions;
2+
using System.Globalization;
33
using System.Runtime.CompilerServices;
44
using Genbox.FastData.Configs;
55
using Genbox.FastData.Internal.Analysis;
66
using Genbox.FastData.Internal.Analysis.Analyzers;
77
using Genbox.FastData.Internal.Analysis.Properties;
88
using Genbox.FastData.Internal.Helpers;
9-
using Genbox.FastData.InternalShared;
10-
using Genbox.FastData.Misc;
9+
using Microsoft.Extensions.Logging;
10+
using Serilog;
11+
using Serilog.Core;
12+
using Serilog.Extensions.Logging;
13+
using Serilog.Templates;
1114

1215
namespace Genbox.FastData.Testbed.Tests;
1316

1417
internal static class AnalysisTest
1518
{
19+
private static readonly Logger _logConf = new LoggerConfiguration()
20+
.MinimumLevel.Debug()
21+
.WriteTo.File(
22+
new ExpressionTemplate("[{@t:HH:mm:ss} {@l:u3}] [{substring(@p['SourceContext'], lastIndexOf(@p['SourceContext'], '.') + 1)}] {@m}\n"),
23+
@"C:\Temp\FastData.log"
24+
)
25+
.CreateLogger();
26+
1627
// private static readonly char[] LoEntropy = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".ToCharArray();
1728
// private static readonly char[] HiEntropy = Enumerable.Range(0, ushort.MaxValue).Select(i => (char)i).ToArray();
1829
private static readonly string[] Data =
@@ -27,6 +38,7 @@ internal static class AnalysisTest
2738
public static void TestBruteForceAnalyzer()
2839
{
2940
RunBruteForce(RunFunc(Data, 5.0, PrependString));
41+
3042
// RunBruteForce(RunFunc(Data, 1.0, PermuteString));
3143
// RunBruteForce(RunFunc(Data, 1.0, (x, y) => MutateString(x, y, LoEntropy)));
3244
// RunBruteForce(RunFunc(Data, 1.0, (x, y) => MutateString(x, y, HiEntropy)));
@@ -35,6 +47,7 @@ public static void TestBruteForceAnalyzer()
3547
public static void TestGeneticAnalyzer()
3648
{
3749
RunGeneticAnalysis(RunFunc(Data, 5.0, PrependString));
50+
3851
// RunGeneticAnalysis(RunFunc(Data, 1.0, PermuteString));
3952
// RunGeneticAnalysis(RunFunc(Data, 1.0, (x, y) => MutateString(x, y, LoEntropy)));
4053
// RunGeneticAnalysis(RunFunc(Data, 1.0, (x, y) => MutateString(x, y, HiEntropy)));
@@ -43,42 +56,47 @@ public static void TestGeneticAnalyzer()
4356
public static void TestGPerfAnalyzer()
4457
{
4558
RunGPerfAnalysis(RunFunc(Data, 5.0, PrependString));
59+
4660
// RunGeneticAnalysis(RunFunc(Data, 1.0, PermuteString));
4761
// RunGeneticAnalysis(RunFunc(Data, 1.0, (x, y) => MutateString(x, y, LoEntropy)));
4862
// RunGeneticAnalysis(RunFunc(Data, 1.0, (x, y) => MutateString(x, y, HiEntropy)));
4963
}
5064

5165
private static void RunBruteForce(string[] data, [CallerArgumentExpression(nameof(data))]string? source = null)
5266
{
53-
Console.WriteLine("###############");
5467
Print(data, source);
55-
StringProperties props = DataAnalyzer.GetStringProperties(data);
5668

57-
BruteForceAnalyzer analyzer = new BruteForceAnalyzer(props, new BruteForceAnalyzerConfig(), new Simulator(data, new SimulatorConfig()));
58-
PrintCandidate(analyzer.GetCandidates().First());
69+
StringProperties props = DataAnalyzer.GetStringProperties(data);
70+
using SerilogLoggerFactory loggerFactory = new SerilogLoggerFactory(_logConf);
71+
BruteForceAnalyzer analyzer = new BruteForceAnalyzer(props, new BruteForceAnalyzerConfig(), new Simulator(data, new SimulatorConfig()), loggerFactory.CreateLogger<BruteForceAnalyzer>());
72+
PrintCandidate(analyzer.GetCandidates().OrderByDescending(x => x.Fitness).First());
5973
}
6074

6175
private static void RunGeneticAnalysis(string[] data, [CallerArgumentExpression(nameof(data))]string? source = null)
6276
{
63-
Console.WriteLine("###############");
6477
Print(data, source);
6578

6679
StringProperties props = DataAnalyzer.GetStringProperties(data);
67-
GeneticAnalyzer analyzer = new GeneticAnalyzer(props, new GeneticAnalyzerConfig(), new Simulator(data, new SimulatorConfig()));
68-
PrintCandidate(analyzer.GetCandidates().First());
80+
using SerilogLoggerFactory loggerFactory = new SerilogLoggerFactory(_logConf);
81+
GeneticAnalyzer analyzer = new GeneticAnalyzer(props, new GeneticAnalyzerConfig(), new Simulator(data, new SimulatorConfig()), loggerFactory.CreateLogger<GeneticAnalyzer>());
82+
PrintCandidate(analyzer.GetCandidates().OrderByDescending(x => x.Fitness).First());
6983
}
7084

7185
private static void RunGPerfAnalysis(string[] data, [CallerArgumentExpression(nameof(data))]string? source = null)
7286
{
73-
Console.WriteLine("###############");
7487
Print(data, source);
7588

7689
StringProperties props = DataAnalyzer.GetStringProperties(data);
77-
GPerfAnalyzer analyzer = new GPerfAnalyzer(data, props, new GPerfAnalyzerConfig(),new Simulator(data, new SimulatorConfig()));
78-
PrintCandidate(analyzer.GetCandidates().First());
90+
using SerilogLoggerFactory loggerFactory = new SerilogLoggerFactory(_logConf);
91+
GPerfAnalyzer analyzer = new GPerfAnalyzer(data, props, new GPerfAnalyzerConfig(), new Simulator(data, new SimulatorConfig()), loggerFactory.CreateLogger<GPerfAnalyzer>());
92+
PrintCandidate(analyzer.GetCandidates().OrderByDescending(x => x.Fitness).First());
7993
}
8094

81-
private static void Print(string[] data, string? source) => Console.WriteLine(source + ": " + string.Join(", ", data.Take(5)));
95+
private static void Print(string[] data, string? source)
96+
{
97+
Console.WriteLine("###############");
98+
Console.WriteLine(source + ": " + string.Join(", ", data.Take(5)));
99+
}
82100

83101
private static string MutateString(string str, double factor, char[] alphabet)
84102
{
@@ -133,6 +151,7 @@ private static void PrintCandidate(Candidate candidate)
133151
Console.WriteLine("#### Candidate ####");
134152
Console.WriteLine($"{nameof(candidate.Fitness)}: {candidate.Fitness}");
135153
Console.WriteLine($"{nameof(candidate.Collisions)}: {candidate.Collisions}");
154+
136155
// Console.WriteLine($"{nameof(candidate.Time)}: {candidate.Time}");
137156
// Console.WriteLine($"{nameof(candidate.Metadata)}: {string.Join(", ", candidate.Metadata.Select(x => x.ToString()))}");
138157

@@ -142,6 +161,8 @@ private static void PrintCandidate(Candidate candidate)
142161

143162
Console.WriteLine();
144163
Console.WriteLine("#### Expression ####");
164+
165+
// Console.WriteLine(candidate.StringHash.GetExpression().ToReadableString());
145166
Console.WriteLine(ExpressionHelper.Print(candidate.StringHash.GetExpression()));
146167
}
147168
}

Src/FastData.Testbed/Tests/GPerfTest.cs

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Globalization;
12
using System.Text;
23
using Genbox.FastData.Abstracts;
34
using Genbox.FastData.Configs;
@@ -6,11 +7,17 @@
67
using Genbox.FastData.Internal.Analysis.Properties;
78
using Genbox.FastData.Internal.Structures;
89
using Genbox.FastData.InternalShared;
10+
using Microsoft.Extensions.Logging;
11+
using Microsoft.Extensions.Logging.Abstractions;
12+
using Serilog;
13+
using Serilog.Configuration;
14+
using Serilog.Core;
15+
using Serilog.Extensions.Logging;
916

1017
namespace Genbox.FastData.Testbed.Tests;
1118

1219
/// <summary>
13-
/// This code enables verification of FastData against GPerf. When DebugPrint is enabled and a special version of GPerf is run on the same files, it should give the same
20+
/// This code enables verification of FastData against GPerf. When Trace logging is enabled and a special version of GPerf is run on the same files, it should give the same
1421
/// console text
1522
/// </summary>
1623
internal static class GPerfTest
@@ -19,7 +26,9 @@ internal static class GPerfTest
1926

2027
public static void ProduceOutputs(string path)
2128
{
22-
TextWriter org = Console.Out;
29+
LoggerSinkConfiguration baseConf = new LoggerConfiguration()
30+
.MinimumLevel.Verbose()
31+
.WriteTo;
2332

2433
foreach (string file in Directory.GetFiles(path))
2534
{
@@ -30,22 +39,20 @@ public static void ProduceOutputs(string path)
3039
options.Access = FileAccess.Write;
3140
options.Mode = FileMode.Create;
3241

33-
using TextWriter tw = new StreamWriter(Path.Combine(path, "fastdata", Path.GetFileNameWithoutExtension(file) + ".output"), Encoding.Latin1, options);
34-
tw.NewLine = "\n";
42+
string logFile = Path.Combine(path, "fastdata", Path.GetFileNameWithoutExtension(file) + ".output");
3543

36-
Console.SetOut(tw);
44+
Logger serilog = baseConf.File(logFile, formatProvider: CultureInfo.InvariantCulture).CreateLogger();
45+
using SerilogLoggerFactory factory = new SerilogLoggerFactory(serilog);
3746

3847
string[] data = File.ReadAllLines(file);
3948
StringProperties props = DataAnalyzer.GetStringProperties(data);
4049

41-
GPerfAnalyzer analyzer = new GPerfAnalyzer(data, props, new GPerfAnalyzerConfig(), new Simulator(data, new SimulatorConfig()));
50+
GPerfAnalyzer analyzer = new GPerfAnalyzer(data, props, new GPerfAnalyzerConfig(), new Simulator(data, new SimulatorConfig()), factory.CreateLogger<GPerfAnalyzer>());
4251
Candidate hashFunc = analyzer.GetCandidates().First();
4352

4453
HashSetPerfectStructure<string> structure = new HashSetPerfectStructure<string>();
4554
structure.TryCreate(data, hashFunc.StringHash.GetHashFunction(), out IContext? context);
4655
}
47-
48-
Console.SetOut(org);
4956
}
5057

5158
public static void MakeTestFiles(string path)

0 commit comments

Comments
 (0)