TextBenchmark.cs 6.8 KB

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