Skip to content

Commit

Permalink
Fix issue with xdist not being able to pass around functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
ionelmc committed Apr 9, 2016
1 parent 1e90922 commit 7493620
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 35 deletions.
10 changes: 8 additions & 2 deletions src/pytest_benchmark/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@
import py

from pytest_benchmark.csv import CSVResults

from . import plugin
from .logger import Logger
from .plugin import add_display_options, add_histogram_options, add_csv_options
from .plugin import add_csv_options
from .plugin import add_display_options
from .plugin import add_global_options
from .plugin import add_histogram_options
from .storage import Storage
from .table import TableResults
from .utils import NAME_FORMATTERS
from .utils import first_or_value
from .utils import report_noprogress

Expand Down Expand Up @@ -149,7 +153,9 @@ def main():
for file in storage.query():
print(file)
elif args.command == 'compare':
results_table = TableResults(args.columns, args.sort, first_or_value(args.histogram, False), args.name, logger)
results_table = TableResults(
args.columns, args.sort, first_or_value(args.histogram, False), NAME_FORMATTERS[args.name], logger
)
groups = load(storage, args.glob_or_file, args.group_by)

results_table.display(TerminalReporter(), groups, progress_reporter=report_noprogress)
Expand Down
4 changes: 3 additions & 1 deletion src/pytest_benchmark/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from .logger import Logger
from .storage import Storage
from .table import TableResults
from .utils import NAME_FORMATTERS
from .utils import SecondsDecimal
from .utils import cached_property
from .utils import first_or_value
Expand All @@ -16,6 +17,7 @@
from .utils import safe_dumps
from .utils import short_filename


class PerformanceRegression(pytest.UsageError):
pass

Expand Down Expand Up @@ -80,7 +82,7 @@ def __init__(self, config):
self.json = config.getoption("benchmark_json")
self.compare = config.getoption("benchmark_compare")
self.compare_fail = config.getoption("benchmark_compare_fail")
self.name_format = config.getoption("benchmark_name")
self.name_format = NAME_FORMATTERS[config.getoption("benchmark_name")]

self.storage = Storage(config.getoption("benchmark_storage"),
default_machine_id=self.machine_id, logger=self.logger)
Expand Down
60 changes: 35 additions & 25 deletions src/pytest_benchmark/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
import argparse
import genericpath
import json
import ntpath
import os
import platform
import re
import subprocess
import sys
import types
import ntpath
from datetime import datetime
from decimal import Decimal
from functools import partial
Expand Down Expand Up @@ -204,35 +204,45 @@ def parse_warmup(string):
raise argparse.ArgumentTypeError("Could not parse value: %r." % string)


def name_formatter_short(bench):
name = bench["name"]
if bench["source"]:
name = "%s (%.4s)" % (name, os.path.split(bench["source"])[-1])
if name.startswith("test_"):
name = name[5:]
return name


def name_formatter_normal(bench):
name = bench["name"]
if bench["source"]:
parts = bench["source"].split('/')
parts[-1] = parts[-1][:12]
name = "%s (%s)" % (name, '/'.join(parts))
return name


def name_formatter_long(bench):
if bench["source"]:
return "%(fullname)s (%(source)s)" % bench
else:
return bench["fullname"]


NAME_FORMATTERS = {
"short": name_formatter_short,
"normal": name_formatter_normal,
"long": name_formatter_long,
}


def parse_name_format(string):
string = string.lower().strip()
if string == "short":
def formatter(bench):
name = bench["name"]
if bench["source"]:
name = "%s (%.4s)" % (name, os.path.split(bench["source"])[-1])
if name.startswith("test_"):
name = name[5:]
return name
elif string == "normal":
def formatter(bench):
name = bench["name"]
if bench["source"]:
parts = bench["source"].split('/')
parts[-1] = parts[-1][:12]
name = "%s (%s)" % (name, '/'.join(parts))
return name
elif string == "long":
def formatter(bench):
if bench["source"]:
return "%(fullname)s (%(source)s)" % bench
else:
return bench["fullname"]
if string in NAME_FORMATTERS:
return string
else:
raise argparse.ArgumentTypeError("Could not parse value: %r." % string)

return formatter


def parse_timer(string):
return str(load_timer(string))
Expand Down
8 changes: 4 additions & 4 deletions tests/test_benchmark.py
Original file line number Diff line number Diff line change
Expand Up @@ -541,12 +541,12 @@ def test_compare(testdir):
result = testdir.runpytest('--benchmark-max-time=0.0000001', '--doctest-modules', '--benchmark-compare=0001',
'--benchmark-compare-fail=min:0.1', test)
result.stderr.fnmatch_lines([
"Comparing against benchmark *0001_unversioned_*.json",
"Comparing against benchmarks from: *0001_unversioned_*.json",
])
result = testdir.runpytest('--benchmark-max-time=0.0000001', '--doctest-modules', '--benchmark-compare=0001',
'--benchmark-compare-fail=min:1%', test)
result.stderr.fnmatch_lines([
"Comparing against benchmark *0001_unversioned_*.json",
"Comparing against benchmarks from: *0001_unversioned_*.json",
])


Expand All @@ -556,12 +556,12 @@ def test_compare_last(testdir):
result = testdir.runpytest('--benchmark-max-time=0.0000001', '--doctest-modules', '--benchmark-compare',
'--benchmark-compare-fail=min:0.1', test)
result.stderr.fnmatch_lines([
"Comparing against benchmark *0001_unversioned_*.json",
"Comparing against benchmarks from: *0001_unversioned_*.json",
])
result = testdir.runpytest('--benchmark-max-time=0.0000001', '--doctest-modules', '--benchmark-compare',
'--benchmark-compare-fail=min:1%', test)
result.stderr.fnmatch_lines([
"Comparing against benchmark *0001_unversioned_*.json",
"Comparing against benchmarks from: *0001_unversioned_*.json",
])


Expand Down
8 changes: 5 additions & 3 deletions tests/test_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
import sys
from io import BytesIO
from io import StringIO
from pathlib import Path

import py
import pytest
from freezegun import freeze_time
from pathlib import Path

from pytest_benchmark import plugin
from pytest_benchmark.plugin import BenchmarkSession
Expand All @@ -17,10 +17,12 @@
from pytest_benchmark.plugin import pytest_benchmark_group_stats
from pytest_benchmark.session import PerformanceRegression
from pytest_benchmark.storage import Storage
from pytest_benchmark.utils import DifferenceRegressionCheck, parse_name_format
from pytest_benchmark.utils import NAME_FORMATTERS
from pytest_benchmark.utils import DifferenceRegressionCheck
from pytest_benchmark.utils import PercentageRegressionCheck
from pytest_benchmark.utils import get_machine_id


pytest_plugins = "pytester"


Expand Down Expand Up @@ -59,7 +61,7 @@ def __init__(self, name_format):
self.logger = logging.getLogger(__name__)
self.machine_id = "FoobarOS"
self.machine_info = {'foo': 'bar'}
self.name_format = parse_name_format(name_format)
self.name_format = NAME_FORMATTERS[name_format]
self.save = self.autosave = self.json = False
self.options = {
'min_rounds': 123,
Expand Down

0 comments on commit 7493620

Please sign in to comment.