Skip to content

Commit

Permalink
Update benchmark menubar.ftl and add preferences.ftl
Browse files Browse the repository at this point in the history
  • Loading branch information
Zibi Braniecki committed Mar 11, 2019
1 parent a5537be commit ec8b223
Show file tree
Hide file tree
Showing 9 changed files with 2,916 additions and 1,117 deletions.
570 changes: 255 additions & 315 deletions fluent-bundle/benches/menubar.ftl

Large diffs are not rendered by default.

1,076 changes: 1,076 additions & 0 deletions fluent-bundle/benches/preferences.ftl

Large diffs are not rendered by default.

46 changes: 43 additions & 3 deletions fluent-bundle/benches/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::fs::File;
use std::io;
use std::io::Read;

use fluent_bundle::{FluentBundle, FluentResource};
use fluent_bundle::{FluentBundle, FluentResource, FluentValue};
use fluent_syntax::ast;

fn read_file(path: &str) -> Result<String, io::Error> {
Expand Down Expand Up @@ -38,8 +38,41 @@ fn get_ids(res: &FluentResource) -> Vec<String> {
.collect()
}

fn get_args(name: &str) -> Option<HashMap<&'static str, FluentValue>> {
match name {
"preferences" => {
let mut prefs_args = HashMap::new();
prefs_args.insert("name", FluentValue::from("John"));
prefs_args.insert("tabCount", FluentValue::from(5));
prefs_args.insert("count", FluentValue::from(3));
prefs_args.insert("version", FluentValue::from("65.0"));
prefs_args.insert("path", FluentValue::from("/tmp"));
prefs_args.insert("num", FluentValue::from(4));
prefs_args.insert("email", FluentValue::from("john@doe.com"));
prefs_args.insert("value", FluentValue::from(4.5));
prefs_args.insert("unit", FluentValue::from("mb"));
prefs_args.insert("service-name", FluentValue::from("Mozilla Disk"));
Some(prefs_args)
}
_ => None,
}
}

fn add_functions(name: &'static str, bundle: &mut FluentBundle) {
match name {
"preferences" => {
bundle
.add_function("PLATFORM", |_args, _named_args| {
return Some("linux".into());
})
.expect("Failed to add a function to the bundle.");
}
_ => {}
}
}

fn resolver_bench(c: &mut Criterion) {
let tests = &["simple", "menubar", "unescape"];
let tests = &["simple", "preferences", "menubar", "unescape"];
let ftl_strings = get_strings(tests);

c.bench_function_over_inputs(
Expand All @@ -53,9 +86,16 @@ fn resolver_bench(c: &mut Criterion) {
bundle
.add_resource(&res)
.expect("Couldn't add FluentResource to the FluentBundle");
add_functions(name, &mut bundle);
let args = get_args(name);

b.iter(|| {
for id in &ids {
bundle.compound(id, None);
let (_msg, errors) = bundle.compound(id, args.as_ref()).expect("Message found");
if !errors.is_empty() {
println!("{:#?}", errors);
}
assert!(errors.len() == 0);
}
})
},
Expand Down
100 changes: 100 additions & 0 deletions fluent-bundle/benches/simple.ftl
Original file line number Diff line number Diff line change
@@ -1,2 +1,102 @@
# Artificial testcase with 100 simple Fluent Messages

key0 = Value 0
key1 = Value 1
key2 = Value 2
key3 = Value 3
key4 = Value 4
key5 = Value 5
key6 = Value 6
key7 = Value 7
key8 = Value 8
key9 = Value 9
key10 = Value 10
key11 = Value 11
key12 = Value 12
key13 = Value 13
key14 = Value 14
key15 = Value 15
key16 = Value 16
key17 = Value 17
key18 = Value 18
key19 = Value 19
key20 = Value 20
key21 = Value 21
key22 = Value 22
key23 = Value 23
key24 = Value 24
key25 = Value 25
key26 = Value 26
key27 = Value 27
key28 = Value 28
key29 = Value 29
key30 = Value 30
key31 = Value 31
key32 = Value 32
key33 = Value 33
key34 = Value 34
key35 = Value 35
key36 = Value 36
key37 = Value 37
key38 = Value 38
key39 = Value 39
key40 = Value 40
key41 = Value 41
key42 = Value 42
key43 = Value 43
key44 = Value 44
key45 = Value 45
key46 = Value 46
key47 = Value 47
key48 = Value 48
key49 = Value 49
key50 = Value 50
key51 = Value 51
key52 = Value 52
key53 = Value 53
key54 = Value 54
key55 = Value 55
key56 = Value 56
key57 = Value 57
key58 = Value 58
key59 = Value 59
key60 = Value 60
key61 = Value 61
key62 = Value 62
key63 = Value 63
key64 = Value 64
key65 = Value 65
key66 = Value 66
key67 = Value 67
key68 = Value 68
key69 = Value 69
key70 = Value 70
key71 = Value 71
key72 = Value 72
key73 = Value 73
key74 = Value 74
key75 = Value 75
key76 = Value 76
key77 = Value 77
key78 = Value 78
key79 = Value 79
key80 = Value 80
key81 = Value 81
key82 = Value 82
key83 = Value 83
key84 = Value 84
key85 = Value 85
key86 = Value 86
key87 = Value 87
key88 = Value 88
key89 = Value 89
key90 = Value 90
key91 = Value 91
key92 = Value 92
key93 = Value 93
key94 = Value 94
key95 = Value 95
key96 = Value 96
key97 = Value 97
key98 = Value 98
key99 = Value 99
17 changes: 10 additions & 7 deletions fluent-bundle/src/bundle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -439,13 +439,16 @@ impl<'bundle> FluentBundle<'bundle> {
let env = Env::new(self, args);
let message = self.entries.get_message(message_id)?;

let value = match message.to_value(&env) {
Ok(value) => Some(value.format(self)),
Err(err) => {
errors.push(FluentError::ResolverError(err));
None
}
};
let value = message
.value
.as_ref()
.and_then(|value| match value.to_value(&env) {
Ok(value) => Some(value.format(self)),
Err(err) => {
errors.push(FluentError::ResolverError(err));
None
}
});

let mut attributes = HashMap::new();

Expand Down
Loading

0 comments on commit ec8b223

Please sign in to comment.