from __future__ import annotations
import sys
from pathlib import Path
import pytest
_BENCHMARKS_DIR = str(Path(__file__).resolve().parent.parent.parent / "benchmarks")
if _BENCHMARKS_DIR not in sys.path:
sys.path.insert(0, _BENCHMARKS_DIR)
@pytest.mark.benchmark
class TestLMBenchmark:
@pytest.fixture(scope="class")
def lm_results(self):
pytest.importorskip("nltk")
from run_lm import run_benchmarks
return run_benchmarks(quick=True, verbose=False)
def test_fit_speedup(self, lm_results):
speedups = lm_results.get("speedups", {})
assert "Fit" in speedups, "No fit speedup computed"
assert (
speedups["Fit"] >= 4.0
), f"Fit speedup {speedups['Fit']:.1f}x below floor 4x"
def test_score_speedup(self, lm_results):
speedups = lm_results.get("speedups", {})
assert "Score" in speedups, "No score speedup computed"
assert (
speedups["Score"] >= 1.0
), f"Score speedup {speedups['Score']:.1f}x below floor 1x"
def test_generate_speedup(self, lm_results):
speedups = lm_results.get("speedups", {})
assert "Generate" in speedups, "No generate speedup computed"
gen = speedups["Generate"]
min_speedup = gen["min"] if isinstance(gen, dict) else gen
assert (
min_speedup >= 15.0
), f"Generate speedup {min_speedup:.1f}x below floor 15x"
@pytest.mark.benchmark
class TestWordsegBenchmark:
@pytest.fixture(scope="class")
def wordseg_results(self):
pytest.importorskip("wordseg")
from run_wordseg import run_benchmarks
return run_benchmarks(quick=True, verbose=False)
def test_lsm_speedup(self, wordseg_results):
speedups = wordseg_results.get("speedups", {})
assert "LongestStringMatching" in speedups, "No LSM speedup computed"
assert (
speedups["LongestStringMatching"] >= 3.0
), f"LSM speedup {speedups['LongestStringMatching']:.1f}x below floor 3x"
@pytest.mark.benchmark
class TestTaggerBenchmark:
@pytest.fixture(scope="class")
def tagger_results(self):
pytest.importorskip("nltk")
from run_perceptron_pos_tagger import run_benchmarks
return run_benchmarks(quick=True, verbose=False)
def test_training_speedup(self, tagger_results):
speedups = tagger_results.get("speedups", {})
assert "Training" in speedups, "No training speedup computed"
assert (
speedups["Training"] >= 2.0
), f"Training speedup {speedups['Training']:.1f}x below floor 2x"
def test_tagging_speedup(self, tagger_results):
speedups = tagger_results.get("speedups", {})
assert "Tagging" in speedups, "No tagging speedup computed"
assert (
speedups["Tagging"] >= 5.0
), f"Tagging speedup {speedups['Tagging']:.1f}x below floor 5x"
@pytest.mark.benchmark
class TestHMMBenchmark:
@pytest.fixture(scope="class")
def hmm_results(self):
pytest.importorskip("hmmlearn")
from run_hmm import run_benchmarks
return run_benchmarks(quick=True, verbose=False)
def test_fit_speedup(self, hmm_results):
speedups = hmm_results.get("speedups", {})
assert "Fit" in speedups, "No fit speedup computed"
assert (
speedups["Fit"] >= 3.0
), f"Fit speedup {speedups['Fit']:.1f}x below floor 3x"
def test_predict_speedup(self, hmm_results):
speedups = hmm_results.get("speedups", {})
assert "Predict" in speedups, "No predict speedup computed"
assert (
speedups["Predict"] >= 0.5
), f"Predict speedup {speedups['Predict']:.1f}x below floor 0.5x"
def test_score_speedup(self, hmm_results):
speedups = hmm_results.get("speedups", {})
assert "Score" in speedups, "No score speedup computed"
assert (
speedups["Score"] >= 1.0
), f"Score speedup {speedups['Score']:.1f}x below floor 1x"
@pytest.mark.benchmark
class TestChatBenchmark:
@pytest.fixture(scope="class")
def chat_results(self):
pytest.importorskip("pylangacq")
from run_chat import run_benchmarks
return run_benchmarks(quick=True, verbose=False)
def test_from_zip_speedup(self, chat_results):
speedups = chat_results.get("speedups", {})
assert "from_zip" in speedups, "No from_zip speedup computed"
assert (
speedups["from_zip"] >= 10.0
), f"from_zip speedup {speedups['from_zip']:.1f}x below floor 10x"
def test_from_strs_speedup(self, chat_results):
speedups = chat_results.get("speedups", {})
assert "from_strs" in speedups, "No from_strs speedup computed"
assert (
speedups["from_strs"] >= 20.0
), f"from_strs speedup {speedups['from_strs']:.1f}x below floor 20x"
def test_utterances_speedup(self, chat_results):
speedups = chat_results.get("speedups", {})
assert "utterances()" in speedups, "No utterances() speedup computed"
assert (
speedups["utterances()"] >= 5.0
), f"utterances() speedup {speedups['utterances()']:.1f}x below floor 5x"
def test_tokens_speedup(self, chat_results):
speedups = chat_results.get("speedups", {})
assert "tokens()" in speedups, "No tokens() speedup computed"
assert (
speedups["tokens()"] >= 3.0
), f"tokens() speedup {speedups['tokens()']:.1f}x below floor 3x"