Commit 28f28326 authored by juga  's avatar juga
Browse files

Merge branch 'ticket28451'

parents 03136450 ae74288f
......@@ -13,6 +13,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
than rounding to the nearest 1000 kilobytes (#28442).
- Continue running with defaults when the config argument is provided
but not found (#28500).
- Make sbws round to 2 significant figures by default. This implements part
of proposal 276 (#28451).
## [1.0.2] - 2018-11-10
......
from math import ceil
from sbws.globals import (fail_hard, SBWS_SCALE_CONSTANT, TORFLOW_SCALING,
SBWS_SCALING, TORFLOW_BW_MARGIN, TORFLOW_ROUND_DIG,
SBWS_SCALING, TORFLOW_BW_MARGIN, PROP276_ROUND_DIG,
DAY_SECS, NUM_MIN_RESULTS)
from sbws.lib.v3bwfile import V3BWFile
from sbws.lib.resultdump import load_recent_results_in_datadir
......@@ -50,10 +50,9 @@ def gen_parser(sub):
p.add_argument('-m', '--torflow-bw-margin', default=TORFLOW_BW_MARGIN,
type=float,
help="Cap maximum bw when scaling as Torflow. ")
p.add_argument('-r', '--torflow-round-digs', default=TORFLOW_ROUND_DIG,
type=int,
help="Number of most significant digits to round bw "
"when scaling as Torflow.")
p.add_argument('-r', '--round-digs', '--torflow-round-digs',
default=PROP276_ROUND_DIG, type=int,
help="Number of most significant digits to round bw.")
p.add_argument('-p', '--secs-recent', default=None, type=int,
help="How many secs in the past are results being "
"still considered. Note this value will supersede "
......
......@@ -40,6 +40,7 @@ TORFLOW_OBS_LAST = 0
TORFLOW_OBS_MEAN = 1
TORFLOW_OBS_DECAYING = 3
TORFLOW_ROUND_DIG = 3
PROP276_ROUND_DIG = 2
DAY_SECS = 86400
NUM_MIN_RESULTS = 2
MIN_REPORT = 60
......
......@@ -14,7 +14,7 @@ from sbws import __version__
from sbws.globals import (SPEC_VERSION, BW_LINE_SIZE, SBWS_SCALE_CONSTANT,
TORFLOW_SCALING, SBWS_SCALING, TORFLOW_BW_MARGIN,
TORFLOW_OBS_LAST, TORFLOW_OBS_MEAN,
TORFLOW_ROUND_DIG, MIN_REPORT, MAX_BW_DIFF_PERC)
PROP276_ROUND_DIG, MIN_REPORT, MAX_BW_DIFF_PERC)
from sbws.lib.resultdump import ResultSuccess, _ResultType
from sbws.util.filelock import DirectoryLock
from sbws.util.timestamp import (now_isodt_str, unixts_to_isodt_str,
......@@ -58,7 +58,7 @@ BW_KEYVALUES_INT = ['bw', 'rtt', 'success', 'error_stream',
BW_KEYVALUES = BW_KEYVALUES_BASIC + BW_KEYVALUES_EXTRA
def round_sig_dig(n, digits=TORFLOW_ROUND_DIG):
def round_sig_dig(n, digits=PROP276_ROUND_DIG):
"""Round n to 'digits' significant digits in front of the decimal point.
Results less than or equal to 1 are rounded to 1.
Returns an integer.
......@@ -66,17 +66,17 @@ def round_sig_dig(n, digits=TORFLOW_ROUND_DIG):
digits must be greater than 0.
n must be less than or equal to 2**73, to avoid floating point errors.
"""
digits = int(digits)
assert digits >= 1
if n <= 1:
return 1
digits = int(digits)
digits_in_n = int(math.log10(n)) + 1
round_digits = max(digits_in_n - digits, 0)
rounded_n = round(n, -round_digits)
return int(rounded_n)
def kb_round_x_sig_dig(bw_bs, digits=TORFLOW_ROUND_DIG):
def kb_round_x_sig_dig(bw_bs, digits=PROP276_ROUND_DIG):
"""Convert bw_bs from bytes to kilobytes, and round the result to
'digits' significant digits.
Results less than or equal to 1 are rounded up to 1.
......@@ -487,7 +487,7 @@ class V3BWFile(object):
scale_constant=SBWS_SCALE_CONSTANT,
scaling_method=None, torflow_obs=TORFLOW_OBS_LAST,
torflow_cap=TORFLOW_BW_MARGIN,
torflow_round_digs=TORFLOW_ROUND_DIG,
torflow_round_digs=PROP276_ROUND_DIG,
secs_recent=None, secs_away=None, min_num=0,
consensus_path=None, max_bw_diff_perc=MAX_BW_DIFF_PERC,
reverse=False):
......@@ -497,7 +497,7 @@ class V3BWFile(object):
:param str state_fpath: path to the state file
:param int scaling_method:
Scaling method to obtain the bandwidth
Posiable values: {NONE, SBWS_SCALING, TORFLOW_SCALING} = {0, 1, 2}
Possible values: {None, SBWS_SCALING, TORFLOW_SCALING} = {0, 1, 2}
:param int scale_constant: sbws scaling constant
:param int torflow_obs: method to choose descriptor observed bandwidth
:param bool reverse: whether to sort the bw lines descending or not
......@@ -639,7 +639,7 @@ class V3BWFile(object):
@staticmethod
def bw_torflow_scale(bw_lines, desc_bw_obs_type=TORFLOW_OBS_MEAN,
cap=TORFLOW_BW_MARGIN,
num_round_dig=TORFLOW_ROUND_DIG, reverse=False):
num_round_dig=PROP276_ROUND_DIG, reverse=False):
"""
Obtain final bandwidth measurements applying Torflow's scaling
method.
......
......@@ -139,6 +139,8 @@ def test_round_sig_dig():
assert(round_sig_dig(24103, 4) == 24100)
assert(round_sig_dig(24103, 5) == 24103)
assert(round_sig_dig(300000, 1) == 300000)
# Floating-point values
# Must round based on fractions, must not double-round
......@@ -165,6 +167,12 @@ def test_round_sig_dig():
assert_round_sig_dig_any_digits(0, 1)
assert_round_sig_dig_any_digits(1, 1)
assert_round_sig_dig_any_digits(2, 2)
assert_round_sig_dig_any_digits(3, 3)
assert_round_sig_dig_any_digits(4, 4)
assert_round_sig_dig_any_digits(5, 5)
assert_round_sig_dig_any_digits(6, 6)
assert_round_sig_dig_any_digits(7, 7)
assert_round_sig_dig_any_digits(8, 8)
assert_round_sig_dig_any_digits(9, 9)
assert_round_sig_dig_any_digits(10, 10)
......@@ -257,10 +265,10 @@ def test_sbws_scale(datadir):
def test_torflow_scale(datadir):
results = load_result_file(str(datadir.join("results.txt")))
v3bwfile = V3BWFile.from_results(results, scaling_method=TORFLOW_SCALING)
assert v3bwfile.bw_lines[0].bw == 524
assert v3bwfile.bw_lines[0].bw == 520
v3bwfile = V3BWFile.from_results(results, scaling_method=TORFLOW_SCALING,
torflow_cap=0.0001)
assert v3bwfile.bw_lines[0].bw == 524
assert v3bwfile.bw_lines[0].bw == 520
v3bwfile = V3BWFile.from_results(results, scaling_method=TORFLOW_SCALING,
torflow_cap=1, torflow_round_digs=1)
assert v3bwfile.bw_lines[0].bw == 500
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment