mirror of
https://github.com/storytold/texturecraft.git
synced 2026-11-24 00:10:45 +01:00
Check translation lookup behavior without timing
Account for 1c2f9175 by removing its standalone benchmark. Replace the lookup_is_fast wall-clock check (F2) with language-switch and missing-translation assertions in saved_language_codes_and_spanish_lookup, following PhotoCraft’s translation tests.
Verified all seven i18n tests with coverage instrumentation and pinned catalogs, formatting, layering, and strict UI Clippy.
This commit is contained in:
1 parent
1c2f91759b
commit
175e92dde6
3 files changed
+10
-58
No files matched your search
@@ -8,11 +8,6 @@ license.workspace = true
|
||||
[lints]
|
||||
workspace = true
|
||||
|
||||
[[bench]]
|
||||
name = "translation_lookup"
|
||||
harness = false
|
||||
test = false
|
||||
|
||||
[dependencies]
|
||||
chrono = { workspace = true }
|
||||
texturecraft-engine = { workspace = true }
|
||||
|
||||
@@ -1,45 +0,0 @@
|
||||
//! 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,17 +428,19 @@ mod tests",
|
||||
set_language(language.code);
|
||||
assert_eq!(current().code, if available(language.code) { language.code } else { "en" });
|
||||
}
|
||||
let key = String::from("Layers");
|
||||
const MISSING: &str = "missing test label";
|
||||
set_language("es");
|
||||
let label = tr(&key);
|
||||
let spanish = tr("Layers");
|
||||
let missing = tr(MISSING);
|
||||
set_language("en");
|
||||
let english = tr(&key);
|
||||
let english = tr("Layers");
|
||||
set_language("es");
|
||||
let reused = tr(&key);
|
||||
let spanish_again = tr("Layers");
|
||||
set_language("en");
|
||||
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));
|
||||
|
||||
assert_eq!(spanish, if available("es") { "Capas" } else { "Layers" });
|
||||
assert_eq!(missing, MISSING);
|
||||
assert_eq!(english, "Layers");
|
||||
assert_eq!(spanish_again, spanish);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user