extern crate test;
use test::{Bencher, black_box};
use super::{Decoder, DecoderFlags};
fn decoder() -> Decoder {
Decoder::new(DecoderFlags::empty())
}
fn bench_parse(b: &mut Bencher, input: &[u8]) {
let mut d = decoder();
b.iter(|| {
let mut rest = black_box(input);
let mut events = 0usize;
while !rest.is_empty() {
let (consumed, event) = d.parse_one(rest);
if consumed == 0 {
break;
}
black_box(&event);
rest = &rest[consumed..];
events += 1;
}
events
});
}
#[bench]
fn csi_cursor_up(b: &mut Bencher) {
bench_parse(b, b"\x1b[A");
}
#[bench]
fn csi_sgr_truecolor(b: &mut Bencher) {
bench_parse(b, b"\x1b[38:2::255:100:50m");
}
#[bench]
fn csi_sgr_semicolons(b: &mut Bencher) {
bench_parse(b, b"\x1b[0;1;38;5;214;48;5;236m");
}
#[bench]
fn csi_private_mode(b: &mut Bencher) {
bench_parse(b, b"\x1b[?2048h");
}
#[bench]
fn csi_intermediate(b: &mut Bencher) {
bench_parse(b, b"\x1b[2 q");
}
#[bench]
fn csi_sgr_mouse(b: &mut Bencher) {
bench_parse(b, b"\x1b[<0;10;20M");
}
#[bench]
fn csi_unrecognized(b: &mut Bencher) {
bench_parse(b, b"\x1b[99;99;99~");
}
#[bench]
fn dcs_decrpss_cursor_style(b: &mut Bencher) {
bench_parse(b, b"\x1bP1$r2 q\x1b\\");
}
#[bench]
fn dcs_xtgettcap(b: &mut Bencher) {
bench_parse(b, b"\x1bP1+r6B663133=1B5B313B3250\x1b\\");
}
#[bench]
fn mixed_stream(b: &mut Bencher) {
let mut input = Vec::new();
for i in 0..64 {
input.extend_from_slice(b"\x1b[0;1;32mhello \x1b[m");
input.extend_from_slice(b"\x1b[<35;12;7M");
input.extend_from_slice(format!("row {i}\r\n").as_bytes());
}
bench_parse(b, &input);
}
#[bench]
fn plain_text(b: &mut Bencher) {
bench_parse(b, b"the quick brown fox jumps over the lazy dog");
}