TextBenchmark.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. .RenderDocument(x => ComposeBook(x, chapters));
  25. }
  26. [Test]
  27. public void Benchmark()
  28. {
  29. var chapters = GetBookChapters().ToList();
  30. var results = PerformTest(128).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. .RenderDocument(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(IDocumentContainer 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. container.Page(page =>
  92. {
  93. page.Margin(50);
  94. page.Content().Column(column =>
  95. {
  96. column.Item().Element(Title);
  97. column.Item().PageBreak();
  98. column.Item().Element(TableOfContents);
  99. column.Item().PageBreak();
  100. Chapters(column);
  101. column.Item().Element(Acknowledgements);
  102. });
  103. page.Footer().Element(Footer);
  104. });
  105. void Title(IContainer container)
  106. {
  107. container
  108. .Extend()
  109. .PaddingBottom(200)
  110. .AlignBottom()
  111. .Column(column =>
  112. {
  113. column.Item().Text("Quo Vadis").FontSize(72).Bold().FontColor(Colors.Blue.Darken2);
  114. column.Item().Text("Henryk Sienkiewicz").FontSize(24).FontColor(Colors.Grey.Darken2);
  115. });
  116. }
  117. void TableOfContents(IContainer container)
  118. {
  119. container.Column(column =>
  120. {
  121. SectionTitle(column, "Spis treści");
  122. foreach (var chapter in chapters)
  123. {
  124. column.Item().InternalLink(chapter.Title).Row(row =>
  125. {
  126. row.RelativeItem().Text(chapter.Title).Style(normalStyle);
  127. row.ConstantItem(100).AlignRight().Text(text => text.BeginPageNumberOfSection(chapter.Title).Style(normalStyle));
  128. });
  129. }
  130. });
  131. }
  132. void Chapters(ColumnDescriptor column)
  133. {
  134. foreach (var chapter in chapters)
  135. {
  136. column.Item().Element(container => Chapter(container, chapter.Title, chapter.Content));
  137. }
  138. }
  139. void Chapter(IContainer container, string title, string content)
  140. {
  141. container.Column(column =>
  142. {
  143. SectionTitle(column, title);
  144. column.Item().Text(text =>
  145. {
  146. text.ParagraphSpacing(5);
  147. text.Span(content).Style(normalStyle);
  148. });
  149. column.Item().PageBreak();
  150. });
  151. }
  152. void Acknowledgements(IContainer container)
  153. {
  154. container.Column(column =>
  155. {
  156. SectionTitle(column, "Podziękowania");
  157. column.Item().Text(text =>
  158. {
  159. text.DefaultTextStyle(normalStyle);
  160. text.Span("Ten dokument został wygenerowany na podstawie książki w formacie TXT opublikowanej w serwisie ");
  161. text.Hyperlink("wolnelektury.pl", "https://wolnelektury.pl/").FontColor(Colors.Blue.Medium).Underline();
  162. text.Span(". Dziękuję za wspieranie polskiego czytelnictwa!");
  163. });
  164. });
  165. }
  166. void SectionTitle(ColumnDescriptor column, string text)
  167. {
  168. column.Item().Location(text).Text(text).Style(subtitleStyle);
  169. column.Item().PaddingTop(10).PaddingBottom(50).BorderBottom(1).BorderColor(Colors.Grey.Lighten2).ExtendHorizontal();
  170. }
  171. void Footer(IContainer container)
  172. {
  173. container
  174. .AlignCenter()
  175. .Text(text =>
  176. {
  177. text.CurrentPageNumber();
  178. text.Span(" / ");
  179. text.TotalPages();
  180. });
  181. }
  182. }
  183. }
  184. }