Skip to content

Commit cf2e98b

Browse files
Fix how bool values from config.yml are parsed for perf benchmarks (#2993)
* Fix boolean parsing in config.yml for perf benchmarks Boolean values were incorrectly parsed and were always set to True. This bugfix uses code from not yet released attrs 21.3.0 to fix the issue. * do not shadow built-in 'bool' Co-authored-by: Brett Langdon <[email protected]>
1 parent 570b494 commit cf2e98b

File tree

7 files changed

+52
-6
lines changed

7 files changed

+52
-6
lines changed

benchmarks/bm/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
from ._scenario import Scenario
22
from ._scenario import var
3+
from ._scenario import var_bool
34

45

56
__all__ = [
67
"var",
8+
"var_bool",
79
"Scenario",
810
]

benchmarks/bm/_scenario.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,16 @@
55
import pyperf
66
import six
77

8+
from ._to_bool import to_bool
9+
810

911
var = attr.ib
1012

1113

14+
def var_bool(*args, **kwargs):
15+
return attr.ib(*args, **kwargs, converter=to_bool)
16+
17+
1218
def _register(scenario_cls):
1319
"""Registers a scenario for benchmarking."""
1420
# This extends pyperf's runner by registering arguments for the scenario config

benchmarks/bm/_to_bool.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
def to_bool(val):
2+
"""
3+
Copied from
4+
https://github.com/python-attrs/attrs/blob/e84b57ea687942e5344badfe41a2728e24037431/src/attr/converters.py#L114
5+
6+
Please remove after updating attrs to 21.3.0.
7+
8+
Convert "boolean" strings (e.g., from env. vars.) to real booleans.
9+
Values mapping to :code:`True`:
10+
- :code:`True`
11+
- :code:`"true"` / :code:`"t"`
12+
- :code:`"yes"` / :code:`"y"`
13+
- :code:`"on"`
14+
- :code:`"1"`
15+
- :code:`1`
16+
Values mapping to :code:`False`:
17+
- :code:`False`
18+
- :code:`"false"` / :code:`"f"`
19+
- :code:`"no"` / :code:`"n"`
20+
- :code:`"off"`
21+
- :code:`"0"`
22+
- :code:`0`
23+
:raises ValueError: for any other value.
24+
.. versionadded:: 21.3.0
25+
"""
26+
if isinstance(val, str):
27+
val = val.lower()
28+
truthy = {True, "true", "t", "yes", "y", "on", "1", 1}
29+
falsy = {False, "false", "f", "no", "n", "off", "0", 0}
30+
try:
31+
if val in truthy:
32+
return True
33+
if val in falsy:
34+
return False
35+
except TypeError:
36+
# Raised when "val" is not hashable (e.g., lists)
37+
pass
38+
raise ValueError("Cannot convert value to bool: {}".format(val))

benchmarks/django_simple/scenario.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33

44

55
class DjangoSimple(bm.Scenario):
6-
tracer_enabled = bm.var(type=bool)
7-
profiler_enabled = bm.var(type=bool)
6+
tracer_enabled = bm.var_bool()
7+
profiler_enabled = bm.var_bool()
88

99
def run(self):
1010
with utils.server(self) as get_response:

benchmarks/encoder/scenario.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class Encoder(bm.Scenario):
88
ntags = bm.var(type=int)
99
ltags = bm.var(type=int)
1010
nmetrics = bm.var(type=int)
11-
dd_origin = bm.var(type=bool)
11+
dd_origin = bm.var_bool()
1212
encoding = bm.var(type=str)
1313

1414
def run(self):

benchmarks/flask_simple/scenario.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33

44

55
class FlaskSimple(bm.Scenario):
6-
tracer_enabled = bm.var(type=bool)
7-
profiler_enabled = bm.var(type=bool)
6+
tracer_enabled = bm.var_bool()
7+
profiler_enabled = bm.var_bool()
88

99
def run(self):
1010
with utils.server(self) as get_response:

benchmarks/span/scenario.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class Span(bm.Scenario):
99
ntags = bm.var(type=int)
1010
ltags = bm.var(type=int)
1111
nmetrics = bm.var(type=int)
12-
finishspan = bm.var(type=bool)
12+
finishspan = bm.var_bool()
1313

1414
def run(self):
1515
# run scenario to also set tags on spans

0 commit comments

Comments
 (0)