Skip to content

Commit 2b10b9a

Browse files
tzaeschkeTilmann Zäschke
andauthored
AVG fix (#17)
* AVG fix * AVG fix --------- Co-authored-by: Tilmann Zäschke <[email protected]>
1 parent efdd9da commit 2b10b9a

File tree

2 files changed

+35
-21
lines changed

2 files changed

+35
-21
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
2020

2121
- ISD-AS assignment parser broken after website change
2222
[#13](https://github.com/netsec-ethz/scion-java-multiping/pull/13)
23+
- Fixed calculation of average and median values
24+
[#17](https://github.com/netsec-ethz/scion-java-multiping/pull/17)
2325

2426
## [0.4.0] - 2025-04-04
2527

src/main/java/org/scion/multiping/PingAll.java

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,12 @@ public static void main(String[] args) throws IOException {
104104
println(" printOnlyICMP=" + SHOW_ONLY_ICMP);
105105

106106
PingAll demo = new PingAll();
107-
List<ParseAssignments.HostEntry> list = DownloadAssignmentsFromWeb.getList();
108-
for (ParseAssignments.HostEntry e : list) {
107+
List<ParseAssignments.HostEntry> allASes = DownloadAssignmentsFromWeb.getList();
108+
// remove entry for local AS
109+
long localAS = Scion.defaultService().getLocalIsdAs();
110+
allASes = allASes.stream().filter(e -> e.getIsdAs() != localAS).collect(Collectors.toList());
111+
// Process all ASes
112+
for (ParseAssignments.HostEntry e : allASes) {
109113
print(ScionUtil.toStringIA(e.getIsdAs()) + " \"" + e.getName() + "\" ");
110114
demo.runDemo(e);
111115
listedAs.add(e.getIsdAs());
@@ -120,27 +124,26 @@ public static void main(String[] args) throws IOException {
120124
}
121125

122126
// max:
123-
Result maxPing =
124-
results.stream().max((o1, o2) -> (int) (o1.getPingMs() - o2.getPingMs())).get();
125-
Result maxHops = results.stream().max(Comparator.comparingInt(Result::getHopCount)).get();
126-
Result maxPaths = results.stream().max(Comparator.comparingInt(Result::getPathCount)).get();
127+
Result maxPing = max(Result::isSuccess, (o1, o2) -> (int) (o1.getPingMs() - o2.getPingMs()));
128+
Result maxHops = max(r -> r.getHopCount() > 0, Comparator.comparingInt(Result::getHopCount));
129+
Result maxPaths = max(r -> r.getPathCount() > 0, Comparator.comparingInt(Result::getPathCount));
127130

128131
// avg/median:
129-
double avgPing = avg(results, Result::isSuccess, Result::getPingMs);
130-
double avgHops = avg(results, r -> r.getHopCount() > 0, Result::getHopCount);
131-
double avgPaths = avg(results, r -> r.getPathCount() > 0, Result::getPathCount);
132-
double medianPing = median(results, Result::isSuccess, Result::getPingMs).orElse(-1.0);
133-
int medianHops = median(results, r -> r.getHopCount() > 0, Result::getHopCount).orElse(-1);
134-
int medianPaths = median(results, r -> r.getPathCount() > 0, Result::getPathCount).orElse(-1);
132+
double avgPing = avg(Result::isSuccess, Result::getPingMs);
133+
double avgHops = avg(r -> r.getHopCount() > 0, Result::getHopCount);
134+
double avgPaths = avg(r -> r.getPathCount() > 0, Result::getPathCount);
135+
double medianPing = median(Result::isSuccess, Result::getPingMs);
136+
double medianHops = median(r -> r.getHopCount() > 0, Result::getHopCount);
137+
double medianPaths = median(r -> r.getPathCount() > 0, Result::getPathCount);
135138

136139
println("");
137140
println("Max hops = " + maxHops.getHopCount() + ": " + maxHops);
138141
println("Max ping [ms] = " + round(maxPing.getPingMs(), 2) + ": " + maxPing);
139142
println("Max paths = " + maxPaths.getPathCount() + ": " + maxPaths);
140143

141-
println("Median hops = " + medianHops);
144+
println("Median hops = " + (int) medianHops);
142145
println("Median ping [ms] = " + round(medianPing, 2));
143-
println("Median paths = " + medianPaths);
146+
println("Median paths = " + (int) medianPaths);
144147

145148
println("Avg hops = " + round(avgHops, 1));
146149
println("Avg ping [ms] = " + round(avgPing, 2));
@@ -439,14 +442,23 @@ public void onException(Throwable t) {
439442
return best;
440443
}
441444

442-
private static double avg(
443-
List<Result> list, Predicate<Result> filter, ToDoubleFunction<Result> mapper) {
444-
return list.stream().filter(filter).mapToDouble(mapper).average().orElse(-1);
445+
private static double avg(Predicate<Result> filter, ToDoubleFunction<Result> mapper) {
446+
return results.stream().filter(filter).mapToDouble(mapper).average().orElse(-1);
445447
}
446448

447-
private static <T> Optional<T> median(
448-
List<Result> list, Predicate<Result> filter, Function<Result, T> mapper) {
449-
List<T> list2 = list.stream().filter(filter).map(mapper).sorted().collect(Collectors.toList());
450-
return list2.isEmpty() ? Optional.empty() : Optional.of(list2.get(list2.size() / 2));
449+
private static Result max(Predicate<Result> filter, Comparator<Result> comparator) {
450+
return results.stream().filter(filter).max(comparator).orElseThrow(NoSuchElementException::new);
451+
}
452+
453+
private static <T> double median(Predicate<Result> filter, Function<Result, T> mapper) {
454+
List<T> list =
455+
results.stream().filter(filter).map(mapper).sorted().collect(Collectors.toList());
456+
if (list.isEmpty()) {
457+
return -1;
458+
}
459+
if (list.get(0) instanceof Double) {
460+
return (Double) list.get(list.size() / 2);
461+
}
462+
return (Integer) list.get(list.size() / 2);
451463
}
452464
}

0 commit comments

Comments
 (0)