Skip to content

Commit 1fd1195

Browse files
committed
bench++
1 parent e8baf2b commit 1fd1195

9 files changed

Lines changed: 165 additions & 22 deletions

File tree

Cargo.toml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,14 @@ hickory-server = "0.25.1"
123123
name = "process"
124124
harness = false
125125

126+
[[bench]]
127+
name = "contentviews"
128+
harness = false
129+
130+
[[bench]]
131+
name = "syntax_highlight"
132+
harness = false
133+
126134
[profile.release]
127135
codegen-units = 1
128136
lto = true

benches/contentviews.rs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
use criterion::{black_box, criterion_group, criterion_main, Criterion};
2+
use mitmproxy::contentviews;
3+
use mitmproxy::contentviews::{Prettify, Reencode, TestMetadata};
4+
5+
fn criterion_benchmark(c: &mut Criterion) {
6+
c.bench_function("protobuf-prettify", |b| {
7+
b.iter(|| {
8+
contentviews::Protobuf.prettify(black_box(b"\n\x13gRPC testing server\x12\x07\n\x05Index\x12\x07\n\x05Empty\x12\x0c\n\nDummyUnary\x12\x0f\n\rSpecificError\x12\r\n\x0bRandomError\x12\x0e\n\x0cHeadersUnary\x12\x11\n\x0fNoResponseUnary"), &TestMetadata::default()).unwrap()
9+
})
10+
});
11+
12+
c.bench_function("protobuf-reencode", |b| {
13+
b.iter(|| {
14+
contentviews::Protobuf.reencode(
15+
black_box("1: gRPC testing server\n2:\n- 1: Index\n- 1: Empty\n- 1: DummyUnary\n- 1: SpecificError\n- 1: RandomError\n- 1: HeadersUnary\n- 1: NoResponseUnary\n"),
16+
&TestMetadata::default()
17+
).unwrap()
18+
})
19+
});
20+
21+
const TEST_MSGPACK: &[u8] = &[
22+
0x83, // map with 3 elements
23+
0xa4, 0x6e, 0x61, 0x6d, 0x65, // "name"
24+
0xa8, 0x4a, 0x6f, 0x68, 0x6e, 0x20, 0x44, 0x6f, 0x65, // "John Doe"
25+
0xa3, 0x61, 0x67, 0x65, // "age"
26+
0x1e, // 30
27+
0xa4, 0x74, 0x61, 0x67, 0x73, // "tags"
28+
0x92, // array with 2 elements
29+
0xa9, 0x64, 0x65, 0x76, 0x65, 0x6c, 0x6f, 0x70, 0x65, 0x72, // "developer"
30+
0xa4, 0x72, 0x75, 0x73, 0x74, // "rust"
31+
];
32+
c.bench_function("msgpack-prettify", |b| {
33+
b.iter(|| {
34+
contentviews::MsgPack
35+
.prettify(black_box(TEST_MSGPACK), &TestMetadata::default())
36+
.unwrap()
37+
})
38+
});
39+
40+
c.bench_function("msgpack-reencode", |b| {
41+
b.iter(|| {
42+
contentviews::MsgPack
43+
.reencode(
44+
black_box(
45+
"\
46+
name: John Doe\n\
47+
age: 30\n\
48+
tags:\n\
49+
- developer\n\
50+
- rust\n\
51+
",
52+
),
53+
&TestMetadata::default(),
54+
)
55+
.unwrap()
56+
})
57+
});
58+
}
59+
60+
criterion_group!(benches, criterion_benchmark);
61+
criterion_main!(benches);

benches/syntax_highlight.rs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
use criterion::{black_box, criterion_group, criterion_main, Criterion};
2+
use mitmproxy::syntax_highlight;
3+
fn criterion_benchmark(c: &mut Criterion) {
4+
c.bench_function("syntax_highlight small", |b| {
5+
b.iter(|| {
6+
syntax_highlight::Language::Xml
7+
.highlight(black_box(
8+
br#"
9+
<!doctype html>
10+
<html lang="en">
11+
<head>
12+
<meta charset="utf-8">
13+
<meta name="viewport" content="width=device-width, initial-scale=1">
14+
<title>Bootstrap demo</title>
15+
</head>
16+
<body>
17+
<h1>Hello, world!</h1>
18+
</body>
19+
</html>"#,
20+
))
21+
.unwrap()
22+
})
23+
});
24+
25+
let data = "<a>x".repeat(8096);
26+
c.bench_function("syntax_highlight xml", |b| {
27+
b.iter(|| {
28+
syntax_highlight::Language::Xml
29+
.highlight(black_box(data.as_bytes()))
30+
.unwrap()
31+
})
32+
});
33+
34+
// tree_sitter_html is faster, but not by orders of magnitude.
35+
/*
36+
let mut config = HighlightConfiguration::new(
37+
tree_sitter_html::LANGUAGE.into(),
38+
"",
39+
tree_sitter_html::HIGHLIGHTS_QUERY,
40+
"",
41+
""
42+
).unwrap();
43+
let names = config.names().iter().map(|x| x.to_string()).collect::<Vec<String>>();
44+
let tags = names.iter().map(|_| Tag::Text).collect::<Vec<Tag>>();
45+
config.configure(&names);
46+
47+
c.bench_function("syntax_highlight html", |b| {
48+
b.iter(|| {
49+
common::highlight(
50+
&config,
51+
&tags,
52+
data.as_bytes(),
53+
)
54+
})
55+
});
56+
*/
57+
}
58+
59+
criterion_group!(benches, criterion_benchmark);
60+
criterion_main!(benches);

src/contentviews/mod.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,11 @@ pub trait Reencode: Send + Sync {
3939
fn reencode(&self, data: &str, metadata: &dyn Metadata) -> Result<Vec<u8>>;
4040
}
4141

42-
#[cfg(test)]
4342
#[derive(Default)]
4443
pub struct TestMetadata {
4544
pub content_type: Option<String>,
4645
}
4746

48-
#[cfg(test)]
4947
impl Metadata for TestMetadata {
5048
fn content_type(&self) -> Option<String> {
5149
self.content_type.clone()

src/contentviews/msgpack.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use crate::contentviews::{Metadata, Prettify, Reencode};
2+
use crate::syntax_highlight::Language;
23
use anyhow::{Context, Result};
34
use rmp_serde::{decode, encode};
45
use serde_yaml;
@@ -10,6 +11,10 @@ impl Prettify for MsgPack {
1011
"MsgPack"
1112
}
1213

14+
fn syntax_highlight(&self) -> Language {
15+
Language::Yaml
16+
}
17+
1318
fn prettify(&self, data: &[u8], _metadata: &dyn Metadata) -> Result<String> {
1419
// Deserialize MsgPack to a serde_yaml::Value
1520
let value: serde_yaml::Value =

src/syntax_highlight/common.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,13 @@ use anyhow::{Context, Result};
33
use tree_sitter_highlight::{HighlightConfiguration, HighlightEvent, Highlighter};
44

55
pub fn highlight(
6-
language: tree_sitter::Language,
7-
highlights_query: &str,
8-
names: &[&str],
6+
config: &HighlightConfiguration,
97
tags: &[Tag],
108
input: &[u8],
119
) -> Result<Vec<Chunk>> {
1210
let mut highlighter = Highlighter::new();
13-
let mut config = HighlightConfiguration::new(language, "", highlights_query, "", "")
14-
.context("failed to create highlight configuration")?;
15-
config.configure(names);
16-
1711
let highlights = highlighter
18-
.highlight(&config, input, None, |_| None)
12+
.highlight(config, input, None, |_| None)
1913
.context("failed to highlight")?;
2014

2115
let mut chunks: Vec<Chunk> = Vec::new();

src/syntax_highlight/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::fmt;
33
use std::fmt::Formatter;
44
use std::str::FromStr;
55

6-
mod common;
6+
pub mod common;
77
mod xml;
88
mod yaml;
99

src/syntax_highlight/xml.rs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use super::common::highlight;
22
use super::{Chunk, Tag};
33
use anyhow::Result;
4+
use std::sync::LazyLock;
5+
use tree_sitter_highlight::HighlightConfiguration;
46

57
const NAMES: &[&str] = &[
68
"tag", // <div>
@@ -19,15 +21,21 @@ const TAGS: &[Tag] = &[
1921
Tag::Text, // markup
2022
];
2123

22-
pub fn highlight_xml(input: &[u8]) -> Result<Vec<Chunk>> {
23-
// There also is tree_sitter_xml, but tree_sitter_html produces slightly nicer output for us.
24-
highlight(
24+
static XML_CONFIG: LazyLock<HighlightConfiguration> = LazyLock::new(|| {
25+
let mut config = HighlightConfiguration::new(
2526
tree_sitter_xml::LANGUAGE_XML.into(),
27+
"",
2628
tree_sitter_xml::XML_HIGHLIGHT_QUERY,
27-
NAMES,
28-
TAGS,
29-
input,
29+
"",
30+
"",
3031
)
32+
.expect("failed to build XML syntax highlighter");
33+
config.configure(NAMES);
34+
config
35+
});
36+
37+
pub fn highlight_xml(input: &[u8]) -> Result<Vec<Chunk>> {
38+
highlight(&XML_CONFIG, TAGS, input)
3139
}
3240

3341
#[cfg(test)]

src/syntax_highlight/yaml.rs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use super::common::highlight;
22
use super::{Chunk, Tag};
33
use anyhow::Result;
4+
use std::sync::LazyLock;
5+
use tree_sitter_highlight::HighlightConfiguration;
46

57
const NAMES: &[&str] = &[
68
"boolean", // YAML booleans
@@ -19,14 +21,21 @@ const TAGS: &[Tag] = &[
1921
Tag::Name,
2022
];
2123

22-
pub fn highlight_yaml(input: &[u8]) -> Result<Vec<Chunk>> {
23-
highlight(
24+
static YAML_CONFIG: LazyLock<HighlightConfiguration> = LazyLock::new(|| {
25+
let mut config = HighlightConfiguration::new(
2426
tree_sitter_yaml::LANGUAGE.into(),
27+
"",
2528
tree_sitter_yaml::HIGHLIGHTS_QUERY,
26-
NAMES,
27-
TAGS,
28-
input,
29+
"",
30+
"",
2931
)
32+
.expect("failed to build YAML syntax highlighter");
33+
config.configure(NAMES);
34+
config
35+
});
36+
37+
pub fn highlight_yaml(input: &[u8]) -> Result<Vec<Chunk>> {
38+
highlight(&YAML_CONFIG, TAGS, input)
3039
}
3140

3241
#[cfg(test)]

0 commit comments

Comments
 (0)