TextBenchmark.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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).Where(x => !string.IsNullOrWhiteSpace(x));
  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. stack.Item().Element(Acknowledgements);
  110. });
  111. decoration.Footer().Element(Footer);
  112. });
  113. }
  114. void Title(IContainer container)
  115. {
  116. container
  117. .Extend()
  118. .PaddingBottom(200)
  119. .AlignBottom()
  120. .Stack(stack =>
  121. {
  122. stack.Item().Text("Quo Vadis", TextStyle.Default.Size(72).Bold().Color(Colors.Blue.Darken2));
  123. stack.Item().Text("Henryk Sienkiewicz", TextStyle.Default.Size(24).Color(Colors.Grey.Darken2));
  124. });
  125. }
  126. void TableOfContents(IContainer container)
  127. {
  128. container.Stack(stack =>
  129. {
  130. SectionTitle(stack, "Spis treści");
  131. foreach (var chapter in chapters)
  132. {
  133. stack.Item().InternalLink(chapter.Title).Row(row =>
  134. {
  135. row.RelativeColumn().Text(chapter.Title, normalStyle);
  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. SectionTitle(stack, title);
  153. stack.Item().Text(text =>
  154. {
  155. text.ParagraphSpacing(5);
  156. text.Span(content, normalStyle);
  157. });
  158. stack.Item().PageBreak();
  159. });
  160. }
  161. void Acknowledgements(IContainer container)
  162. {
  163. container.Stack(stack =>
  164. {
  165. SectionTitle(stack, "Podziękowania");
  166. stack.Item().Text(text =>
  167. {
  168. text.DefaultTextStyle(normalStyle);
  169. text.Span("Ten dokument został wygenerowany na podstawie książki w formacie TXT opublikowanej w serwisie ");
  170. text.ExternalLocation("wolnelektury.pl", "https://wolnelektury.pl/", normalStyle.Color(Colors.Blue.Medium).Underline());
  171. text.Span(". Dziękuję za wspieranie polskiego czytelnictwa!");
  172. });
  173. });
  174. }
  175. void SectionTitle(StackDescriptor stack, string text)
  176. {
  177. stack.Item().Location(text).Text(text, subtitleStyle);
  178. stack.Item().PaddingTop(10).PaddingBottom(50).BorderBottom(1).BorderColor(Colors.Grey.Lighten2).ExtendHorizontal();
  179. }
  180. void Footer(IContainer container)
  181. {
  182. container
  183. .AlignCenter()
  184. .Text(text =>
  185. {
  186. text.CurrentPageNumber();
  187. text.Span(" / ");
  188. text.TotalPages();
  189. });
  190. }
  191. }
  192. }
  193. }