Skip to content
GitLab
Menu
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
The Tor Project
Network Health
sbws
Commits
69264308
Unverified
Commit
69264308
authored
Nov 15, 2018
by
teor
Browse files
Round bandwidths to 2 significant digits by default
Implements part of proposal 276. Implements 28451.
parent
41714b06
Changes
4
Hide whitespace changes
Inline
Side-by-side
sbws/core/generate.py
View file @
69264308
from
math
import
ceil
from
sbws.globals
import
(
fail_hard
,
SBWS_SCALE_CONSTANT
,
TORFLOW_SCALING
,
SBWS_SCALING
,
TORFLOW_BW_MARGIN
,
TORFLOW_ROUND
_D
IG
,
DAY_SECS
,
NUM_MIN_RESULTS
)
SBWS_SCALING
,
TORFLOW_BW_MARGIN
,
TORFLOW_ROUNDI
N
G
,
PROP276_ROUND_DIG
,
DAY_SECS
,
NUM_MIN_RESULTS
)
from
sbws.lib.v3bwfile
import
V3BWFile
from
sbws.lib.resultdump
import
load_recent_results_in_datadir
from
argparse
import
ArgumentDefaultsHelpFormatter
...
...
@@ -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 "
...
...
sbws/globals.py
View file @
69264308
...
...
@@ -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
...
...
sbws/lib/v3bwfile.py
View file @
69264308
...
...
@@ -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
,
...
...
@@ -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
Posi
a
ble values: {N
ONE
, SBWS_SCALING, TORFLOW_SCALING} = {0, 1, 2}
Pos
s
ible values: {N
one
, 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.
...
...
tests/unit/lib/test_v3bwfile.py
View file @
69264308
...
...
@@ -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
==
52
4
assert
v3bwfile
.
bw_lines
[
0
].
bw
==
52
0
v3bwfile
=
V3BWFile
.
from_results
(
results
,
scaling_method
=
TORFLOW_SCALING
,
torflow_cap
=
0.0001
)
assert
v3bwfile
.
bw_lines
[
0
].
bw
==
52
4
assert
v3bwfile
.
bw_lines
[
0
].
bw
==
52
0
v3bwfile
=
V3BWFile
.
from_results
(
results
,
scaling_method
=
TORFLOW_SCALING
,
torflow_cap
=
1
,
torflow_round_digs
=
1
)
assert
v3bwfile
.
bw_lines
[
0
].
bw
==
500
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment