using System; using System.Globalization; using Amphora; using Annulus; using Azimuth; using Glass.Text; using Tessera.Text; using static Tessera.Text.Plurals; using static Tessera.Text.Styles; namespace Glass.PerformanceTests { /// /// Times writing a line of text in a language, holding it, and checking whether the language has changed, plus how /// much memory holding one costs. A control that does fixed arithmetic runs beside the rows to show whether the /// machine's speed has drifted. /// public static class LineBenchmark { public const string Argument = "--line"; public const string Usage = "Usage: Glass.PerformanceTests --line"; public const int CallsPerAction = 1000; public const int ObjectsPerSizeRow = 200; private static int sink; private static LocaleBehavior NewLocale() { return new LocaleBehavior(); } private static void Timed(string name, Action action) { TypingBenchmark.Row row = TypingBenchmark.Measure(name, action); if (WarmStart.IsWarming) { return; } double scale = 1e6 / CallsPerAction; Console.WriteLine(string.Format(CultureInfo.InvariantCulture, " {0,-46} {1,9:F1} ns (min {2,9:F1}) {3,8:F1} B/call", name, row.MedianMilliseconds * scale, row.MinimumMilliseconds * scale, row.KilobytesPerKey * 1024 / CallsPerAction)); } private static void Control() { Timed("control: 1000 adds", () => { int total = 0; for (int i = 0; i < CallsPerAction; ++i) { total += i; } sink += total; }); } private static long BytesFor(Action make) { GC.Collect(); long before = GC.GetAllocatedBytesForCurrentThread(); make(); return GC.GetAllocatedBytesForCurrentThread() - before; } private static void SizeRow(string name, Func make) { var kept = new object[ObjectsPerSizeRow]; long bytes = BytesFor(() => { for (int i = 0; i < kept.Length; ++i) { kept[i] = make(); } }); GC.KeepAlive(kept); Console.WriteLine(string.Format(CultureInfo.InvariantCulture, " {0,-46} {1,9:F0} B", name, (double)bytes / kept.Length)); } private static double ElementBytes(Func makeThousand) { Array elements = null; long bytes = BytesFor(() => elements = makeThousand()); GC.KeepAlive(elements); return (double)bytes / elements.Length; } private static TextMaterialStyle LabelStyle() { return new TextMaterialStyle { Font = SampleFonts.Philosopher18, HorizontalMargin = 0, VerticalMargin = 0, }; } private static TextBox NewBox() { var material = new TextBoxMaterial { Scale = default, Background = default, InactiveSelection = default, CompositionThickness = default, CompositionTarget = default, CompositionTargetThickness = default, ScrollBar = default, HorizontalScrollBar = default, Text = LabelStyle(), Selection = new SolidColorMaterial(), Caret = new SolidColorMaterial(), Composition = new SolidColorMaterial(), CaretWidth = 2, }; var box = new TextBox(new TextBoxBehavior { Editable = false }) { TextBoxMaterial = material }; box.Bounds = Rect.FromPositionAndSize(Vector.Zero, new Vector(400, SampleFonts.Philosopher18.LineHeight)); return box; } private static Label NewLabel() { return new Label(LabelStyle(), "") { Bounds = Rect.FromPositionAndSize(Vector.Zero, new Vector(400, SampleFonts.Philosopher18.LineHeight)), }; } private static void WritingRows() { LocaleBehavior locale = NewLocale(); char[] scratch = null; var spans = new DynamicArray(); TextPieces simple = $"{Count(1234)} boxes"; TextPieces rich = $"{Strikethrough(Count(1234))} of {Count(5678)} is {Fixed(12.5, 2)} and {Plain(42)} left"; PluralLine plural = Plural(Count(1234), one: $"{Amount} box", other: $"{Amount} boxes"); PluralLine pluralRich = Plural(Count(1234), one: $"{Strikethrough(Amount)} of {Count(5678)} box", other: $"{Strikethrough(Amount)} of {Count(5678)} boxes"); Timed("write, simple line", () => { for (int i = 0; i < CallsPerAction; ++i) { sink += locale.Write(simple, ref scratch, spans).Length; } }); Timed("write, line with four holes", () => { for (int i = 0; i < CallsPerAction; ++i) { sink += locale.Write(rich, ref scratch, spans).Length; } }); Timed("write, plural line", () => { for (int i = 0; i < CallsPerAction; ++i) { sink += locale.Write(plural, ref scratch, spans).Length; } }); Timed("write, plural line with four holes", () => { for (int i = 0; i < CallsPerAction; ++i) { sink += locale.Write(pluralRich, ref scratch, spans).Length; } }); TextPieces heldPieces = default; PluralLine heldPlural = default; Timed("copy a TextPieces into a field", () => { for (int i = 0; i < CallsPerAction; ++i) { heldPieces = rich; } }); Timed("copy a PluralLine into a field", () => { for (int i = 0; i < CallsPerAction; ++i) { heldPlural = pluralRich; } }); GC.KeepAlive(heldPieces); GC.KeepAlive(heldPlural); } private static void CheckingRows() { LocaleBehavior locale = NewLocale(); TextPieces rich = $"{Strikethrough(Count(1234))} of {Count(5678)} is {Fixed(12.5, 2)} and {Plain(42)} left"; Label plainLabel = NewLabel(); plainLabel.Text = "boxes"; Label heldLabel = NewLabel(); heldLabel.SetLine(rich, locale); TextBox plainBox = NewBox(); plainBox.Text = "boxes"; TextBox heldBox = NewBox(); heldBox.SetLine(rich, locale); var state = new UpdateState(); Timed("Label.Update, no line (control)", () => { for (int i = 0; i < CallsPerAction; ++i) { plainLabel.Update(Amphora.Duration.Zero, state); } }); Timed("Label.Update, line held, language unchanged", () => { for (int i = 0; i < CallsPerAction; ++i) { heldLabel.Update(Amphora.Duration.Zero, state); } }); Timed("TextBox.Update, no line (control)", () => { for (int i = 0; i < CallsPerAction; ++i) { plainBox.Update(Amphora.Duration.Zero, state); } }); Timed("TextBox.Update, line held, language unchanged", () => { for (int i = 0; i < CallsPerAction; ++i) { heldBox.Update(Amphora.Duration.Zero, state); } }); } private static void ChangeRows() { LocaleBehavior locale = NewLocale(); NumberFormat first = NumberFormat.Invariant; NumberFormat second = NumberFormat.FromCulture(new CultureInfo("de-DE")); TextPieces rich = $"{Strikethrough(Count(1234))} of {Count(5678)} is {Fixed(12.5, 2)} and {Plain(42)} left"; var state = new UpdateState(); Label label = NewLabel(); label.SetLine(rich, locale); TextBox box = NewBox(); box.SetLine(rich, locale); bool useSecond = false; Timed("language change, then Label.Update", () => { for (int i = 0; i < CallsPerAction; ++i) { useSecond = !useSecond; locale.NumberFormat = useSecond ? second : first; label.Update(Amphora.Duration.Zero, state); } }); Timed("language change, then TextBox.Update", () => { for (int i = 0; i < CallsPerAction; ++i) { useSecond = !useSecond; locale.NumberFormat = useSecond ? second : first; box.Update(Amphora.Duration.Zero, state); } }); Timed("language change alone (control)", () => { for (int i = 0; i < CallsPerAction; ++i) { useSecond = !useSecond; locale.NumberFormat = useSecond ? second : first; } }); } private static void SizeRows() { TextPieces rich = $"{Strikethrough(Count(1234))} of {Count(5678)} is {Fixed(12.5, 2)} and {Plain(42)} left"; LocaleBehavior locale = NewLocale(); Console.WriteLine(string.Format(CultureInfo.InvariantCulture, " {0,-46} {1,9:F0} B", "TextPieces, as an array element", ElementBytes(() => new TextPieces[1000]))); Console.WriteLine(string.Format(CultureInfo.InvariantCulture, " {0,-46} {1,9:F0} B", "PluralLine, as an array element", ElementBytes(() => new PluralLine[1000]))); Console.WriteLine(string.Format(CultureInfo.InvariantCulture, " {0,-46} {1,9:F0} B", "StyleSpan, as an array element", ElementBytes(() => new StyleSpan[1000]))); SizeRow("LocalizedLine holding a line", () => { var returnMe = new LocalizedLine(); returnMe.Set(rich, locale); return returnMe; }); SizeRow("Label, no line", () => NewLabel()); SizeRow("Label, line held", () => { Label returnMe = NewLabel(); returnMe.SetLine(rich, locale); return returnMe; }); SizeRow("TextBox, no line", () => NewBox()); SizeRow("TextBox, line held", () => { TextBox returnMe = NewBox(); returnMe.SetLine(rich, locale); return returnMe; }); } public static int Run(string[] args) { // The warm-up run adds a text size, which no row here uses. if (args.Length > 2) { Console.Error.WriteLine(Usage); return 1; } string bitness = Environment.Is64BitProcess ? "x64" : "x86"; Console.WriteLine($"Lines of text, {bitness}. Each row is the cost of one call."); Console.WriteLine("--- writing and copying"); Control(); WritingRows(); Control(); Console.WriteLine("--- checking for a language change"); Control(); CheckingRows(); Control(); Console.WriteLine("--- writing again after a language change"); Control(); ChangeRows(); Control(); if (!WarmStart.IsWarming) { Console.WriteLine("--- memory"); SizeRows(); } return 0; } } }