TextBenchmark.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. .ProducePdf()
  23. .ShowResults()
  24. .Render(x => ComposeBook(x, chapters));
  25. }
  26. [Test]
  27. public void Benchmark()
  28. {
  29. var chapters = GetBookChapters().ToList();
  30. var results = PerformTest(16).ToList();
  31. Console.WriteLine($"Min: {results.Min():F}");
  32. Console.WriteLine($"Max: {results.Max():F}");
  33. Console.WriteLine($"Avg: {results.Average():F}");
  34. void GenerateDocument()
  35. {
  36. RenderingTest
  37. .Create()
  38. .PageSize(PageSizes.A4)
  39. .ProducePdf()
  40. .Render(x => ComposeBook(x, chapters));
  41. }
  42. IEnumerable<float> PerformTest(int attempts)
  43. {
  44. foreach (var i in Enumerable.Range(0, attempts))
  45. {
  46. var timer = new Stopwatch();
  47. timer.Start();
  48. GenerateDocument();
  49. timer.Stop();
  50. Console.WriteLine($"Attempt {i}: {timer.ElapsedMilliseconds:F}");
  51. yield return timer.ElapsedMilliseconds;
  52. }
  53. }
  54. }
  55. class BookChapter
  56. {
  57. public string Title { get; set; }
  58. public string Content { get; set; }
  59. }
  60. private static IEnumerable<BookChapter> GetBookChapters()
  61. {
  62. var book = File.ReadAllLines("quo-vadis.txt");
  63. var chapterPointers = book
  64. .Select((line, index) => new
  65. {
  66. LineNumber = index,
  67. Text = line
  68. })
  69. .Where(x => x.Text.Length < 50 && x.Text.Contains("Rozdział") || x.Text.Contains("-----"))
  70. .Select(x => x.LineNumber)
  71. .ToList();
  72. foreach (var index in Enumerable.Range(0, chapterPointers.Count - 1))
  73. {
  74. var chapter = chapterPointers[index];
  75. var title = book[chapter];
  76. var lineFrom = chapterPointers[index];
  77. var lineTo = chapterPointers[index + 1] - 1;
  78. var lines = book.Skip(lineFrom + 1).Take(lineTo - lineFrom).Where(x => !string.IsNullOrWhiteSpace(x));
  79. var content = string.Join(Environment.NewLine, lines);
  80. yield return new BookChapter
  81. {
  82. Title = title,
  83. Content = content
  84. };
  85. }
  86. }
  87. private void ComposeBook(IContainer container, ICollection<BookChapter> chapters)
  88. {
  89. var subtitleStyle = TextStyle.Default.Size(24).SemiBold().Color(Colors.Blue.Medium);
  90. var normalStyle = TextStyle.Default.Size(14);
  91. ComposePage(container);
  92. void ComposePage(IContainer container)
  93. {
  94. container
  95. .Padding(50)
  96. .Decoration(decoration =>
  97. {
  98. decoration
  99. .Content()
  100. .Column(column =>
  101. {
  102. column.Item().Element(Title);
  103. column.Item().PageBreak();
  104. column.Item().Element(TableOfContents);
  105. column.Item().PageBreak();
  106. Chapters(column);
  107. column.Item().Element(Acknowledgements);
  108. });
  109. decoration.After().Element(Footer);
  110. });
  111. }
  112. void Title(IContainer container)
  113. {
  114. container
  115. .Extend()
  116. .PaddingBottom(200)
  117. .AlignBottom()
  118. .Column(column =>
  119. {
  120. column.Item().Text("Quo Vadis", TextStyle.Default.Size(72).Bold().Color(Colors.Blue.Darken2));
  121. column.Item().Text("Henryk Sienkiewicz", TextStyle.Default.Size(24).Color(Colors.Grey.Darken2));
  122. });
  123. }
  124. void TableOfContents(IContainer container)
  125. {
  126. container.Column(column =>
  127. {
  128. SectionTitle(column, "Spis treści");
  129. foreach (var chapter in chapters)
  130. {
  131. column.Item().InternalLink(chapter.Title).Row(row =>
  132. {
  133. row.RelativeItem().Text(chapter.Title, normalStyle);
  134. row.ConstantItem(100).AlignRight().Text(text => text.PageNumberOfLocation(chapter.Title, normalStyle));
  135. });
  136. }
  137. });
  138. }
  139. void Chapters(ColumnDescriptor column)
  140. {
  141. foreach (var chapter in chapters)
  142. {
  143. column.Item().Element(container => Chapter(container, chapter.Title, chapter.Content));
  144. }
  145. }
  146. void Chapter(IContainer container, string title, string content)
  147. {
  148. container.Column(column =>
  149. {
  150. SectionTitle(column, title);
  151. column.Item().Text(text =>
  152. {
  153. text.ParagraphSpacing(5);
  154. text.Span(content, normalStyle);
  155. });
  156. column.Item().PageBreak();
  157. });
  158. }
  159. void Acknowledgements(IContainer container)
  160. {
  161. container.Column(column =>
  162. {
  163. SectionTitle(column, "Podziękowania");
  164. column.Item().Text(text =>
  165. {
  166. text.DefaultTextStyle(normalStyle);
  167. text.Span("Ten dokument został wygenerowany na podstawie książki w formacie TXT opublikowanej w serwisie ");
  168. text.ExternalLocation("wolnelektury.pl", "https://wolnelektury.pl/", normalStyle.Color(Colors.Blue.Medium).Underline());
  169. text.Span(". Dziękuję za wspieranie polskiego czytelnictwa!");
  170. });
  171. });
  172. }
  173. void SectionTitle(ColumnDescriptor column, string text)
  174. {
  175. column.Item().Location(text).Text(text, subtitleStyle);
  176. column.Item().PaddingTop(10).PaddingBottom(50).BorderBottom(1).BorderColor(Colors.Grey.Lighten2).ExtendHorizontal();
  177. }
  178. void Footer(IContainer container)
  179. {
  180. container
  181. .AlignCenter()
  182. .Text(text =>
  183. {
  184. text.CurrentPageNumber();
  185. text.Span(" / ");
  186. text.TotalPages();
  187. });
  188. }
  189. }
  190. }
  191. }