using System; using System.Diagnostics; using System.Globalization; using Amphora; using Clave; using Glass.Startup; namespace Glass.PerformanceTests { /// /// Times what costs, then the first BMFont parse, text box and text box with a scroll bar /// in a process, with and without it. Each is followed by a second, built the same way, as the control. That is /// what the first would cost with nothing left to compile. /// public static class WarmupBenchmark { public const string Argument = "--warmup"; public const string WarmedOption = "warmed"; public const string Usage = "Usage: Glass.PerformanceTests --warmup [warmed]"; public const int Chars = 2000; private static double Milliseconds(long ticks) => ticks * 1000.0 / Stopwatch.Frequency; /// /// Builds a box over a text, counts it, types a key into it, undoes that and presses Escape, and returns the /// ticks it took. /// public static long TimeABox(string text, bool withScrollBar) { var timer = Stopwatch.StartNew(); TextBox box = TypingBenchmark.NewBox(text, withScrollBar); while (!box.AllLinesCounted) { box.Update(Duration.Zero, new UpdateState()); } var keys = new KeyboardManager(); keys.Controls.Add(box); keys.TrySetFocus(box); box.TextInput("x", keys); box.Update(Duration.Zero, new UpdateState { Focused = box }); box.Buffer.Undo(); keys.KeyDown(new KeyTracker().KeyDown(KeyCode.Escape, KeyCode.None, isRepeat: false)); return timer.ElapsedTicks; } public static int Run(string[] args) { bool warmed = args.Length == 2 && args[1] == WarmedOption; if (args.Length > 2 || args.Length == 2 && !warmed) { Console.Error.WriteLine(Usage); return 1; } Program.ClaimOneCore(); string text = TypingBenchmark.TextOfAbout(Chars); long warmupTicks = 0; if (warmed) { var timer = Stopwatch.StartNew(); Warmup.Run(); warmupTicks = timer.ElapsedTicks; // The warm-up leaves garbage. A host collects while it is still loading, so no frame pays for it. GC.Collect(); GC.WaitForPendingFinalizers(); GC.Collect(); } // The first parse in the process. Loading the sample fonts after it parses them again, off the clock. var parseTimer = Stopwatch.StartNew(); SampleFonts.Load("Philosopher-Regular-18px.fnt"); long parseTicks = parseTimer.ElapsedTicks; parseTimer.Restart(); SampleFonts.Load("Philosopher-Regular-18px.fnt"); long secondParseTicks = parseTimer.ElapsedTicks; _ = SampleFonts.Philosopher18; double first = Milliseconds(TimeABox(text, withScrollBar: false)); double second = Milliseconds(TimeABox(text, withScrollBar: false)); double barFirst = Milliseconds(TimeABox(text, withScrollBar: true)); double barSecond = Milliseconds(TimeABox(text, withScrollBar: true)); Console.WriteLine(string.Format(CultureInfo.InvariantCulture, "{0,-7} warm-up {1,6:0.0} font parse {2,5:0.0} / {3,4:0.0} box {4,6:0.0} / {5,5:0.0}" + " box + bar {6,6:0.0} / {7,5:0.0} (ms, first / second)", warmed ? "warmed" : "cold", Milliseconds(warmupTicks), Milliseconds(parseTicks), Milliseconds(secondParseTicks), first, second, barFirst, barSecond)); return 0; } } }