Fix two issues found using metrics-test.

- Make unit test locale-independent, similar to Onionoo's 860228c.

 - Replace deprecated method Class.newInstance().
This commit is contained in:
Karsten Loesing 2019-09-16 16:29:10 +02:00
parent 412ac16376
commit 5d98d4806e
5 changed files with 21 additions and 14 deletions

View File

@ -13,6 +13,7 @@ import java.io.IOException;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.SortedMap;
@ -176,8 +177,8 @@ public class Aggregator {
/* Put together all aggregated values in a single line. */
String date = e.getKey();
int numStats = weightedValues.size();
sb.append(String.format("%s,%s,%.0f,%.0f,%.0f,%.8f,%d%n", date,
type, weightedMean, weightedMedian, weightedInterquartileMean,
sb.append(String.format(Locale.US, "%s,%s,%.0f,%.0f,%.0f,%.8f,%d%n",
date, type, weightedMean, weightedMedian, weightedInterquartileMean,
sumFraction, numStats));
}
}

View File

@ -8,6 +8,7 @@ import org.slf4j.LoggerFactory;
import java.util.Collections;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
/** Computed fraction of hidden-service activity that a single relay is
@ -116,9 +117,9 @@ public class ComputedNetworkFractions implements Document {
validAfterDate);
String second = validAfterHour
+ (this.fractionRendRelayedCells == 0.0 ? ","
: String.format(",%f", this.fractionRendRelayedCells))
: String.format(Locale.US, ",%f", this.fractionRendRelayedCells))
+ (this.fractionDirOnionsSeen == 0.0 ? ","
: String.format(",%f", this.fractionDirOnionsSeen));
: String.format(Locale.US, ",%f", this.fractionDirOnionsSeen));
return new String[] { first, second };
}

View File

@ -13,6 +13,7 @@ import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.LineNumberReader;
import java.lang.reflect.InvocationTargetException;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
@ -136,7 +137,7 @@ public class DocumentStore<T extends Document> {
&& !formattedString0.startsWith(prefix)) {
/* Skip line not starting with prefix. */
} else {
T document = this.clazz.newInstance();
T document = this.clazz.getDeclaredConstructor().newInstance();
if (!document.parse(new String[] { formattedString0,
line.substring(1) })) {
log.warn("Unable to read line {} from {}. Not retrieving any "
@ -151,7 +152,8 @@ public class DocumentStore<T extends Document> {
log.warn("Unable to read {}. Not retrieving any previously stored "
+ "documents.", documentFile.getAbsolutePath(), e);
return null;
} catch (InstantiationException | IllegalAccessException e) {
} catch (InstantiationException | IllegalAccessException
| NoSuchMethodException | InvocationTargetException e) {
log.warn("Unable to read {}. Cannot instantiate document object.",
documentFile.getAbsolutePath(), e);
return null;

View File

@ -6,6 +6,8 @@ package org.torproject.metrics.stats.hidserv;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Locale;
/** Extrapolated network totals of hidden-service statistics reported by a
* single relay. Extrapolated values are based on reported statistics and
* computed network fractions in the statistics interval. */
@ -116,11 +118,11 @@ public class ExtrapolatedHidServStats implements Document {
DateTimeHelper.ISO_DATE_FORMAT);
String second = this.fingerprint
+ (this.fractionRendRelayedCells == 0.0 ? ",,"
: String.format(",%.0f,%f", this.extrapolatedRendRelayedCells,
this.fractionRendRelayedCells))
: String.format(Locale.US, ",%.0f,%f",
this.extrapolatedRendRelayedCells, this.fractionRendRelayedCells))
+ (this.fractionDirOnionsSeen == 0.0 ? ",,"
: String.format(",%.0f,%f", this.extrapolatedDirOnionsSeen,
this.fractionDirOnionsSeen));
: String.format(Locale.US, ",%.0f,%f",
this.extrapolatedDirOnionsSeen, this.fractionDirOnionsSeen));
return new String[] { first, second };
}

View File

@ -13,6 +13,7 @@ import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Random;
import java.util.SortedMap;
@ -196,8 +197,8 @@ public class Simulate {
totalInterquartileProbability += extrapolation[2];
}
}
sb.append(String.format("%d,%.2f,%.0f,%.0f,%.0f%n", run, fraction,
totalValues / totalProbability, weightedMedian,
sb.append(String.format(Locale.US, "%d,%.2f,%.0f,%.0f,%.0f%n", run,
fraction, totalValues / totalProbability, weightedMedian,
totalInterquartileValues / totalInterquartileProbability));
}
return sb.toString();
@ -351,8 +352,8 @@ public class Simulate {
totalInterquartileProbability += extrapolation[2];
}
}
sb.append(String.format("%d,%.2f,%.0f,%.0f,%.0f%n", run, fraction,
totalValues / totalProbability, weightedMedian,
sb.append(String.format(Locale.US, "%d,%.2f,%.0f,%.0f,%.0f%n", run,
fraction, totalValues / totalProbability, weightedMedian,
totalInterquartileValues / totalInterquartileProbability));
}
return sb.toString();