mirror of
https://github.com/storytold/texturecraft.git
synced 2026-11-24 00:10:45 +01:00
Measure translation lookup speed in a standalone benchmark
Move i18n::tests::lookup_is_fast to the translation_lookup benchmark (F2). Keep the 1,000 ns budget for warm lookups over the embedded Japanese catalog. Verify borrowed translations and the English fallback deterministically in the language-switch unit test.
This commit is contained in:
1 parent
30b1f3b938
commit
1c2f91759b
3 files changed
+59
-20
No files matched your search
@@ -8,6 +8,11 @@ license.workspace = true
|
||||
[lints]
|
||||
workspace = true
|
||||
|
||||
[[bench]]
|
||||
name = "translation_lookup"
|
||||
harness = false
|
||||
test = false
|
||||
|
||||
[dependencies]
|
||||
chrono = { workspace = true }
|
||||
texturecraft-engine = { workspace = true }
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
//! Time warm lookups with the Japanese catalog embedded by the regular build.
|
||||
//! With `CRAFT_I18N_DIR` pointing at the pinned catalog checkout, run
|
||||
//! `cargo bench -p texturecraft-ui-egui --bench translation_lookup --profile dev`.
|
||||
|
||||
use std::{hint::black_box, time::Instant};
|
||||
|
||||
use texturecraft_ui_egui::i18n::{set_language, tr};
|
||||
|
||||
include!(concat!(env!("OUT_DIR"), "/craft_i18n.rs"));
|
||||
|
||||
fn main() -> Result<(), String> {
|
||||
// Cargo can run this binary under test --all-targets too. Only bench passes --bench.
|
||||
if !std::env::args().any(|arg| arg == "--bench") {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let &(_, shared, app) = CRAFT_I18N.iter().find(|(code, _, _)| *code == "ja").ok_or("set CRAFT_I18N_DIR to the pinned craft-i18n checkout")?;
|
||||
|
||||
// The plain rows with literal English keys are representative of the labels tr() looks up.
|
||||
let mut keys: Vec<_> = shared
|
||||
.lines()
|
||||
.chain(app.lines())
|
||||
.filter_map(|row| row.strip_prefix('\t')?.split_once('\t').map(|(key, _)| key))
|
||||
.filter(|key| !key.is_empty() && !key.contains('\\'))
|
||||
.collect();
|
||||
keys.sort_unstable();
|
||||
keys.dedup();
|
||||
if keys.is_empty() {
|
||||
return Err("the Japanese catalog has no plain translation keys".into());
|
||||
}
|
||||
|
||||
set_language("ja");
|
||||
black_box(tr(keys[0]));
|
||||
const ROUNDS: usize = 100;
|
||||
let started = Instant::now();
|
||||
for _ in 0..ROUNDS {
|
||||
for key in &keys {
|
||||
black_box(tr(black_box(key)));
|
||||
}
|
||||
}
|
||||
let ns_per_lookup = started.elapsed().as_secs_f64() * 1e9 / (ROUNDS * keys.len()) as f64;
|
||||
println!("{ns_per_lookup:.0} ns per lookup across {} Japanese keys", keys.len());
|
||||
assert!(ns_per_lookup < 1000.0, "{ns_per_lookup:.0} ns per lookup exceeded the 1000 ns budget");
|
||||
Ok(())
|
||||
}
|
||||
@@ -428,28 +428,17 @@ mod tests",
|
||||
set_language(language.code);
|
||||
assert_eq!(current().code, if available(language.code) { language.code } else { "en" });
|
||||
}
|
||||
let key = String::from("Layers");
|
||||
set_language("es");
|
||||
assert_eq!(tr("Layers"), if available("es") { "Capas" } else { "Layers" });
|
||||
let label = tr(&key);
|
||||
set_language("en");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn lookup_is_fast() {
|
||||
let _lock = LANGUAGE.lock().unwrap_or_else(std::sync::PoisonError::into_inner);
|
||||
let keys = keys();
|
||||
set_language("ja");
|
||||
let started = std::time::Instant::now();
|
||||
let mut total_chars = 0usize;
|
||||
for _ in 0..100 {
|
||||
for key in &keys {
|
||||
total_chars += tr(key).len();
|
||||
}
|
||||
}
|
||||
let ns_per_lookup = started.elapsed().as_secs_f64() * 1e9 / (100 * keys.len()) as f64;
|
||||
let english = tr(&key);
|
||||
set_language("es");
|
||||
let reused = tr(&key);
|
||||
set_language("en");
|
||||
assert!(total_chars > 0);
|
||||
|
||||
// A frame looks up a few hundred strings, each expected below one microsecond
|
||||
assert!(ns_per_lookup < 1000.0, "{ns_per_lookup:.0} ns per lookup");
|
||||
assert_eq!(label, if available("es") { "Capas" } else { "Layers" });
|
||||
assert!(std::ptr::eq(english, key.as_str()));
|
||||
// Switching away and back reuses the translation already stored in the catalog.
|
||||
assert!(std::ptr::eq(reused, label));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user