Debian Codegraph @

benchmarks/benchmarks.py

raw
1 from io import StringIO
2
3 from benchmarks import snippets
4 from rich.color import Color, ColorSystem
5 from rich.console import Console
6 from rich.pretty import Pretty
7 from rich.segment import Segment
8 from rich.style import Style
9 from rich.syntax import Syntax
10 from rich.table import Table
11 from rich.text import Text
12
13
14 class TextSuite:
15 def setup(self):
16 self.console = Console(
17 file=StringIO(), color_system="truecolor", legacy_windows=False
18 )
19 self.len_lorem_ipsum = len(snippets.LOREM_IPSUM)
20 self.text = Text.from_markup(snippets.MARKUP)
21
22 def time_wrapping(self):
23 self.text.wrap(self.console, 12, overflow="fold")
24
25 def time_indent_guides(self):
26 Text(snippets.PYTHON_SNIPPET).with_indent_guides()
27
28 def time_fit(self):
29 Text(snippets.LOREM_IPSUM).fit(12)
30
31 def time_split(self):
32 self.text.split()
33
34 def time_divide(self):
35 Text(snippets.LOREM_IPSUM).divide(range(20, 100, 4))
36
37 def time_align_center(self):
38 Text(snippets.LOREM_IPSUM).align("center", width=self.len_lorem_ipsum * 3)
39
40 def time_render(self):
41 list(self.text.render(self.console))
42
43 def time_wrapping_unicode_heavy(self):
44 Text(snippets.UNICODE_HEAVY_TEXT).wrap(self.console, 12, overflow="fold")
45
46 def time_fit_unicode_heavy(self):
47 Text(snippets.UNICODE_HEAVY_TEXT).fit(12)
48
49 def time_split_unicode_heavy(self):
50 Text(snippets.UNICODE_HEAVY_TEXT).split()
51
52 def time_divide_unicode_heavy(self):
53 self.text.divide(range(20, 100, 4))
54
55 def time_align_center_unicode_heavy(self):
56 Text(snippets.UNICODE_HEAVY_TEXT).align(
57 "center", width=self.len_lorem_ipsum * 3
58 )
59
60 def time_render_unicode_heavy(self):
61 list(Text(snippets.UNICODE_HEAVY_TEXT).render(self.console))
62
63
64 class TextHotCacheSuite:
65 def setup(self):
66 self.console = Console(
67 file=StringIO(), color_system="truecolor", legacy_windows=False
68 )
69
70 def time_wrapping_unicode_heavy_warm_cache(self):
71 for _ in range(20):
72 Text(snippets.UNICODE_HEAVY_TEXT).wrap(self.console, 12, overflow="fold")
73
74
75 class SyntaxWrappingSuite:
76 def setup(self):
77 self.console = Console(
78 file=StringIO(), color_system="truecolor", legacy_windows=False
79 )
80 self.syntax = Syntax(
81 code=snippets.PYTHON_SNIPPET, lexer="python", word_wrap=True
82 )
83
84 def time_text_thin_terminal_heavy_wrapping(self):
85 self._print_with_width(20)
86
87 def time_text_thin_terminal_medium_wrapping(self):
88 self._print_with_width(60)
89
90 def time_text_wide_terminal_no_wrapping(self):
91 self._print_with_width(100)
92
93 def _print_with_width(self, width):
94 self.console.print(self.syntax, width)
95
96
97 class TableSuite:
98 def time_table_no_wrapping(self):
99 self._print_table(width=100)
100
101 def time_table_heavy_wrapping(self):
102 self._print_table(width=30)
103
104 def _print_table(self, width):
105 table = Table(title="Star Wars Movies")
106 console = Console(
107 file=StringIO(), color_system="truecolor", legacy_windows=False, width=width
108 )
109 table.add_column("Released", justify="right", style="cyan", no_wrap=True)
110 table.add_column("Title", style="magenta")
111 table.add_column("Box Office", justify="right", style="green")
112 table.add_row(
113 "Dec 20, 2019", "[b]Star Wars[/]: The Rise of Skywalker", "$952,110,690"
114 )
115 table.add_row(
116 "May 25, 2018", "Solo: A [red][b]Star Wars[/] Story[/]", "$393,151,347"
117 )
118 table.add_row(
119 "Dec 15, 2017",
120 "[b red]Star Wars[/] Ep. V111: The Last Jedi",
121 "$1,332,539,889",
122 )
123 table.add_row(
124 "Dec 16, 2016", "Rogue One: A [blue]Star Wars[/] Story", "$1,332,439,889"
125 )
126 console.print(table)
127
128
129 class PrettySuite:
130 def setup(self):
131 self.console = Console(
132 file=StringIO(), color_system="truecolor", legacy_windows=False, width=100
133 )
134
135 def time_pretty(self):
136 pretty = Pretty(snippets.PYTHON_DICT)
137 self.console.print(pretty)
138
139 def time_pretty_indent_guides(self):
140 pretty = Pretty(snippets.PYTHON_DICT, indent_guides=True)
141 self.console.print(pretty)
142
143 def time_pretty_justify_center(self):
144 pretty = Pretty(snippets.PYTHON_DICT, justify="center")
145 self.console.print(pretty)
146
147
148 class StyleSuite:
149 def setup(self):
150 self.console = Console(
151 file=StringIO(), color_system="truecolor", legacy_windows=False, width=100
152 )
153 self.style1 = Style.parse("blue on red")
154 self.style2 = Style.parse("green italic bold")
155
156 def time_parse_ansi(self):
157 Style.parse("red on blue")
158
159 def time_parse_hex(self):
160 Style.parse("#f0f0f0 on #e2e28a")
161
162 def time_parse_mixed_complex_style(self):
163 Style.parse("dim bold reverse #00ee00 on rgb(123,12,50)")
164
165 def time_style_add(self):
166 self.style1 + self.style2
167
168
169 class ColorSuite:
170 def setup(self):
171 self.console = Console(
172 file=StringIO(), color_system="truecolor", legacy_windows=False, width=100
173 )
174 self.color = Color.parse("#0d1da0")
175
176 def time_downgrade_to_eight_bit(self):
177 self.color.downgrade(ColorSystem.EIGHT_BIT)
178
179 def time_downgrade_to_standard(self):
180 self.color.downgrade(ColorSystem.STANDARD)
181
182 def time_downgrade_to_windows(self):
183 self.color.downgrade(ColorSystem.WINDOWS)
184
185
186 class ColorSuiteCached:
187 def setup(self):
188 self.console = Console(
189 file=StringIO(), color_system="truecolor", legacy_windows=False, width=100
190 )
191 self.color = Color.parse("#0d1da0")
192 # Warm cache
193 self.color.downgrade(ColorSystem.EIGHT_BIT)
194 self.color.downgrade(ColorSystem.STANDARD)
195 self.color.downgrade(ColorSystem.WINDOWS)
196
197 def time_downgrade_to_eight_bit(self):
198 self.color.downgrade(ColorSystem.EIGHT_BIT)
199
200 def time_downgrade_to_standard(self):
201 self.color.downgrade(ColorSystem.STANDARD)
202
203 def time_downgrade_to_windows(self):
204 self.color.downgrade(ColorSystem.WINDOWS)
205
206
207 class SegmentSuite:
208 def setup(self):
209 self.line = [
210 Segment("foo"),
211 Segment("bar"),
212 Segment("egg"),
213 Segment("Where there is a Will"),
214 Segment("There is a way"),
215 ] * 2
216
217 def test_divide_complex(self):
218 list(Segment.divide(self.line, [5, 10, 20, 50, 108, 110, 118]))