Files
telegram-tui/benches/format_markdown.rs
Mikhail Kilin 264f183510
Some checks failed
ci/woodpecker/pr/check Pipeline failed
CI / Check (pull_request) Has been cancelled
CI / Format (pull_request) Has been cancelled
CI / Clippy (pull_request) Has been cancelled
CI / Build (macos-latest) (pull_request) Has been cancelled
CI / Build (ubuntu-latest) (pull_request) Has been cancelled
CI / Build (windows-latest) (pull_request) Has been cancelled
style: auto-format entire codebase with cargo fmt (stable rustfmt.toml)
2026-02-22 17:09:51 +03:00

87 lines
2.5 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use tdlib_rs::enums::{TextEntity, TextEntityType};
use tele_tui::formatting::format_text_with_entities;
fn create_text_with_entities() -> (String, Vec<TextEntity>) {
let text = "This is bold and italic text with code and a link and mention".to_string();
let entities = vec![
TextEntity {
offset: 8,
length: 4, // bold
type_: TextEntityType::Bold,
},
TextEntity {
offset: 17,
length: 6, // italic
type_: TextEntityType::Italic,
},
TextEntity {
offset: 34,
length: 4, // code
type_: TextEntityType::Code,
},
TextEntity {
offset: 45,
length: 4, // link
type_: TextEntityType::Url,
},
TextEntity {
offset: 54,
length: 7, // mention
type_: TextEntityType::Mention,
},
];
(text, entities)
}
fn benchmark_format_simple_text(c: &mut Criterion) {
let text = "Simple text without any formatting".to_string();
let entities = vec![];
c.bench_function("format_simple_text", |b| {
b.iter(|| format_text_with_entities(black_box(&text), black_box(&entities)));
});
}
fn benchmark_format_markdown_text(c: &mut Criterion) {
let (text, entities) = create_text_with_entities();
c.bench_function("format_markdown_text", |b| {
b.iter(|| format_text_with_entities(black_box(&text), black_box(&entities)));
});
}
fn benchmark_format_long_text(c: &mut Criterion) {
let mut text = String::new();
let mut entities = vec![];
// Создаем длинный текст с множеством форматирований
for i in 0..100 {
let start = text.len();
text.push_str(&format!("Word{} ", i));
// Добавляем форматирование к каждому 3-му слову
if i % 3 == 0 {
entities.push(TextEntity {
offset: start as i32,
length: format!("Word{}", i).len() as i32,
type_: TextEntityType::Bold,
});
}
}
c.bench_function("format_long_text_with_100_entities", |b| {
b.iter(|| format_text_with_entities(black_box(&text), black_box(&entities)));
});
}
criterion_group!(
benches,
benchmark_format_simple_text,
benchmark_format_markdown_text,
benchmark_format_long_text
);
criterion_main!(benches);