diff --git a/aws_xray_sdk/core/sampling/local/sampler.py b/aws_xray_sdk/core/sampling/local/sampler.py index fe524294..5b2b3a81 100644 --- a/aws_xray_sdk/core/sampling/local/sampler.py +++ b/aws_xray_sdk/core/sampling/local/sampler.py @@ -1,13 +1,11 @@ import json +import pkgutil from random import Random -from pkg_resources import resource_filename from .sampling_rule import SamplingRule from ...exceptions.exceptions import InvalidSamplingManifestError - -with open(resource_filename(__name__, 'sampling_rule.json')) as f: - local_sampling_rule = json.load(f) +local_sampling_rule = json.loads(pkgutil.get_data(__name__, 'sampling_rule.json')) SUPPORTED_RULE_VERSION = (1, 2) diff --git a/aws_xray_sdk/ext/boto_utils.py b/aws_xray_sdk/ext/boto_utils.py index 9bd9763e..06532fe7 100644 --- a/aws_xray_sdk/ext/boto_utils.py +++ b/aws_xray_sdk/ext/boto_utils.py @@ -1,8 +1,8 @@ from __future__ import absolute_import # Need absolute import as botocore is also in the current folder for py27 import json +import pkgutil -from pkg_resources import resource_filename from botocore.exceptions import ClientError from aws_xray_sdk.core import xray_recorder @@ -12,8 +12,7 @@ from aws_xray_sdk.ext.util import inject_trace_header, to_snake_case -with open(resource_filename(__name__, 'resources/aws_para_whitelist.json'), 'r') as data_file: - whitelist = json.load(data_file) +whitelist = json.loads(pkgutil.get_data(__name__, 'resources/aws_para_whitelist.json')) def inject_header(wrapped, instance, args, kwargs): diff --git a/tests/mock_sampling_rule.json b/tests/mock_sampling_rule.json new file mode 100644 index 00000000..32891ac7 --- /dev/null +++ b/tests/mock_sampling_rule.json @@ -0,0 +1,9 @@ +{ + "version": 2, + "default": { + "fixed_target": 1, + "rate": 0.05 + }, + "rules": [ + ] + } \ No newline at end of file diff --git a/tests/test_local_sampling_benchmark.py b/tests/test_local_sampling_benchmark.py new file mode 100644 index 00000000..74e69cb2 --- /dev/null +++ b/tests/test_local_sampling_benchmark.py @@ -0,0 +1,16 @@ +import json +import pkgutil +from pkg_resources import resource_filename + +# Faster +def test_pkgutil_static_read(benchmark): + def get_sampling_rule(): + return json.loads(pkgutil.get_data(__name__, 'mock_sampling_rule.json')) + benchmark(get_sampling_rule) + +# Slower +def test_pkg_resources_static_read(benchmark): + def get_sampling_rule(): + with open(resource_filename(__name__, 'mock_sampling_rule.json')) as f: + return json.load(f) + benchmark(get_sampling_rule) diff --git a/tox.ini b/tox.ini index 8173d485..130ecc2c 100644 --- a/tox.ini +++ b/tox.ini @@ -16,6 +16,7 @@ skip_missing_interpreters = True passenv = TOXENV CI TRAVIS TRAVIS_* CODECOV_* deps = pytest > 3.0.0 + pytest-benchmark coverage==4.5.4 codecov requests