commit 8dd9d118977366ef8a17ad08a39809cbee735df4 Author: Damian Johnson atagar@torproject.org Date: Sun May 13 14:47:40 2012 -0700
Parsing some more cell-* extrainfo lines
Handling the cell-processed-cells, cell-queued-cells, and cell-time-in-queue entries which are all numeric lists. I was a little surprised to find negative cell-processed-cells values in the wild. --- stem/descriptor/extrainfo_descriptor.py | 24 ++++++++++++++++++ test/unit/descriptor/extrainfo_descriptor.py | 33 +++++++++++++++++++++++++- 2 files changed, 56 insertions(+), 1 deletions(-)
diff --git a/stem/descriptor/extrainfo_descriptor.py b/stem/descriptor/extrainfo_descriptor.py index 64a33b0..7fa008e 100644 --- a/stem/descriptor/extrainfo_descriptor.py +++ b/stem/descriptor/extrainfo_descriptor.py @@ -192,6 +192,9 @@ class ExtraInfoDescriptor(stem.descriptor.Descriptor): Cell relaying statistics: cell_stats_end (datetime) - end of the period when stats were gathered cell_stats_interval (int) - length in seconds of the interval + cell_processed_cells (list) - measurement of processed cells per circuit + cell_queued_cells (list) - measurement of queued cells per circuit + cell_time_in_queue (list) - mean enqueued time in milliseconds for cells
Directory Mirror Attributes: dir_stats_end (datetime) - end of the period when stats were gathered @@ -280,6 +283,9 @@ class ExtraInfoDescriptor(stem.descriptor.Descriptor):
self.cell_stats_end = None self.cell_stats_interval = None + self.cell_processed_cells = None + self.cell_queued_cells = None + self.cell_time_in_queue = None
self.dir_stats_end = None self.dir_stats_interval = None @@ -449,6 +455,24 @@ class ExtraInfoDescriptor(stem.descriptor.Descriptor): except ValueError, exc: if validate: raise ValueError("Value can't be parsed as a percentage: %s" % line) + elif keyword in ("cell-processed-cells", "cell-queued-cells", "cell-time-in-queue"): + # "<keyword>" num,...,num + + entries = [] + + if value: + for entry in value.split(","): + try: + entries.append(float(entry)) + except ValueError: + if validate: raise ValueError("Non-numeric entry in %s listing: %s" % (keyword, line)) + + if keyword == "cell-processed-cells": + self.cell_processed_cells = entries + elif keyword == "cell-queued-cells": + self.cell_queued_cells = entries + elif keyword == "cell-time-in-queue": + self.cell_time_in_queue = entries elif keyword in ("published", "geoip-start-time"): # "<keyword>" YYYY-MM-DD HH:MM:SS
diff --git a/test/unit/descriptor/extrainfo_descriptor.py b/test/unit/descriptor/extrainfo_descriptor.py index 92f37bc..00013c0 100644 --- a/test/unit/descriptor/extrainfo_descriptor.py +++ b/test/unit/descriptor/extrainfo_descriptor.py @@ -238,6 +238,37 @@ class TestExtraInfoDescriptor(unittest.TestCase): desc_text = _make_descriptor({keyword: entry}) self._expect_invalid_attr(desc_text, attr, expected)
+ def test_number_list_lines(self): + """ + Uses valid and invalid data to tests lines of the form... + "<keyword>" num,...,num + """ + + for keyword in ('cell-processed-cells', 'cell-queued-cells', 'cell-time-in-queue'): + attr = keyword.replace('-', '_') + + test_entries = ( + ("", []), + (" ", []), + ("0,0,0", [0.0, 0.0, 0.0]), + ("2.3,-4.6,8.9,16.12,32.15", [2.3, -4.6, 8.9, 16.12, 32.15]), + ) + + for test_value, expected_value in test_entries: + desc_text = _make_descriptor({keyword: test_value}) + desc = ExtraInfoDescriptor(desc_text) + self.assertEquals(expected_value, getattr(desc, attr)) + + test_entries = ( + (",,11", [11.0]), + ("abc,5.7,def", [5.7]), + ("blarg", []), + ) + + for entry, expected in test_entries: + desc_text = _make_descriptor({keyword: entry}) + self._expect_invalid_attr(desc_text, attr, expected) + def test_timestamp_lines(self): """ Uses valid and invalid data to tests lines of the form... @@ -364,7 +395,7 @@ class TestExtraInfoDescriptor(unittest.TestCase):
for entry in test_entries: desc_text = _make_descriptor({keyword: entry}) - desc = self._expect_invalid_attr(desc_text, attr, {}) + self._expect_invalid_attr(desc_text, attr, {})
def _expect_invalid_attr(self, desc_text, attr = None, expected_value = None): """