[tor-commits] [sbws/master] Round bandwidths to 2 significant digits by default

juga at torproject.org juga at torproject.org
Tue Nov 20 13:39:49 UTC 2018


commit 6926430808f484b17115186f0aaf564e363b5bb0
Author: teor <teor at torproject.org>
Date:   Thu Nov 15 10:53:30 2018 +1000

    Round bandwidths to 2 significant digits by default
    
    Implements part of proposal 276.
    
    Implements 28451.
---
 sbws/core/generate.py           | 11 +++++------
 sbws/globals.py                 |  1 +
 sbws/lib/v3bwfile.py            | 12 ++++++------
 tests/unit/lib/test_v3bwfile.py | 12 ++++++++++--
 4 files changed, 22 insertions(+), 14 deletions(-)

diff --git a/sbws/core/generate.py b/sbws/core/generate.py
index 78b98b6..1913be7 100644
--- a/sbws/core/generate.py
+++ b/sbws/core/generate.py
@@ -1,8 +1,8 @@
 from math import ceil
 
 from sbws.globals import (fail_hard, SBWS_SCALE_CONSTANT, TORFLOW_SCALING,
-                          SBWS_SCALING, TORFLOW_BW_MARGIN, TORFLOW_ROUND_DIG,
-                          DAY_SECS, NUM_MIN_RESULTS)
+                          SBWS_SCALING, TORFLOW_BW_MARGIN, TORFLOW_ROUNDING,
+                          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 "
diff --git a/sbws/globals.py b/sbws/globals.py
index 5fe0442..217e1a7 100644
--- a/sbws/globals.py
+++ b/sbws/globals.py
@@ -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
diff --git a/sbws/lib/v3bwfile.py b/sbws/lib/v3bwfile.py
index f2077d9..c9ab259 100644
--- a/sbws/lib/v3bwfile.py
+++ b/sbws/lib/v3bwfile.py
@@ -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
-            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.
diff --git a/tests/unit/lib/test_v3bwfile.py b/tests/unit/lib/test_v3bwfile.py
index 4bb8b06..3367c77 100644
--- a/tests/unit/lib/test_v3bwfile.py
+++ b/tests/unit/lib/test_v3bwfile.py
@@ -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





More information about the tor-commits mailing list