|
| 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) |
0 commit comments