Skip to content

Commit d8d8a1d

Browse files
committed
Create benchmark for fluent.runtime (fixes #85)
This has a benchmark for imports, one for bundle generation, and one for a pseudo template. The data here should inform down-stream consumers what to cache.
1 parent 16af8e9 commit d8d8a1d

File tree

3 files changed

+87
-0
lines changed

3 files changed

+87
-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::TestBenchmark --benchmark-warmup=on
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::TestBenchmark --benchmark-warmup=off
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: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#!/usr/bin/env python
2+
# This should be run using pytest
3+
4+
from __future__ import unicode_literals
5+
6+
import sys
7+
import pytest
8+
import six
9+
10+
from fluent.runtime import FluentBundle
11+
12+
13+
FTL_CONTENT = """
14+
one = One
15+
two = Two
16+
three = Three
17+
four = Four
18+
five = Five
19+
six = Six
20+
seven = Seven ways to { $destination }
21+
eight = Eight
22+
nine = Nine
23+
ten = Ten
24+
"""
25+
26+
@pytest.fixture
27+
def fluent_bundle():
28+
ctx = FluentBundle(['pl'], use_isolating=False)
29+
ctx.add_messages(FTL_CONTENT)
30+
return ctx
31+
32+
33+
def fluent_template(bundle):
34+
return (
35+
"preface" +
36+
bundle.format("one")[0] +
37+
bundle.format("two")[0] +
38+
bundle.format("three")[0] +
39+
bundle.format("four")[0] +
40+
bundle.format("five")[0] +
41+
bundle.format("six")[0] +
42+
bundle.format("seven", {"destination": "Mars"})[0] +
43+
bundle.format("eight")[0] +
44+
bundle.format("nine")[0] +
45+
bundle.format("ten")[0] +
46+
"tail"
47+
)
48+
49+
50+
class TestBenchmark(object):
51+
def test_template(self, fluent_bundle, benchmark):
52+
result = benchmark(lambda: fluent_template(fluent_bundle))
53+
54+
def test_bundle(self, benchmark):
55+
def test_bundles():
56+
FluentBundle(['pl'], use_isolating=False)
57+
FluentBundle(['fr'], use_isolating=False)
58+
benchmark(test_bundles)
59+
60+
def test_import(self, benchmark):
61+
def test_imports():
62+
# prune cached imports
63+
fluent_deps = [
64+
k for k in sys.modules.keys()
65+
if k.split('.', 1)[0] in ('babel','fluent','pytz')
66+
]
67+
for k in fluent_deps:
68+
del sys.modules[k]
69+
from fluent.runtime import FluentBundle # noqa
70+
benchmark(test_imports)
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)