GitLab is used only for code review, issue tracking and project management. Canonical locations for source code are still https://gitweb.torproject.org/ https://git.torproject.org/ and git-rw.torproject.org.

Commit 1b11b5d9 authored by juga  's avatar juga
Browse files

fix: scaling: Stop returning 1 as the means minima

since they are used as the numerator when calculating the ratio and the
rounding already returns a minimum of 1.
parent b0269321
Pipeline #4211 passed with stage
in 24 minutes and 58 seconds
......@@ -17,19 +17,20 @@ def bw_filt(bw_measurements):
It is the equivalent to Torflow's ``filt_sbw``.
``mu`` in this function is the equivalent to Torflow's ``sbw``.
"""
# It's safe to return 0 here, because:
# 1. this value will be the numerator when calculating the ratio.
# 2. `kb_round_x_sig_dig` returns a minimum of 1.
# This should never be the case, as the measurements come from successful
# results.
if not bw_measurements:
return 1
return 0
# Torflow is rounding to an integer, so is `bw_mean_from_results` in
# `v3bwfile.py`
mu = round(mean(bw_measurements))
bws_gte_mean = list(filter(lambda bw: bw >= mu, bw_measurements))
if bws_gte_mean:
# This could end up being 0 but since the ratio takes the maximum
# of this and the mean, there won't be scaled values being 0.
return round(mean(bws_gte_mean))
return 1
return 0
def network_means_by_relay_type(bw_lines, router_statuses_d):
......
......@@ -875,9 +875,12 @@ class V3BWLine(object):
def bw_mean_from_results(results):
bws = [dl['amount'] / dl['duration']
for r in results for dl in r.downloads]
# It's safe to return 0 here, because:
# 1. this value will be the numerator when calculating the ratio.
# 2. `kb_round_x_sig_dig` returns a minimum of 1.
if bws:
return max(round(mean(bws)), 1)
return 1
return round(mean(bws))
return 0
@staticmethod
def last_time_from_results(results):
......
......@@ -16,7 +16,7 @@ def test_bw_filt():
# When there are no measurements what can not be the case for successful
# results.
bw_measurements = []
assert 1 == scaling.bw_filt(bw_measurements)
assert 0 == scaling.bw_filt(bw_measurements)
bw_measurements = [1, 0]
# Because rounded to int
......
......@@ -331,7 +331,7 @@ def test_torflow_scale(mock_consensus, datadir, tmpdir, conf):
state_fpath,
scaling_method=TORFLOW_SCALING,
round_digs=TORFLOW_ROUND_DIG)
assert v3bwfile.bw_lines[0].bw == 8
assert v3bwfile.bw_lines[0].bw == 6
v3bwfile = V3BWFile.from_results(results, '', '',
state_fpath,
scaling_method=TORFLOW_SCALING,
......@@ -344,13 +344,13 @@ def test_torflow_scale(mock_consensus, datadir, tmpdir, conf):
scaling_method=TORFLOW_SCALING,
torflow_cap=1,
round_digs=TORFLOW_ROUND_DIG)
assert v3bwfile.bw_lines[0].bw == 168
assert v3bwfile.bw_lines[0].bw == 123
v3bwfile = V3BWFile.from_results(results, '', '',
state_fpath,
scaling_method=TORFLOW_SCALING,
torflow_cap=1,
round_digs=PROP276_ROUND_DIG)
assert v3bwfile.bw_lines[0].bw == 170
assert v3bwfile.bw_lines[0].bw == 120
def test_torflow_scale_no_desc_bw_avg(datadir, conf, caplog):
......@@ -358,7 +358,7 @@ def test_torflow_scale_no_desc_bw_avg(datadir, conf, caplog):
results = load_result_file(str(datadir.join("results_no_desc_bw_avg.txt")))
caplog.set_level(logging.DEBUG)
v3bwfile = V3BWFile.from_results(results, '', '', state_fpath)
assert v3bwfile.bw_lines[0].bw == 36
assert v3bwfile.bw_lines[0].bw == 26
def test_torflow_scale_no_desc_bw_obs(datadir, conf, caplog):
......@@ -366,7 +366,7 @@ def test_torflow_scale_no_desc_bw_obs(datadir, conf, caplog):
results = load_result_file(str(datadir.join("results_no_desc_bw_obs.txt")))
caplog.set_level(logging.DEBUG)
v3bwfile = V3BWFile.from_results(results, '', '', state_fpath)
assert v3bwfile.bw_lines[0].bw == 41
assert v3bwfile.bw_lines[0].bw == 30
def test_torflow_scale_no_desc_bw_avg_obs(datadir, conf, caplog):
......@@ -376,7 +376,7 @@ def test_torflow_scale_no_desc_bw_avg_obs(datadir, conf, caplog):
)
caplog.set_level(logging.DEBUG)
v3bwfile = V3BWFile.from_results(results, '', '', state_fpath)
assert v3bwfile.bw_lines[0].bw == 41
assert v3bwfile.bw_lines[0].bw == 30
def test_torflow_scale_no_consensus_bw(datadir, conf, caplog):
......@@ -386,7 +386,7 @@ def test_torflow_scale_no_consensus_bw(datadir, conf, caplog):
)
caplog.set_level(logging.DEBUG)
v3bwfile = V3BWFile.from_results(results, '', '', state_fpath)
assert v3bwfile.bw_lines[0].bw == 36
assert v3bwfile.bw_lines[0].bw == 26
def test_torflow_scale_0_consensus_bw(datadir, conf, caplog):
......@@ -394,7 +394,7 @@ def test_torflow_scale_0_consensus_bw(datadir, conf, caplog):
results = load_result_file(str(datadir.join("results_0_consensus_bw.txt")))
caplog.set_level(logging.DEBUG)
v3bwfile = V3BWFile.from_results(results, '', '', state_fpath)
assert v3bwfile.bw_lines[0].bw == 36
assert v3bwfile.bw_lines[0].bw == 26
def test_results_away_each_other(datadir):
......
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