commit a62f7cd45013bb237c18175f2d66092c6ba9991e Author: Karsten Loesing karsten.loesing@gmx.net Date: Fri Apr 15 17:04:52 2016 +0200
Recognize Torperf DATEPERCx fields for 0 <= x <= 100.
And don't break if x is non-numeric. Spotted by iwakeh. --- CHANGELOG.md | 2 ++ src/org/torproject/descriptor/TorperfResult.java | 6 ++-- .../descriptor/impl/TorperfResultImpl.java | 32 ++++++++++------------ .../descriptor/impl/TorperfResultImplTest.java | 13 ++++++++- 4 files changed, 32 insertions(+), 21 deletions(-)
diff --git a/CHANGELOG.md b/CHANGELOG.md index d5b9b9a..7d2a246 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,8 @@ - Also accept \r\n as newline in Torperf results files. - Make unrecognized keys of Torperf results available together with the corresponding values, rather than just the whole line. + - In Torperf results, recognize all percentiles of expected bytes + read for 0 <= x <= 100 rather than just x = { 10, 20, ..., 90 }.
* Minor changes - Include a Torperf results line with more than one unrecognized diff --git a/src/org/torproject/descriptor/TorperfResult.java b/src/org/torproject/descriptor/TorperfResult.java index 0c75cb2..9c47a61 100644 --- a/src/org/torproject/descriptor/TorperfResult.java +++ b/src/org/torproject/descriptor/TorperfResult.java @@ -55,9 +55,9 @@ public interface TorperfResult extends Descriptor { * if the torperf line didn't contain that information. */ public Boolean didTimeout();
- /* Return the times when x% of expected bytes were read for x = { 10, - * 20, 30, 40, 50, 60, 70, 80, 90 }, or null if the torperf line didn't - * contain that information. */ + /* Return the times when x% of expected bytes were read for + * 0 <= x <= 100, or null if the torperf line didn't contain that + * information. */ public SortedMap<Integer, Long> getDataPercentiles();
/* Return the time when the circuit was launched, or -1 if the torperf diff --git a/src/org/torproject/descriptor/impl/TorperfResultImpl.java b/src/org/torproject/descriptor/impl/TorperfResultImpl.java index 94e756e..62cf68d 100644 --- a/src/org/torproject/descriptor/impl/TorperfResultImpl.java +++ b/src/org/torproject/descriptor/impl/TorperfResultImpl.java @@ -275,22 +275,28 @@ public class TorperfResultImpl extends DescriptorImpl } }
- private Set<String> unparsedPercentiles = new HashSet<>( - Arrays.asList("10,20,30,40,50,60,70,80,90".split(","))); private void parseDataPercentile(String value, String keyValue, String line) throws DescriptorParseException { String key = keyValue.substring(0, keyValue.indexOf("=")); String percentileString = key.substring("DATAPERC".length()); - if (!this.unparsedPercentiles.contains(percentileString)) { + int percentile = -1; + try { + percentile = Integer.parseInt(percentileString); + } catch (NumberFormatException e) { + /* Treat key as unrecognized below. */ + percentile = -1; + } + if (percentile < 0 || percentile > 100) { if (this.unrecognizedKeys == null) { this.unrecognizedKeys = new TreeMap<>(); } this.unrecognizedKeys.put(key, value); } else { - this.unparsedPercentiles.remove(percentileString); - int decileIndex = (Integer.parseInt(percentileString) / 10) - 1; long timestamp = this.parseTimestamp(value, keyValue, line); - this.dataDeciles[decileIndex] = timestamp; + if (this.dataPercentiles == null) { + this.dataPercentiles = new TreeMap<>(); + } + this.dataPercentiles.put(percentile, timestamp); } }
@@ -466,18 +472,10 @@ public class TorperfResultImpl extends DescriptorImpl return this.didTimeout; }
- private Long[] dataDeciles = new Long[9]; + private SortedMap<Integer, Long> dataPercentiles; public SortedMap<Integer, Long> getDataPercentiles() { - if (this.dataDeciles == null) { - return null; - } - SortedMap<Integer, Long> result = new TreeMap<>(); - for (int i = 0; i < dataDeciles.length; i++) { - if (dataDeciles[i] > 0L) { - result.put(10 * (i + 1), dataDeciles[i]); - } - } - return result; + return this.dataPercentiles == null ? null + : new TreeMap<>(this.dataPercentiles); }
private long launchMillis = -1L; diff --git a/test/org/torproject/descriptor/impl/TorperfResultImplTest.java b/test/org/torproject/descriptor/impl/TorperfResultImplTest.java index be9221f..b5cde0a 100644 --- a/test/org/torproject/descriptor/impl/TorperfResultImplTest.java +++ b/test/org/torproject/descriptor/impl/TorperfResultImplTest.java @@ -44,7 +44,7 @@ public class TorperfResultImplTest { }
@Test() - public void testAllAnnotatedInput() throws Exception{ + public void testAllAnnotatedInput() throws Exception { byte[] asciiBytes = (torperfAnnotation + input + torperfAnnotation + input + torperfAnnotation + input).getBytes("US-ASCII"); @@ -82,5 +82,16 @@ public class TorperfResultImplTest { + "READBYTES=51416 REQUEST=1441065601.86 RESPONSE=1441065602.38 " + "SOCKET=1441065601.86 SOURCE=moria START=1441065601.86 " + "TIMEOUT=1500 USED_AT=1441065603.40 USED_BY=2475 WRITEBYTES=75\n"; + + @Test() + public void testDatapercNonNumeric() throws Exception { + List<Descriptor> result = TorperfResultImpl.parseTorperfResults( + ("DATAPERMILLE=2.0 " + input).getBytes(), false); + assertEquals(1, result.size()); + TorperfResultImpl torperfResult = (TorperfResultImpl) result.get(0); + assertEquals(1, torperfResult.getUnrecognizedKeys().size()); + assertEquals("DATAPERMILLE", + torperfResult.getUnrecognizedKeys().firstKey()); + } }
tor-commits@lists.torproject.org