<s> import json <EOL> import logging <EOL> import os <EOL> import sys <EOL> from io import BytesIO <EOL> from io import StringIO <EOL> import py <EOL> import pytest <EOL> from freezegun import freeze_time <EOL> from pathlib import Path <EOL> from pytest_benchmark import plugin <EOL> from pytest_benchmark . plugin import BenchmarkSession <EOL> from pytest_benchmark . plugin import pytest_benchmark_compare_machine_info <EOL> from pytest_benchmark . plugin import pytest_benchmark_generate_json <EOL> from pytest_benchmark . plugin import pytest_benchmark_group_stats <EOL> from pytest_benchmark . session import PerformanceRegression <EOL> from pytest_benchmark . storage import Storage <EOL> from pytest_benchmark . utils import NAME_FORMATTERS <EOL> from pytest_benchmark . utils import DifferenceRegressionCheck <EOL> from pytest_benchmark . utils import PercentageRegressionCheck <EOL> from pytest_benchmark . utils import get_machine_id <EOL> pytest_plugins = "<STR_LIT>" <EOL> THIS = py . path . local ( __file__ ) <EOL> STORAGE = THIS . dirpath ( THIS . purebasename ) <EOL> SAVE_DATA = json . load ( STORAGE . listdir ( '<STR_LIT>' ) [ <NUM_LIT:0> ] . open ( ) ) <EOL> JSON_DATA = json . load ( STORAGE . listdir ( '<STR_LIT>' ) [ <NUM_LIT:0> ] . open ( ) ) <EOL> SAVE_DATA [ "<STR_LIT>" ] = JSON_DATA [ "<STR_LIT>" ] = { '<STR_LIT:foo>' : '<STR_LIT:bar>' } <EOL> SAVE_DATA [ "<STR_LIT>" ] = JSON_DATA [ "<STR_LIT>" ] = { '<STR_LIT:foo>' : '<STR_LIT:bar>' } <EOL> class Namespace ( object ) : <EOL> def __init__ ( self , ** kwargs ) : <EOL> self . __dict__ . update ( kwargs ) <EOL> def __getitem__ ( self , item ) : <EOL> return self . __dict__ [ item ] <EOL> class LooseFileLike ( BytesIO ) : <EOL> def close ( self ) : <EOL> value = self . getvalue ( ) <EOL> super ( LooseFileLike , self ) . close ( ) <EOL> self . getvalue = lambda : value <EOL> class MockSession ( BenchmarkSession ) : <EOL> def __init__ ( self , name_format ) : <EOL> self . histogram = True <EOL> self . storage = Storage ( str ( STORAGE ) , default_machine_id = get_machine_id ( ) , logger = None ) <EOL> self . benchmarks = [ ] <EOL> self . performance_regressions = [ ] <EOL> self . sort = u"<STR_LIT>" <EOL> self . compare = '<STR_LIT>' <EOL> self . logger = logging . getLogger ( __name__ ) <EOL> self . machine_id = "<STR_LIT>" <EOL> self . machine_info = { '<STR_LIT:foo>' : '<STR_LIT:bar>' } <EOL> self . name_format = NAME_FORMATTERS [ name_format ] <EOL> self . save = self . autosave = self . json = False <EOL> self . options = { <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> } <EOL> self . compare_fail = [ ] <EOL> self . config = Namespace ( hook = Namespace ( <EOL> pytest_benchmark_group_stats = pytest_benchmark_group_stats , <EOL> pytest_benchmark_generate_machine_info = lambda ** kwargs : { '<STR_LIT:foo>' : '<STR_LIT:bar>' } , <EOL> pytest_benchmark_update_machine_info = lambda ** kwargs : None , <EOL> pytest_benchmark_compare_machine_info = pytest_benchmark_compare_machine_info , <EOL> pytest_benchmark_generate_json = pytest_benchmark_generate_json , <EOL> pytest_benchmark_update_json = lambda ** kwargs : None , <EOL> pytest_benchmark_generate_commit_info = lambda ** kwargs : { '<STR_LIT:foo>' : '<STR_LIT:bar>' } , <EOL> pytest_benchmark_update_commit_info = lambda ** kwargs : None , <EOL> ) ) <EOL> self . group_by = '<STR_LIT>' <EOL> self . columns = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> for bench_file in reversed ( self . storage . query ( "<STR_LIT>" ) ) : <EOL> with bench_file . open ( '<STR_LIT>' ) as fh : <EOL> data = json . load ( fh ) <EOL> self . benchmarks . extend ( <EOL> Namespace ( <EOL> as_dict = lambda include_data = False , stats = True , flat = False , _bench = bench : <EOL> dict ( _bench , ** _bench [ "<STR_LIT>" ] ) if flat else dict ( _bench ) , <EOL> name = bench [ '<STR_LIT:name>' ] , <EOL> fullname = bench [ '<STR_LIT>' ] , <EOL> group = bench [ '<STR_LIT>' ] , <EOL> options = bench [ '<STR_LIT>' ] , <EOL> has_error = False , <EOL> params = None , <EOL> ** bench [ '<STR_LIT>' ] <EOL> ) <EOL> for bench in data [ '<STR_LIT>' ] <EOL> ) <EOL> break <EOL> try : <EOL> text_type = unicode <EOL> except NameError : <EOL> text_type = str <EOL> def force_text ( text ) : <EOL> if isinstance ( text , text_type ) : <EOL> return text <EOL> else : <EOL> return text . decode ( '<STR_LIT:utf-8>' ) <EOL> def force_bytes ( text ) : <EOL> if isinstance ( text , text_type ) : <EOL> return text . encode ( '<STR_LIT:utf-8>' ) <EOL> else : <EOL> return text <EOL> @ pytest . fixture ( params = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> def name_format ( request ) : <EOL> return request . param <EOL> @ pytest . fixture <EOL> def sess ( request , name_format ) : <EOL> return MockSession ( name_format ) <EOL> def make_logger ( sess ) : <EOL> output = StringIO ( ) <EOL> sess . logger = Namespace ( <EOL> warn = lambda code , text , ** opts : output . write ( u"<STR_LIT>" % ( code , force_text ( text ) , opts ) ) , <EOL> info = lambda text , ** opts : output . write ( force_text ( text ) + u'<STR_LIT:\n>' ) , <EOL> error = lambda text : output . write ( force_text ( text ) + u'<STR_LIT:\n>' ) , <EOL> ) <EOL> return output <EOL> def test_rendering ( sess ) : <EOL> output = make_logger ( sess ) <EOL> sess . histogram = os . path . join ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> sess . compare = '<STR_LIT>' <EOL> sess . sort = '<STR_LIT:name>' <EOL> sess . finish ( ) <EOL> sess . display ( Namespace ( <EOL> ensure_newline = lambda : None , <EOL> write_line = lambda line , ** opts : output . write ( force_text ( line ) + u'<STR_LIT:\n>' ) , <EOL> write = lambda text , ** opts : output . write ( force_text ( text ) ) , <EOL> rewrite = lambda text , ** opts : output . write ( force_text ( text ) ) , <EOL> ) ) <EOL> def test_regression_checks ( sess , name_format ) : <EOL> output = make_logger ( sess ) <EOL> sess . handle_loading ( ) <EOL> sess . performance_regressions = [ ] <EOL> sess . compare_fail = [ <EOL> PercentageRegressionCheck ( "<STR_LIT>" , <NUM_LIT:5> ) , <EOL> DifferenceRegressionCheck ( "<STR_LIT>" , <NUM_LIT> ) <EOL> ] <EOL> sess . finish ( ) <EOL> pytest . raises ( PerformanceRegression , sess . display , Namespace ( <EOL> ensure_newline = lambda : None , <EOL> write_line = lambda line , ** opts : output . write ( force_text ( line ) + u'<STR_LIT:\n>' ) , <EOL> write = lambda text , ** opts : output . write ( force_text ( text ) ) , <EOL> rewrite = lambda text , ** opts : output . write ( force_text ( text ) ) , <EOL> ) ) <EOL> print ( output . getvalue ( ) ) <EOL> assert sess . performance_regressions == { <EOL> '<STR_LIT>' : [ <EOL> ( '<STR_LIT>' , <EOL> "<STR_LIT>" ) , <EOL> ( '<STR_LIT>' , <EOL> "<STR_LIT>" ) <EOL> ] , <EOL> '<STR_LIT>' : [ <EOL> ( '<STR_LIT>' , <EOL> "<STR_LIT>" ) , <EOL> ( '<STR_LIT>' , <EOL> "<STR_LIT>" ) <EOL> ] , <EOL> '<STR_LIT>' : [ <EOL> ( '<STR_LIT>' , <EOL> "<STR_LIT>" ) , <EOL> ( '<STR_LIT>' , <EOL> "<STR_LIT>" ) <EOL> ] , <EOL> } [ name_format ] <EOL> output = make_logger ( sess ) <EOL> pytest . raises ( PerformanceRegression , sess . check_regressions ) <EOL> print ( output . getvalue ( ) ) <EOL> assert output . getvalue ( ) == { <EOL> '<STR_LIT>' : """<STR_LIT>""" , <EOL> '<STR_LIT>' : """<STR_LIT>""" , <EOL> '<STR_LIT>' : """<STR_LIT>""" <EOL> } [ name_format ] <EOL> @ pytest . mark . skipif ( sys . version_info [ : <NUM_LIT:2> ] < ( <NUM_LIT:2> , <NUM_LIT:7> ) , <EOL> reason = "<STR_LIT>" ) <EOL> def test_regression_checks_inf ( sess , name_format ) : <EOL> output = make_logger ( sess ) <EOL> sess . compare = '<STR_LIT>' <EOL> sess . handle_loading ( ) <EOL> sess . performance_regressions = [ ] <EOL> sess . compare_fail = [ <EOL> PercentageRegressionCheck ( "<STR_LIT>" , <NUM_LIT:5> ) , <EOL> DifferenceRegressionCheck ( "<STR_LIT>" , <NUM_LIT> ) <EOL> ] <EOL> sess . finish ( ) <EOL> pytest . raises ( PerformanceRegression , sess . display , Namespace ( <EOL> ensure_newline = lambda : None , <EOL> write_line = lambda line , ** opts : output . write ( force_text ( line ) + u'<STR_LIT:\n>' ) , <EOL> write = lambda text , ** opts : output . write ( force_text ( text ) ) , <EOL> rewrite = lambda text , ** opts : output . write ( force_text ( text ) ) , <EOL> ) ) <EOL> print ( output . getvalue ( ) ) <EOL> assert sess . performance_regressions == { <EOL> '<STR_LIT>' : [ <EOL> ( '<STR_LIT>' , <EOL> "<STR_LIT>" ) , <EOL> ( '<STR_LIT>' , <EOL> "<STR_LIT>" ) <EOL> ] , <EOL> '<STR_LIT>' : [ <EOL> ( '<STR_LIT>' , <EOL> "<STR_LIT>" ) , <EOL> ( '<STR_LIT>' , <EOL> "<STR_LIT>" ) <EOL> ] , <EOL> '<STR_LIT>' : [ <EOL> ( '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> "<STR_LIT>" ) , <EOL> ( '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> "<STR_LIT>" <EOL> '<STR_LIT>' ) <EOL> ] <EOL> } [ name_format ] <EOL> output = make_logger ( sess ) <EOL> pytest . raises ( PerformanceRegression , sess . check_regressions ) <EOL> print ( output . getvalue ( ) ) <EOL> assert output . getvalue ( ) == { <EOL> '<STR_LIT>' : """<STR_LIT>""" , <EOL> '<STR_LIT>' : """<STR_LIT>""" , <EOL> '<STR_LIT>' : """<STR_LIT>""" <EOL> } [ name_format ] <EOL> def test_compare_1 ( sess , LineMatcher ) : <EOL> output = make_logger ( sess ) <EOL> sess . handle_loading ( ) <EOL> sess . finish ( ) <EOL> sess . display ( Namespace ( <EOL> ensure_newline = lambda : None , <EOL> write_line = lambda line , ** opts : output . write ( force_text ( line ) + u'<STR_LIT:\n>' ) , <EOL> write = lambda text , ** opts : output . write ( force_text ( text ) ) , <EOL> rewrite = lambda text , ** opts : output . write ( force_text ( text ) ) , <EOL> ) ) <EOL> print ( output . getvalue ( ) ) <EOL> LineMatcher ( output . getvalue ( ) . splitlines ( ) ) . fnmatch_lines ( [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] ) <EOL> def test_compare_2 ( sess , LineMatcher ) : <EOL> output = make_logger ( sess ) <EOL> sess . compare = '<STR_LIT>' <EOL> sess . handle_loading ( ) <EOL> sess . finish ( ) <EOL> sess . display ( Namespace ( <EOL> ensure_newline = lambda : None , <EOL> write_line = lambda line , ** opts : output . write ( force_text ( line ) + u'<STR_LIT:\n>' ) , <EOL> section = lambda line , ** opts : output . write ( force_text ( line ) + u'<STR_LIT:\n>' ) , <EOL> write = lambda text , ** opts : output . write ( force_text ( text ) ) , <EOL> rewrite = lambda text , ** opts : output . write ( force_text ( text ) ) , <EOL> ) ) <EOL> print ( output . getvalue ( ) ) <EOL> LineMatcher ( output . getvalue ( ) . splitlines ( ) ) . fnmatch_lines ( [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] ) <EOL> @ freeze_time ( "<STR_LIT>" ) <EOL> def test_save_json ( sess , tmpdir , monkeypatch ) : <EOL> monkeypatch . setattr ( plugin , '<STR_LIT>' , '<STR_LIT>' ) <EOL> sess . save = False <EOL> sess . autosave = False <EOL> sess . json = LooseFileLike ( ) <EOL> sess . save_data = False <EOL> sess . handle_saving ( ) <EOL> assert tmpdir . listdir ( ) == [ ] <EOL> assert json . loads ( sess . json . getvalue ( ) . decode ( ) ) == JSON_DATA <EOL> @ freeze_time ( "<STR_LIT>" ) <EOL> def test_save_with_name ( sess , tmpdir , monkeypatch ) : <EOL> monkeypatch . setattr ( plugin , '<STR_LIT>' , '<STR_LIT>' ) <EOL> sess . save = '<STR_LIT>' <EOL> sess . autosave = True <EOL> sess . json = None <EOL> sess . save_data = False <EOL> sess . storage . path = Path ( str ( tmpdir ) ) <EOL> sess . handle_saving ( ) <EOL> files = list ( Path ( str ( tmpdir ) ) . rglob ( '<STR_LIT>' ) ) <EOL> assert len ( files ) == <NUM_LIT:1> <EOL> assert json . load ( files [ <NUM_LIT:0> ] . open ( '<STR_LIT>' ) ) == SAVE_DATA <EOL> @ freeze_time ( "<STR_LIT>" ) <EOL> def test_save_no_name ( sess , tmpdir , monkeypatch ) : <EOL> monkeypatch . setattr ( plugin , '<STR_LIT>' , '<STR_LIT>' ) <EOL> sess . save = True <EOL> sess . autosave = True <EOL> sess . json = None <EOL> sess . save_data = False <EOL> sess . storage . path = Path ( str ( tmpdir ) ) <EOL> sess . handle_saving ( ) <EOL> files = list ( Path ( str ( tmpdir ) ) . rglob ( '<STR_LIT>' ) ) <EOL> assert len ( files ) == <NUM_LIT:1> <EOL> assert json . load ( files [ <NUM_LIT:0> ] . open ( '<STR_LIT>' ) ) == SAVE_DATA <EOL> @ freeze_time ( "<STR_LIT>" ) <EOL> def test_save_with_error ( sess , tmpdir , monkeypatch ) : <EOL> monkeypatch . setattr ( plugin , '<STR_LIT>' , '<STR_LIT>' ) <EOL> sess . save = True <EOL> sess . autosave = True <EOL> sess . json = None <EOL> sess . save_data = False <EOL> sess . storage . path = Path ( str ( tmpdir ) ) <EOL> for bench in sess . benchmarks : <EOL> bench . has_error = True <EOL> sess . handle_saving ( ) <EOL> files = list ( Path ( str ( tmpdir ) ) . rglob ( '<STR_LIT>' ) ) <EOL> assert len ( files ) == <NUM_LIT:1> <EOL> assert json . load ( files [ <NUM_LIT:0> ] . open ( '<STR_LIT>' ) ) == { <EOL> '<STR_LIT>' : [ ] , <EOL> '<STR_LIT>' : { '<STR_LIT:foo>' : '<STR_LIT:bar>' } , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : { '<STR_LIT:foo>' : '<STR_LIT:bar>' } , <EOL> '<STR_LIT:version>' : '<STR_LIT>' <EOL> } <EOL> @ freeze_time ( "<STR_LIT>" ) <EOL> def test_autosave ( sess , tmpdir , monkeypatch ) : <EOL> monkeypatch . setattr ( plugin , '<STR_LIT>' , '<STR_LIT>' ) <EOL> sess . save = False <EOL> sess . autosave = True <EOL> sess . json = None <EOL> sess . save_data = False <EOL> sess . storage . path = Path ( str ( tmpdir ) ) <EOL> sess . handle_saving ( ) <EOL> files = list ( Path ( str ( tmpdir ) ) . rglob ( '<STR_LIT>' ) ) <EOL> assert len ( files ) == <NUM_LIT:1> <EOL> assert json . load ( files [ <NUM_LIT:0> ] . open ( '<STR_LIT>' ) ) == SAVE_DATA </s>
