|
24 | 24 | import java.util.concurrent.ConcurrentHashMap;
|
25 | 25 | import java.util.concurrent.CountDownLatch;
|
26 | 26 | import java.util.concurrent.TimeUnit;
|
| 27 | +import java.util.function.Function; |
| 28 | +import java.util.function.Predicate; |
| 29 | +import java.util.function.ToDoubleFunction; |
27 | 30 | import java.util.stream.Collectors;
|
28 |
| - |
29 | 31 | import org.scion.jpan.*;
|
30 | 32 | import org.scion.jpan.internal.PathRawParser;
|
31 | 33 | import org.scion.multiping.util.*;
|
@@ -89,7 +91,7 @@ private enum Policy {
|
89 | 91 | }
|
90 | 92 |
|
91 | 93 | private static final Policy POLICY = Policy.FASTEST_TR_ASYNC;
|
92 |
| - private static final boolean SHOW_PATH = !true; |
| 94 | + private static final boolean SHOW_PATH = false; |
93 | 95 |
|
94 | 96 | public static void main(String[] args) throws IOException {
|
95 | 97 | PRINT = true;
|
@@ -124,18 +126,12 @@ public static void main(String[] args) throws IOException {
|
124 | 126 | Result maxPaths = results.stream().max(Comparator.comparingInt(Result::getPathCount)).get();
|
125 | 127 |
|
126 | 128 | // avg/median:
|
127 |
| - double avgPing = |
128 |
| - results.stream().filter(Result::isSuccess).mapToDouble(Result::getPingMs).average().orElse(-1); |
129 |
| - double avgHops = |
130 |
| - results.stream().filter(r -> r.getHopCount() > 0).mapToInt(Result::getHopCount).average().orElse(-1); |
131 |
| - double avgPaths = |
132 |
| - results.stream().filter(r -> r.getPathCount() > 0).mapToInt(Result::getPathCount).average().orElse(-1); |
133 |
| - List<Double> pings = results.stream().filter(Result::isSuccess).map(Result::getPingMs).sorted().collect(Collectors.toList()); |
134 |
| - double medianPing = pings.isEmpty() ? -1 : pings.get(pings.size() / 2); |
135 |
| - List<Integer> hops = results.stream().map(Result::getHopCount).filter(hopCount -> hopCount > 0).sorted().collect(Collectors.toList()); |
136 |
| - int medianHops = hops.isEmpty() ? -1 : hops.get(hops.size() / 2); |
137 |
| - List<Integer> paths = results.stream().map(Result::getPathCount).filter(pathCount -> pathCount > 0).sorted().collect(Collectors.toList()); |
138 |
| - int medianPaths = paths.isEmpty() ? -1 : paths.get(paths.size() / 2); |
| 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); |
139 | 135 |
|
140 | 136 | println("");
|
141 | 137 | println("Max hops = " + maxHops.getHopCount() + ": " + maxHops);
|
@@ -404,15 +400,16 @@ public void onException(Throwable t) {
|
404 | 400 | }
|
405 | 401 | }
|
406 | 402 |
|
407 |
| - // Wait for all messages to be received |
408 |
| - try { |
409 |
| - if (!barrier.await(1100, TimeUnit.MILLISECONDS)) { |
410 |
| - throw new IllegalStateException("Missing messages: " + barrier.getCount() + "/" + paths.size()); |
411 |
| - } |
412 |
| - } catch (InterruptedException e) { |
| 403 | + // Wait for all messages to be received |
| 404 | + try { |
| 405 | + if (!barrier.await(1100, TimeUnit.MILLISECONDS)) { |
| 406 | + throw new IllegalStateException( |
| 407 | + "Missing messages: " + barrier.getCount() + "/" + paths.size()); |
| 408 | + } |
| 409 | + } catch (InterruptedException e) { |
413 | 410 | Thread.currentThread().interrupt();
|
414 | 411 | throw new IllegalStateException(e);
|
415 |
| - } |
| 412 | + } |
416 | 413 |
|
417 | 414 | } catch (IOException e) {
|
418 | 415 | println("ERROR: " + e.getMessage());
|
@@ -441,4 +438,15 @@ public void onException(Throwable t) {
|
441 | 438 | }
|
442 | 439 | return best;
|
443 | 440 | }
|
| 441 | + |
| 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 | + } |
| 446 | + |
| 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)); |
| 451 | + } |
444 | 452 | }
|
0 commit comments