extern crate test;
use test::{Bencher, black_box};
use crate::ansi::text::{Token, WidthMode, string_width, tokenize};
use crate::ansi::wrap::{DEFAULT_BREAKPOINTS, hardwrap, wordwrap, wrap};
const BIG: usize = 250_000;
fn ascii(n: usize) -> String {
const UNIT: &str = "the quick brown fox jumps over the lazy dog ";
std::iter::repeat_n(UNIT, n / UNIT.len()).collect()
}
fn mixed(n: usize) -> String {
let unit = "hello \u{4e00}\u{4e16} caf\u{e9} e\u{301} \u{1f600} ";
std::iter::repeat_n(unit, n / unit.len()).collect()
}
fn one_long_line(n: usize) -> String {
std::iter::repeat_n('x', n).collect()
}
#[bench]
fn width_ascii_250k(b: &mut Bencher) {
let s = ascii(BIG);
b.iter(|| black_box(string_width(black_box(s.as_bytes()), WidthMode::Wc, false)));
}
#[bench]
fn width_mixed_250k(b: &mut Bencher) {
let s = mixed(BIG);
b.iter(|| black_box(string_width(black_box(s.as_bytes()), WidthMode::Wc, false)));
}
#[bench]
fn width_grapheme_mixed_250k(b: &mut Bencher) {
let s = mixed(BIG);
b.iter(|| {
black_box(string_width(
black_box(s.as_bytes()),
WidthMode::Grapheme,
false,
))
});
}
#[bench]
fn width_one_32k_line(b: &mut Bencher) {
let s = one_long_line(32_000);
b.iter(|| {
black_box(string_width(
black_box(s.as_bytes()),
WidthMode::Grapheme,
false,
))
});
}
#[bench]
fn tokenize_32k_malformed(b: &mut Bencher) {
let bytes: Vec<u8> = std::iter::repeat_n([0xc2u8, 0x41], 16_000)
.flatten()
.collect();
b.iter(|| {
let mut n = 0usize;
for t in tokenize(black_box(&bytes), WidthMode::Wc, false) {
n += match t {
Token::Text { text, width } => text.len() + width as usize,
Token::Escape(seq) => seq.len(),
Token::Control(_) => 1,
};
}
black_box(n)
});
}
#[bench]
fn wrap_ascii_250k(b: &mut Bencher) {
let s = ascii(BIG);
b.iter(|| black_box(wrap(black_box(&s), 80, DEFAULT_BREAKPOINTS)));
}
#[bench]
fn wrap_mixed_250k(b: &mut Bencher) {
let s = mixed(BIG);
b.iter(|| black_box(wrap(black_box(&s), 80, DEFAULT_BREAKPOINTS)));
}
#[bench]
fn wrap_all_hard_250k(b: &mut Bencher) {
let s: String = std::iter::repeat_n("abcdefghij", BIG / 10).collect();
b.iter(|| black_box(wrap(black_box(&s), 80, DEFAULT_BREAKPOINTS)));
}
#[bench]
fn wrap_one_32k_word(b: &mut Bencher) {
let s = one_long_line(32_000);
b.iter(|| black_box(wrap(black_box(&s), 80, DEFAULT_BREAKPOINTS)));
}
#[bench]
fn wordwrap_ascii_250k(b: &mut Bencher) {
let s = ascii(BIG);
b.iter(|| black_box(wordwrap(black_box(&s), 80, DEFAULT_BREAKPOINTS)));
}
#[bench]
fn hardwrap_ascii_250k(b: &mut Bencher) {
let s = ascii(BIG);
b.iter(|| black_box(hardwrap(black_box(&s), 80, true)));
}
#[bench]
fn width_escape_dense_250k(b: &mut Bencher) {
let s: String = std::iter::repeat_n("a\x1b[31m", BIG / 6).collect();
b.iter(|| black_box(string_width(black_box(s.as_bytes()), WidthMode::Wc, false)));
}
#[bench]
fn width_cjk_250k(b: &mut Bencher) {
let s: String = std::iter::repeat_n('\u{4e00}', BIG / 3).collect();
b.iter(|| black_box(string_width(black_box(s.as_bytes()), WidthMode::Wc, false)));
}
#[bench]
fn width_emoji_250k(b: &mut Bencher) {
let s: String = std::iter::repeat_n('\u{1f600}', BIG / 4).collect();
b.iter(|| {
black_box(string_width(
black_box(s.as_bytes()),
WidthMode::Grapheme,
false,
))
});
}
#[bench]
fn width_styled_250k(b: &mut Bencher) {
const UNIT: &str = "\x1b[31mword\x1b[0m ";
let s: String = std::iter::repeat_n(UNIT, BIG / UNIT.len()).collect();
b.iter(|| black_box(string_width(black_box(s.as_bytes()), WidthMode::Wc, false)));
}