diff options
Diffstat (limited to 'Samples/PerfTests/Program.cs')
| -rw-r--r-- | Samples/PerfTests/Program.cs | 124 |
1 files changed, 106 insertions, 18 deletions
diff --git a/Samples/PerfTests/Program.cs b/Samples/PerfTests/Program.cs index 68d8c73e..39ba8c88 100644 --- a/Samples/PerfTests/Program.cs +++ b/Samples/PerfTests/Program.cs @@ -11,14 +11,56 @@ namespace PerfTests { class TestInterface : Interface { - public TestInterface (int width = 800, int height = 600) + readonly int count; + readonly bool miliseconds = false; + readonly bool resetStylesAndTemplates = true; + readonly string outDirectory; + + Stream outStream; + TextWriter writer; + + bool logToDisk => writer != null; + + void writeHeader (TextWriter txtWriter) { + txtWriter.WriteLine ($"Crow perf test ({clientRectangle.Width} X {clientRectangle.Height}), repeat count = {count}, reset style and templates = {resetStylesAndTemplates}"); + txtWriter.WriteLine ($"git:{ThisAssembly.Git.Commit} {ThisAssembly.Git.Branch} {ThisAssembly.Git.SemVer.Major}.{ThisAssembly.Git.SemVer.Minor}.{ThisAssembly.Git.SemVer.Patch} {ThisAssembly.Git.SemVer.Label}"); + } + public TestInterface (int count, string _outDirectory = "", int width = 800, int height = 600) : base (width, height, false, false) { + this.count = count; + + if (string.IsNullOrEmpty (_outDirectory)) { + writeHeader (Console.Out); + Console.WriteLine (new string ('-', 100)); + Console.WriteLine ($"{" Path",-50}| Min | Mean | Max | Alloc(kb)| Lost(Kb) |"); + } else { + string dirName = Path.IsPathRooted (_outDirectory) ? _outDirectory : + Path.Combine (Directory.GetCurrentDirectory (), _outDirectory); + if (!Directory.Exists (dirName)) + Directory.CreateDirectory (dirName); + + string filename = "crow-" + DateTime.Now.ToString ("yyyy-MM-dd-HH-mm") +".perflog"; + outStream = new FileStream (Path.Combine (dirName, filename), FileMode.Create); + writer = new StreamWriter (outStream); + writeHeader (writer); + writer.WriteLine ("Path;Min;Mean;Max;Allocated;LostMem"); + } + surf = new Crow.Cairo.ImageSurface (Crow.Cairo.Format.Argb32, ClientRectangle.Width, ClientRectangle.Height); Init (); } - long Test (string path, out long min, out long max, int count = 10) + protected override void Dispose (bool disposing) { + base.Dispose (disposing); + + if (logToDisk) { + writer.Dispose (); + outStream.Dispose (); + } + } + + long Test (string path, out long min, out long max) { min = long.MaxValue; max = long.MinValue; @@ -37,11 +79,30 @@ namespace PerfTests sw.Stop (); - if (sw.ElapsedTicks < min) - min = sw.ElapsedMilliseconds; - if (sw.ElapsedTicks > max) - max = sw.ElapsedMilliseconds; - total += sw.ElapsedMilliseconds; + ClearInterface (); + if (resetStylesAndTemplates) { + this.Styling.Clear (); + this.StylingConstants.Clear (); + this.Instantiators.Clear (); + this.Templates.Clear (); + this.DefaultTemplates.Clear (); + this.DefaultValuesLoader.Clear (); + GC.Collect (); + } + + if (miliseconds) { + if (sw.ElapsedMilliseconds < min) + min = sw.ElapsedMilliseconds; + if (sw.ElapsedMilliseconds > max) + max = sw.ElapsedMilliseconds; + total += sw.ElapsedMilliseconds; + } else { + if (sw.ElapsedTicks < min) + min = sw.ElapsedTicks; + if (sw.ElapsedTicks > max) + max = sw.ElapsedTicks; + total += sw.ElapsedTicks; + } } @@ -50,31 +111,58 @@ namespace PerfTests void testDir (string dirPath, int level = 0) { - Console.WriteLine ($"{new string (' ', level * 4)}- {dirPath}"); + string label = $"{new string (' ', level * 4)}- {dirPath}"; + if (!logToDisk) + Console.WriteLine (label, -50); level++; foreach (string d in Directory.GetDirectories (dirPath)) testDir (d, level); - foreach (string f in Directory.GetFiles (dirPath)) { + foreach (string f in Directory.GetFiles (dirPath, "*.crow")) { + label = $"{new string (' ', level * 4)}{ Path.GetFileName (f)}"; try { + long totMemBefore = GC.GetTotalMemory (true); + long allocBefore = GC.GetTotalAllocatedBytes (true); long mean = Test (f, out long min, out long max); - Console.WriteLine ($"{new string (' ', level * 4)}{ Path.GetFileName (f),-30}|{min,5}|{mean,5}|{max,5}"); + long allocAfter = GC.GetTotalAllocatedBytes (true); + long totMemAfter = GC.GetTotalMemory (true); + double allocated = (double)(allocAfter - allocBefore) / 1024.0; + double lostMem = (double)(totMemAfter - totMemBefore) / 1024.0; + if (logToDisk) { + writer.WriteLine ($"{f};{min};{mean};{max};{allocAfter - allocBefore};{totMemAfter - totMemBefore}"); + } else { + if (miliseconds) + Console.WriteLine ($"{label,-50}|{min,10}|{mean,10}|{max,10}| {allocated,8:0.0} | {lostMem,8:0.0} |"); + else + Console.WriteLine ($"{label,-50}|{0.001 * min,10:0.00}|{0.001 * mean,10:0.00}|{0.001 * max,10:0.00}| {allocated,8:0.0} | {lostMem,8:0.0} |"); + } } catch (Exception ex) { - Console.ForegroundColor = ConsoleColor.Red; - Console.WriteLine ($"- {ex.Message}"); - Console.ResetColor (); + if (logToDisk) { + writer.WriteLine ($"{f};{ex.Message}"); + } else { + Console.WriteLine ($"{label,-50}"); + Console.ForegroundColor = ConsoleColor.Red; + Console.WriteLine ($"\t\t{ex.Message}"); + Console.ResetColor (); + } } } } - + public static void Main (string [] args) { - Console.WriteLine ($"git:{ThisAssembly.Git.Commit} {ThisAssembly.Git.Branch} {ThisAssembly.Git.SemVer.Major}.{ThisAssembly.Git.SemVer.Minor}.{ThisAssembly.Git.SemVer.Patch} {ThisAssembly.Git.SemVer.Label}"); - //IndentedTextWriter w = new IndentedTextWriter() - using (TestInterface iface = new TestInterface ()) { - iface.testDir ("Interfaces"); + int count = 10; + string outDir = ""; + + if (args.Length > 0 && int.TryParse (args[0], out int c)) + count = c; + if (args.Length > 1) + outDir = args[1]; + + using (TestInterface iface = new TestInterface (count, outDir)) { + iface.testDir (@"Interfaces"); } } } |