use criterion::criterion_group; use criterion::criterion_main; use criterion::BenchmarkId; use criterion::Criterion; use std::collections::HashMap; use std::fs::File; use std::io; use std::io::Read; use std::rc::Rc;
use fluent_bundle::{FluentArgs, FluentBundle, FluentResource, FluentValue}; use fluent_syntax::ast; use unic_langid::langid;
fn read_file(path: &str) -> Result<String, io::Error> { letmut f = File::open(path)?; letmut s = String::new();
f.read_to_string(&mut s)?;
Ok(s)
}
fn get_strings(tests: &[&'static str]) -> HashMap<&'static str, String> { letmut ftl_strings = HashMap::new(); for test in tests { let path = format!("./benches/{}.ftl", test);
ftl_strings.insert(*test, read_file(&path).expect("Couldn't load file"));
} return ftl_strings;
}
fn add_functions<R>(name: &'static str, bundle: &mut FluentBundle<R>) { match name { "preferences" => {
bundle
.add_function("PLATFORM", |_args, _named_args| { return"linux".into();
})
.expect("Failed to add a function to the bundle.");
}
_ => {}
}
}
fn get_bundle(name: &'static str, source: &str) -> (FluentBundle<FluentResource>, Vec<String>) { let res = FluentResource::try_new(source.to_owned()).expect("Couldn't parse an FTL source"); let ids = get_ids(&res); let lids = vec![langid!("en")]; letmut bundle = FluentBundle::new(lids);
bundle
.add_resource(res)
.expect("Couldn't add FluentResource to the FluentBundle");
add_functions(name, &mut bundle);
(bundle, ids)
}
letmut group = c.benchmark_group("construct"); for name in tests { let source = ftl_strings.get(name).expect("Failed to find the source.");
group.bench_with_input(BenchmarkId::from_parameter(name), &source, |b, source| { let res = Rc::new(
FluentResource::try_new(source.to_string()).expect("Couldn't parse an FTL source"),
);
b.iter(|| { let lids = vec![langid!("en")]; letmut bundle = FluentBundle::new(lids);
bundle
.add_resource(res.clone())
.expect("Couldn't add FluentResource to the FluentBundle");
add_functions(name, &mut bundle);
})
});
}
group.finish();
letmut group = c.benchmark_group("resolve"); for name in tests { let source = ftl_strings.get(name).expect("Failed to find the source.");
group.bench_with_input(BenchmarkId::from_parameter(name), &source, |b, source| { let (bundle, ids) = get_bundle(name, source); let args = get_args(name);
b.iter(|| { letmut s = String::new(); for id in &ids { let msg = bundle.get_message(id).expect("Message found"); letmut errors = vec![]; iflet Some(value) = msg.value() { let _ = bundle.write_pattern(&mut s, value, args.as_ref(), &tyle='color:red'>mut errors);
s.clear();
} for attr in msg.attributes() { let _ =
bundle.write_pattern(&mut s, attr.value(), args.as_ref(), & style='color:red'>mut errors);
s.clear();
}
assert!(errors.len() == 0, "Resolver errors: {:#?}", errors);
}
})
});
}
group.finish();
letmut group = c.benchmark_group("resolve_to_str"); for name in tests { let source = ftl_strings.get(name).expect("Failed to find the source.");
group.bench_with_input(BenchmarkId::from_parameter(name), &source, |b, source| { let (bundle, ids) = get_bundle(name, source); let args = get_args(name);
b.iter(|| { for id in &ids { let msg = bundle.get_message(id).expect("Message found"); letmut errors = vec![]; iflet Some(value) = msg.value() { let _ = bundle.format_pattern(value, args.as_ref(), &mut errors);
} for attr in msg.attributes() { let _ = bundle.format_pattern(attr.value(), args.as_ref(), &muterrors);
}
assert!(errors.len() == 0, "Resolver errors: {:#?}", errors);
}
})
});
}
group.finish();
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.