TextBenchmark.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Diagnostics;
  5. using System.IO;
  6. using System.Linq;
  7. using NUnit.Framework;
  8. using QuestPDF.Examples.Engine;
  9. using QuestPDF.Fluent;
  10. using QuestPDF.Helpers;
  11. using QuestPDF.Infrastructure;
  12. namespace QuestPDF.Examples
  13. {
  14. public class TextBenchmark
  15. {
  16. [Test]
  17. public void Benchmark()
  18. {
  19. var subtitleStyle = TextStyle.Default.Size(24).SemiBold().Color(Colors.Blue.Medium);
  20. var normalStyle = TextStyle.Default.Size(14);
  21. var chapters = GetChapters().ToList();
  22. var results = PerformTest(16).ToList();
  23. Console.WriteLine($"Min: {results.Min():F}");
  24. Console.WriteLine($"Max: {results.Max():F}");
  25. Console.WriteLine($"Avg: {results.Average():F}");
  26. IEnumerable<(string title, string content)> GetChapters()
  27. {
  28. var book = File.ReadAllLines("quo-vadis.txt");
  29. var chapterPointers = book
  30. .Select((line, index) => new
  31. {
  32. LineNumber = index,
  33. Text = line
  34. })
  35. .Where(x => x.Text.Length < 50 && x.Text.Contains("Rozdział") || x.Text.Contains("-----"))
  36. .Select(x => x.LineNumber)
  37. .ToList();
  38. foreach (var index in Enumerable.Range(0, chapterPointers.Count - 1))
  39. {
  40. var chapter = chapterPointers[index];
  41. var title = book[chapter];
  42. var lineFrom = chapterPointers[index];
  43. var lineTo = chapterPointers[index + 1] - 1;
  44. var lines = book.Skip(lineFrom + 1).Take(lineTo - lineFrom);
  45. var content = string.Join(Environment.NewLine, lines);
  46. yield return (title, content);
  47. }
  48. }
  49. void GenerateDocument()
  50. {
  51. RenderingTest
  52. .Create()
  53. .PageSize(PageSizes.A4)
  54. .FileName()
  55. .ProducePdf()
  56. .Render(ComposePage);
  57. }
  58. IEnumerable<float> PerformTest(int attempts)
  59. {
  60. foreach (var i in Enumerable.Range(0, attempts))
  61. {
  62. var timer = new Stopwatch();
  63. timer.Start();
  64. GenerateDocument();
  65. timer.Stop();
  66. Console.WriteLine($"Attempt {i}: {timer.ElapsedMilliseconds:F}");
  67. yield return timer.ElapsedMilliseconds;
  68. }
  69. }
  70. void ComposePage(IContainer container)
  71. {
  72. container
  73. .Padding(50)
  74. .Decoration(decoration =>
  75. {
  76. decoration
  77. .Content()
  78. .Stack(stack =>
  79. {
  80. stack.Item().Element(Title);
  81. stack.Item().PageBreak();
  82. stack.Item().Element(TableOfContents);
  83. stack.Item().PageBreak();
  84. Chapters(stack);
  85. });
  86. decoration.Footer().Element(Footer);
  87. });
  88. }
  89. void Title(IContainer container)
  90. {
  91. container
  92. .Extend()
  93. .PaddingBottom(200)
  94. .AlignBottom()
  95. .Stack(stack =>
  96. {
  97. stack.Item().Text("Quo Vadis", TextStyle.Default.Size(72).Bold().Color(Colors.Blue.Darken2));
  98. stack.Item().Text("Henryk Sienkiewicz", TextStyle.Default.Size(24).Color(Colors.Grey.Darken2));
  99. });
  100. }
  101. void TableOfContents(IContainer container)
  102. {
  103. container.Stack(stack =>
  104. {
  105. stack.Item().Text("Table of contents", subtitleStyle);
  106. stack.Item().PaddingTop(10).PaddingBottom(50).BorderBottom(1).BorderColor(Colors.Grey.Lighten2).ExtendHorizontal();
  107. foreach (var chapter in chapters)
  108. {
  109. stack.Item().InternalLink(chapter.title).Row(row =>
  110. {
  111. row.RelativeColumn().Text(chapter.title);
  112. row.ConstantColumn(100).AlignRight().Text(text => text.PageNumberOfLocation(chapter.title, normalStyle));
  113. });
  114. }
  115. });
  116. }
  117. void Chapters(StackDescriptor stack)
  118. {
  119. foreach (var chapter in chapters)
  120. {
  121. stack.Item().Element(container => Chapter(container, chapter.title, chapter.content));
  122. }
  123. }
  124. void Chapter(IContainer container, string title, string content)
  125. {
  126. container.Stack(stack =>
  127. {
  128. stack.Item().Location(title).Text(title, subtitleStyle);
  129. stack.Item().PaddingTop(10).PaddingBottom(50).BorderBottom(1).BorderColor(Colors.Grey.Lighten2).ExtendHorizontal();
  130. stack.Item().Text(text =>
  131. {
  132. text.ParagraphSpacing(10);
  133. text.Span(content, normalStyle);
  134. });
  135. stack.Item().PageBreak();
  136. });
  137. }
  138. void Footer(IContainer container)
  139. {
  140. container
  141. .AlignCenter()
  142. .Text(text =>
  143. {
  144. text.CurrentPageNumber();
  145. text.Span(" / ");
  146. text.TotalPages();
  147. });
  148. }
  149. }
  150. }
  151. }