Skip to content

Commit

Permalink
[pre-commit.ci] auto fixes from pre-commit.com hooks
Browse files Browse the repository at this point in the history
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] committed May 26, 2022
1 parent d036df2 commit de98a60
Show file tree
Hide file tree
Showing 8 changed files with 442 additions and 295 deletions.
73 changes: 39 additions & 34 deletions tests/benchmark3.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"""
import random
import sys

import ubelt as ub


Expand All @@ -27,21 +28,23 @@ def json_test_data_generators():
>>> print('test_object = {!r}'.format(test_object))
"""
data_lut = {}

def _register_data(name):
def _wrap(func):
data_lut[name] = func

return _wrap

# seed if desired
#rng = random.Random()
# rng = random.Random()
rng = random

@_register_data('Array with doubles')
@_register_data("Array with doubles")
def array_with_doubles(size):
test_object = [sys.maxsize * rng.random() for _ in range(size)]
return test_object

@_register_data('Array with UTF-8 strings')
@_register_data("Array with UTF-8 strings")
def array_with_utf8_strings(size):
utf8_string = (
"نظام الحكم سلطاني وراثي "
Expand All @@ -51,7 +54,7 @@ def array_with_utf8_strings(size):
test_object = [utf8_string for _ in range(size)]
return test_object

@_register_data('Medium complex object')
@_register_data("Medium complex object")
def medium_complex_object(size):
user = {
"userId": 3381293,
Expand All @@ -68,20 +71,19 @@ def medium_complex_object(size):
test_object = [[user, friends] for _ in range(size)]
return test_object

@_register_data('Array with True values')
@_register_data("Array with True values")
def true_values(size):
test_object = [True for _ in range(size)]
return test_object

@_register_data('Array of Dict[str, int]')
@_register_data("Array of Dict[str, int]")
def array_of_dict_string_int(size):
test_object = [
{str(rng.random() * 20): int(rng.random() * 1000000)}
for _ in range(size)
{str(rng.random() * 20): int(rng.random() * 1000000)} for _ in range(size)
]
return test_object

@_register_data('Dict of List[Dict[str, int]]')
@_register_data("Dict of List[Dict[str, int]]")
def dict_of_list_dict_str_int(size):
keys = set()
while len(keys) < size:
Expand All @@ -96,23 +98,25 @@ def dict_of_list_dict_str_int(size):
}
return test_object

@_register_data('Complex object')
@_register_data("Complex object")
def complex_object(size):
import json

# TODO: might be better to reigster this file with setup.py or
# download it via some mechanism
try:
dpath = ub.Path(__file__).parent
fpath = dpath / 'sample.json'
fpath = dpath / "sample.json"
if not fpath.exists():
raise Exception
except Exception:
import ujson
dpath = ub.Path(ujson.__file__).parent / 'tests'
fpath = dpath / 'sample.json'

dpath = ub.Path(ujson.__file__).parent / "tests"
fpath = dpath / "sample.json"
if not fpath.exists():
raise Exception
with open(fpath, 'r') as f:
with open(fpath) as f:
test_object = json.load(f)
if size > 1:
test_object = [test_object] * size
Expand All @@ -123,9 +127,8 @@ def complex_object(size):

def available_json_impls():
import importlib
known_modnames = [
'ujson', 'json', 'nujson', 'orjson', 'simplejson'
]

known_modnames = ["ujson", "json", "nujson", "orjson", "simplejson"]
json_impls = {}
for libname in known_modnames:
try:
Expand All @@ -134,15 +137,15 @@ def available_json_impls():
pass
else:
json_impls[libname] = {
'module': module,
'version': module.__version__,
"module": module,
"version": module.__version__,
}
return json_impls


def benchmark_json_dumps():
# TODO: remove this hack
sys.path.append(ub.expandpath('~/code/ultrajson/tests'))
sys.path.append(ub.expandpath("~/code/ultrajson/tests"))
from benchmarker import Benchmarker

json_impls = available_json_impls()
Expand All @@ -153,11 +156,11 @@ def benchmark_json_dumps():
# These are the parameters that we benchmark over
basis = {
"input": [
'Array with doubles',
'Array with UTF-8 strings',
"Array with doubles",
"Array with UTF-8 strings",
# 'Medium complex object',
'Array with True values',
'Array of Dict[str, int]',
"Array with True values",
"Array of Dict[str, int]",
# 'Dict of List[Dict[str, int]]',
# 'Complex object'
],
Expand All @@ -169,7 +172,7 @@ def benchmark_json_dumps():
# abstract away the details of timing a process over a grid of parameters,
# serializing the results, and aggregating results from disparate runs.
benchmark = Benchmarker(
name='bench_json_dumps',
name="bench_json_dumps",
num=100,
bestof=10,
verbose=2,
Expand All @@ -181,8 +184,8 @@ def benchmark_json_dumps():
# Make any modifications you need to compute input kwargs for each
# method here.
impl_info = json_impls[params["impl"]]
method = impl_info['module'].dumps
impl_version = impl_info['version']
method = impl_info["module"].dumps
impl_version = impl_info["version"]
params["impl_version"] = impl_version
data = data_lut[params["input"]](params["size"])
# Timerit will run some user-specified number of loops.
Expand All @@ -194,28 +197,30 @@ def benchmark_json_dumps():
# Put the logic you want to time here
method(data)

dpath = ub.Path.appdir('ujson/benchmark_results').ensuredir()
dpath = ub.Path.appdir("ujson/benchmark_results").ensuredir()
benchmark.dump_in_dpath(dpath)

RECORD_ALL = 0
metric_key = "time" if RECORD_ALL else "mean_time"

from benchmarker import result_analysis

results = benchmark.result.to_result_list()

analysis = result_analysis.ResultAnalysis(
results,
metrics=[metric_key],
params=['impl'],
params=["impl"],
metric_objectives={
'min_time': 'min',
'mean_time': 'min',
'time': 'min',
})
"min_time": "min",
"mean_time": "min",
"time": "min",
},
)
analysis.analysis()
analysis.table

param_group = ['impl', 'impl_version']
param_group = ["impl", "impl_version"]
analysis.abalate(param_group)

# benchmark_analysis(rows, xlabel, group_labels, basis, RECORD_ALL)
Expand Down
72 changes: 48 additions & 24 deletions tests/benchmarker/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,53 @@
mkinit ~/code/ultrajson/tests/benchmarker/__init__.py -w
"""

__version__ = '0.1.0'
__version__ = "0.1.0"

from benchmarker import aggregate
from benchmarker import benchmarker
from benchmarker import process_context
from benchmarker import result_analysis
from benchmarker import util_json
from benchmarker import visualize
from benchmarker import (
aggregate,
benchmarker,
process_context,
result_analysis,
util_json,
visualize,
)
from benchmarker.aggregate import demo, demo_data
from benchmarker.benchmarker import (
Benchmarker,
BenchmarkerConfig,
BenchmarkerResult,
combine_stats,
stats_dict,
)
from benchmarker.process_context import ProcessContext
from benchmarker.result_analysis import Result, ResultAnalysis, SkillTracker
from benchmarker.util_json import (
ensure_json_serializable,
find_json_unserializable,
indexable_allclose,
)
from benchmarker.visualize import benchmark_analysis

from benchmarker.aggregate import (demo, demo_data,)
from benchmarker.benchmarker import (Benchmarker, BenchmarkerConfig,
BenchmarkerResult, combine_stats,
stats_dict,)
from benchmarker.process_context import (ProcessContext,)
from benchmarker.result_analysis import (Result, ResultAnalysis, SkillTracker,)
from benchmarker.util_json import (ensure_json_serializable,
find_json_unserializable,
indexable_allclose,)
from benchmarker.visualize import (benchmark_analysis,)

__all__ = ['Benchmarker', 'BenchmarkerConfig', 'BenchmarkerResult',
'ProcessContext', 'Result', 'ResultAnalysis', 'SkillTracker',
'aggregate', 'benchmark_analysis', 'benchmarker', 'combine_stats',
'demo', 'demo_data', 'ensure_json_serializable',
'find_json_unserializable', 'indexable_allclose', 'process_context',
'result_analysis', 'stats_dict', 'util_json', 'visualize']
__all__ = [
"Benchmarker",
"BenchmarkerConfig",
"BenchmarkerResult",
"ProcessContext",
"Result",
"ResultAnalysis",
"SkillTracker",
"aggregate",
"benchmark_analysis",
"benchmarker",
"combine_stats",
"demo",
"demo_data",
"ensure_json_serializable",
"find_json_unserializable",
"indexable_allclose",
"process_context",
"result_analysis",
"stats_dict",
"util_json",
"visualize",
]
43 changes: 24 additions & 19 deletions tests/benchmarker/aggregate.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,35 @@
import json

import pandas as pd
import ubelt as ub


def demo_data():
from benchmarker.benchmarker import Benchmarker
import numpy as np
from benchmarker.benchmarker import Benchmarker

impl_lut = {
'numpy': np.sum,
'builtin': sum,
"numpy": np.sum,
"builtin": sum,
}

def data_lut(params):
item = 42 if params['dtype'] == 'int' else 42.0
data = [item] * params['size']
item = 42 if params["dtype"] == "int" else 42.0
data = [item] * params["size"]
return data

basis = {
'impl': ['builtin', 'numpy'],
'size': [10, 10000],
'dtype': ['int', 'float'],
"impl": ["builtin", "numpy"],
"size": [10, 10000],
"dtype": ["int", "float"],
}

dpath = ub.Path.appdir('benchmarker/agg_demo').delete().ensuredir()
dpath = ub.Path.appdir("benchmarker/agg_demo").delete().ensuredir()

def run_one_benchmark():
self = Benchmarker(name='agg_demo', num=10, bestof=3, basis=basis)
self = Benchmarker(name="agg_demo", num=10, bestof=3, basis=basis)
for params in self.iter_params():
impl = impl_lut[params['impl']]
impl = impl_lut[params["impl"]]
data = data_lut(params)
for timer in self.measure():
with timer:
Expand All @@ -43,25 +47,26 @@ def run_one_benchmark():


def demo():
from benchmarker import BenchmarkerResult
from benchmarker import result_analysis
from benchmarker import BenchmarkerResult, result_analysis

fpaths = demo_data()

results = []
for fpath in fpaths:
data = json.loads(fpath.read_text())
for row in data['rows']:
for row in data["rows"]:
result = BenchmarkerResult.load(fpath)
results.extend(result.to_result_list())

analysis = result_analysis.ResultAnalysis(
results,
metrics=['min', 'mean'],
params=['impl'],
metrics=["min", "mean"],
params=["impl"],
metric_objectives={
'min': 'min',
'mean': 'min',
})
"min": "min",
"mean": "min",
},
)
analysis.analysis()
# single_df = pd.DataFrame(data['rows'])
# context = data['context']
Expand Down

0 comments on commit de98a60

Please sign in to comment.