Skip to content

Commit 1ba241c

Browse files
committed
Create benchmark for fluent.runtime (fixes #85)
1 parent 16af8e9 commit 1ba241c

File tree

3 files changed

+72
-0
lines changed

3 files changed

+72
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
To run the benchmarks, do:
2+
3+
$ pip install -r tools/benchmarks/requirements.txt
4+
$ py.test ./tools/benchmarks/fluent_benchmark.py --benchmark-warmup=on -k test_benchmark
5+
6+
To profile the benchmark suite, we recommend py-spy as a
7+
good tool. Install py-spy: https://github.com/benfred/py-spy
8+
9+
Then do something like this to profile the benchmark. Depending on your
10+
platform, you might need to use `sudo`.
11+
12+
$ py-spy -f prof.svg -- py.test ./tools/benchmarks/fluent_benchmark.py -k test_fluent
13+
14+
And look at prof.svg in a browser. Note that this diagram includes the fixture
15+
setup, warmup and calibration phases which you should ignore.
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#!/usr/bin/env python
2+
# This should be run using pytest
3+
4+
from __future__ import unicode_literals
5+
6+
import pytest
7+
import six
8+
9+
from fluent.runtime import FluentBundle
10+
11+
12+
FTL_CONTENT = """
13+
one = One
14+
two = Two
15+
three = Three
16+
four = Four
17+
five = Five
18+
six = Six
19+
seven = Seven ways to { $destination }
20+
eight = Eight
21+
nine = Nine
22+
ten = Ten
23+
"""
24+
25+
@pytest.fixture
26+
def fluent_bundle():
27+
ctx = FluentBundle(['pl'], use_isolating=False)
28+
ctx.add_messages(FTL_CONTENT)
29+
return ctx
30+
31+
32+
def fluent_template(bundle):
33+
return (
34+
"preface" +
35+
bundle.format("one")[0] +
36+
bundle.format("two")[0] +
37+
bundle.format("three")[0] +
38+
bundle.format("four")[0] +
39+
bundle.format("five")[0] +
40+
bundle.format("six")[0] +
41+
bundle.format("seven", {"destination": "Mars"})[0] +
42+
bundle.format("eight")[0] +
43+
bundle.format("nine")[0] +
44+
bundle.format("ten")[0] +
45+
"tail"
46+
)
47+
48+
49+
def test_benchmark(fluent_bundle, benchmark):
50+
result = benchmark(lambda: fluent_template(fluent_bundle))
51+
52+
53+
def test_fluent(fluent_bundle):
54+
for _ in range(10000):
55+
result = fluent_template(fluent_bundle)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pytest
2+
pytest-benchmark

0 commit comments

Comments
 (0)