[tor-commits] [metrics-lib/master] Order exit-stats numerically, not alphanumerically.
karsten at torproject.org
karsten at torproject.org
Thu Apr 26 14:15:27 UTC 2012
commit cd2fb43d1511568ee33a3b674bed79f84e397de4
Author: Karsten Loesing <karsten.loesing at gmx.net>
Date: Thu Apr 26 08:47:41 2012 +0200
Order exit-stats numerically, not alphanumerically.
---
.../descriptor/impl/ExtraInfoDescriptorImpl.java | 45 +++++++++++++++----
.../impl/ExtraInfoDescriptorImplTest.java | 37 +++++++++++++---
2 files changed, 66 insertions(+), 16 deletions(-)
diff --git a/src/org/torproject/descriptor/impl/ExtraInfoDescriptorImpl.java b/src/org/torproject/descriptor/impl/ExtraInfoDescriptorImpl.java
index 763c9bf..3520934 100644
--- a/src/org/torproject/descriptor/impl/ExtraInfoDescriptorImpl.java
+++ b/src/org/torproject/descriptor/impl/ExtraInfoDescriptorImpl.java
@@ -5,6 +5,7 @@ package org.torproject.descriptor.impl;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Arrays;
+import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Scanner;
@@ -448,20 +449,48 @@ public class ExtraInfoDescriptorImpl extends DescriptorImpl
private void parseExitKibibytesWrittenLine(String line,
String lineNoOpt, String[] partsNoOpt)
throws DescriptorParseException {
- this.exitKibibytesWritten = ParseHelper.
- parseCommaSeparatedKeyValueList(line, partsNoOpt, 1, 0);
+ this.exitKibibytesWritten = this.sortByPorts(ParseHelper.
+ parseCommaSeparatedKeyValueList(line, partsNoOpt, 1, 0));
}
private void parseExitKibibytesReadLine(String line, String lineNoOpt,
String[] partsNoOpt) throws DescriptorParseException {
- this.exitKibibytesRead = ParseHelper.parseCommaSeparatedKeyValueList(
- line, partsNoOpt, 1, 0);
+ this.exitKibibytesRead = this.sortByPorts(ParseHelper.
+ parseCommaSeparatedKeyValueList(line, partsNoOpt, 1, 0));
}
private void parseExitStreamsOpenedLine(String line, String lineNoOpt,
String[] partsNoOpt) throws DescriptorParseException {
- this.exitStreamsOpened = ParseHelper.parseCommaSeparatedKeyValueList(
- line, partsNoOpt, 1, 0);
+ this.exitStreamsOpened = this.sortByPorts(ParseHelper.
+ parseCommaSeparatedKeyValueList(line, partsNoOpt, 1, 0));
+ }
+
+ private SortedMap<String, Integer> sortByPorts(
+ SortedMap<String, Integer> naturalOrder) {
+ SortedMap<String, Integer> byPortNumber =
+ new TreeMap<String, Integer>(new Comparator<String>() {
+ public int compare(String arg0, String arg1) {
+ int port0 = 0, port1 = 0;
+ try {
+ port1 = Integer.parseInt(arg1);
+ } catch (NumberFormatException e) {
+ return -1;
+ }
+ try {
+ port0 = Integer.parseInt(arg0);
+ } catch (NumberFormatException e) {
+ return 1;
+ }
+ if (port0 < port1) {
+ return -1;
+ } else if (port0 > port1) {
+ return 1;
+ } else {
+ return 0;
+ }
+ }});
+ byPortNumber.putAll(naturalOrder);
+ return byPortNumber;
}
private void parseBridgeStatsEndLine(String line, String lineNoOpt,
@@ -722,10 +751,6 @@ public class ExtraInfoDescriptorImpl extends DescriptorImpl
return this.exitStatsIntervalLength;
}
- /* TODO Add custom comparators to the maps returned by all three
- * exit-stats methods to sort keys alphanumerically, not
- * alphabetically. */
-
private SortedMap<String, Integer> exitKibibytesWritten;
public SortedMap<String, Integer> getExitKibibytesWritten() {
return this.exitKibibytesWritten == null ? null :
diff --git a/test/org/torproject/descriptor/impl/ExtraInfoDescriptorImplTest.java b/test/org/torproject/descriptor/impl/ExtraInfoDescriptorImplTest.java
index 1eab3b9..ae1b87a 100644
--- a/test/org/torproject/descriptor/impl/ExtraInfoDescriptorImplTest.java
+++ b/test/org/torproject/descriptor/impl/ExtraInfoDescriptorImplTest.java
@@ -3,16 +3,13 @@
package org.torproject.descriptor.impl;
import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertNull;
-import static org.junit.Assert.assertTrue;
import java.util.ArrayList;
import java.util.List;
+import java.util.Map;
import org.junit.Test;
-import org.torproject.descriptor.BandwidthHistory;
import org.torproject.descriptor.ExtraInfoDescriptor;
/* Test parsing of extra-info descriptors. */
@@ -801,8 +798,36 @@ public class ExtraInfoDescriptorImplTest {
@Test()
public void testExitStatsValid() throws DescriptorParseException {
- ExitStatsBuilder.createWithDefaultLines();
- /* TODO Check stats parts. */
+ ExtraInfoDescriptor descriptor = ExitStatsBuilder.
+ createWithDefaultLines();
+ assertEquals(1328925579000L, descriptor.getExitStatsEndMillis());
+ assertEquals(86400L, descriptor.getExitStatsIntervalLength());
+ String[] ports = new String[] { "25", "80", "443", "49755",
+ "52563", "52596", "57528", "60912", "61351", "64811", "other" };
+ int[] writtenValues = new int[] { 74647, 31370, 20577, 23, 12, 1111,
+ 4, 11, 6, 3365, 2592};
+ int i = 0;
+ for (Map.Entry<String, Integer> e :
+ descriptor.getExitKibibytesWritten().entrySet()) {
+ assertEquals(ports[i], e.getKey());
+ assertEquals(writtenValues[i++], e.getValue().intValue());
+ }
+ int[] readValues = new int[] { 35562, 1254256, 110279, 9396, 1911,
+ 648, 1188, 1427, 1824, 14, 3054 };
+ i = 0;
+ for (Map.Entry<String, Integer> e :
+ descriptor.getExitKibibytesRead().entrySet()) {
+ assertEquals(ports[i], e.getKey());
+ assertEquals(readValues[i++], e.getValue().intValue());
+ }
+ int[] streamsValues = new int[] { 369748, 64212, 151660, 4, 4, 4, 4,
+ 4, 4, 4, 1212 };
+ i = 0;
+ for (Map.Entry<String, Integer> e :
+ descriptor.getExitStreamsOpened().entrySet()) {
+ assertEquals(ports[i], e.getKey());
+ assertEquals(streamsValues[i++], e.getValue().intValue());
+ }
}
/* TODO Add tests for invalid exit stats. */
More information about the tor-commits
mailing list