Skip to main content

max / pter

3.2 KB · 106 lines History Blame Raw
1 use criterion::{Criterion, criterion_group, criterion_main};
2 use std::fmt::Write as _;
3 use std::hint::black_box;
4
5 fn simple_email() -> &'static str {
6 r#"<html><body>
7 <h1>Meeting Tomorrow</h1>
8 <p>Hi Max,</p>
9 <p>Just confirming our meeting tomorrow at <strong>2pm</strong>.
10 Please review the <a href="https://example.com/doc">document</a> beforehand.</p>
11 <p>Best,<br>Alice</p>
12 </body></html>"#
13 }
14
15 fn newsletter_email() -> &'static str {
16 r#"<html><body>
17 <table width="100%" cellpadding="0" cellspacing="0" role="presentation">
18 <tr><td align="center">
19 <table width="600" cellpadding="0" cellspacing="0">
20 <tr><td>
21 <h2>Weekly Digest</h2>
22 <p>Here are your updates:</p>
23 <ul>
24 <li>New feature: <strong>Dark mode</strong> is now available</li>
25 <li>Bug fix: Resolved <a href="https://example.com/issue/123">issue #123</a></li>
26 <li>Update: API v2 documentation published</li>
27 </ul>
28 <p>Thanks for reading!</p>
29 <hr>
30 <p><small>Unsubscribe: <a href="https://example.com/unsub">click here</a></small></p>
31 </td></tr>
32 </table>
33 </td></tr>
34 </table>
35 <img src="https://track.example.com/open.gif" width="1" height="1">
36 </body></html>"#
37 }
38
39 fn reply_chain() -> &'static str {
40 r#"<html><body>
41 <div dir="ltr"><p>Got it, thanks!</p></div>
42 <div class="gmail_quote">
43 <div class="gmail_attr">On Tue, Jan 6, Bob wrote:</div>
44 <blockquote class="gmail_quote">
45 <div dir="ltr"><p>Here's the update you requested.</p></div>
46 <div class="gmail_quote">
47 <div class="gmail_attr">On Mon, Jan 5, Alice wrote:</div>
48 <blockquote class="gmail_quote">
49 <div dir="ltr"><p>What's the status on the deployment?</p></div>
50 </blockquote>
51 </div>
52 </blockquote>
53 </div>
54 </body></html>"#
55 }
56
57 fn large_email() -> String {
58 let paragraph = "<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. \
59 Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. \
60 Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris.</p>";
61 let mut html = String::from("<html><body>");
62 for i in 0..100 {
63 let _ = write!(html, "<h3>Section {i}</h3>");
64 html.push_str(paragraph);
65 }
66 html.push_str("</body></html>");
67 html
68 }
69
70 fn bench_simple(c: &mut Criterion) {
71 let html = simple_email();
72 c.bench_function("simple_email", |b| {
73 b.iter(|| pter::convert(black_box(html)));
74 });
75 }
76
77 fn bench_newsletter(c: &mut Criterion) {
78 let html = newsletter_email();
79 c.bench_function("newsletter_layout_tables", |b| {
80 b.iter(|| pter::convert(black_box(html)));
81 });
82 }
83
84 fn bench_reply_chain(c: &mut Criterion) {
85 let html = reply_chain();
86 c.bench_function("reply_chain_nested", |b| {
87 b.iter(|| pter::convert(black_box(html)));
88 });
89 }
90
91 fn bench_large(c: &mut Criterion) {
92 let html = large_email();
93 c.bench_function("large_100_sections", |b| {
94 b.iter(|| pter::convert(black_box(&html)));
95 });
96 }
97
98 criterion_group!(
99 benches,
100 bench_simple,
101 bench_newsletter,
102 bench_reply_chain,
103 bench_large
104 );
105 criterion_main!(benches);
106