From 8abb35372a95298c2c9437b203a1d698c3db0b65 Mon Sep 17 00:00:00 2001 From: Damien Elmes Date: Sun, 16 Feb 2020 21:40:22 +1000 Subject: [PATCH] test out the Python Fluent implementation The parsing step is considerably slower in Python, but if parsing is moved out of the test function, Python wins at 45ms to Rust's 67ms on 10,000 rounds, presumably due to the overhead of serializing to Protobuf. Not enough of a difference to justify the inclusion of extra dependencies and duplicating the lookup code in any case. --- pylib/setup.py | 1 + pylib/tests/test_collection.py | 38 ++++++++++++++++++++++++++++++---- 2 files changed, 35 insertions(+), 4 deletions(-) diff --git a/pylib/setup.py b/pylib/setup.py index ffb93affd..9462d2542 100644 --- a/pylib/setup.py +++ b/pylib/setup.py @@ -23,5 +23,6 @@ setuptools.setup( "protobuf", 'psutil; sys_platform == "win32"', 'distro; sys_platform != "darwin" and sys_platform != "win32"', + "fluent.runtime", ], ) diff --git a/pylib/tests/test_collection.py b/pylib/tests/test_collection.py index 623e16181..3fc58c44d 100644 --- a/pylib/tests/test_collection.py +++ b/pylib/tests/test_collection.py @@ -158,7 +158,37 @@ def test_translate(): def no_uni(s: str) -> str: return s.replace("\u2068", "").replace("\u2069", "") - assert tr(StringsGroup.TEST, "valid-key") == "a valid key" - assert "invalid-key" in tr(StringsGroup.TEST, "invalid-key") - assert no_uni(tr(StringsGroup.TEST, "plural", hats=1)) == "You have 1 hat." - assert no_uni(tr(StringsGroup.TEST, "plural", hats=2)) == "You have 2 hats." + def test_backend(): + assert tr(StringsGroup.TEST, "valid-key") == "a valid key" + assert "invalid-key" in tr(StringsGroup.TEST, "invalid-key") + assert no_uni(tr(StringsGroup.TEST, "plural", hats=1)) == "You have 1 hat." + assert no_uni(tr(StringsGroup.TEST, "plural", hats=2)) == "You have 2 hats." + + import time + t = time.time() + test_backend() + print(time.time() - t) + + from fluent.runtime import FluentBundle, FluentResource + + test_path = os.path.join( + os.path.dirname(__file__), "../../rslib/tests/support/test.ftl" + ) + + def python_render(bundle, key, **kwargs): + try: + return bundle.format_pattern(bundle.get_message(key).value, kwargs)[0] + except: + return f"Missing key: {key}" + + def test_python(): + bundle = FluentBundle(["en-US"]) + bundle.add_resource(FluentResource(open(test_path).read())) + assert python_render(bundle, "valid-key") == "a valid key" + assert "invalid-key" in python_render(bundle, "invalid-key") + assert no_uni(python_render(bundle, "plural", hats=1)) == "You have 1 hat." + assert no_uni(python_render(bundle, "plural", hats=2)) == "You have 2 hats." + + t = time.time() + test_python() + print(time.time() - t)