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