11using System . Diagnostics ;
2- using System . Linq . Expressions ;
2+ using System . Globalization ;
33using System . Runtime . CompilerServices ;
44using Genbox . FastData . Configs ;
55using Genbox . FastData . Internal . Analysis ;
66using Genbox . FastData . Internal . Analysis . Analyzers ;
77using Genbox . FastData . Internal . Analysis . Properties ;
88using 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
1215namespace Genbox . FastData . Testbed . Tests ;
1316
1417internal 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}
0 commit comments