@Test public void testFindLongResponseTimes() throws URISyntaxException { final Map<NodeIdentifier, NodeResponse> responses = new HashMap<>(); final NodeIdentifier id1 = new NodeIdentifier("1", "localhost", 8000, "localhost", 8001, "localhost", 8002, 8003, false); final NodeIdentifier id2 = new NodeIdentifier("2", "localhost", 8200, "localhost", 8201, "localhost", 8202, 8203, false); final NodeIdentifier id3 = new NodeIdentifier("3", "localhost", 8300, "localhost", 8301, "localhost", 8302, 8303, false); final NodeIdentifier id4 = new NodeIdentifier("4", "localhost", 8400, "localhost", 8401, "localhost", 8402, 8403, false); final URI uri = new URI("localhost:8080"); final ClientResponse clientResponse = Mockito.mock(ClientResponse.class); responses.put(id1, new NodeResponse(id1, "GET", uri, clientResponse, TimeUnit.MILLISECONDS.toNanos(80), "1")); responses.put(id2, new NodeResponse(id1, "GET", uri, clientResponse, TimeUnit.MILLISECONDS.toNanos(92), "1")); responses.put(id3, new NodeResponse(id1, "GET", uri, clientResponse, TimeUnit.MILLISECONDS.toNanos(3), "1")); responses.put(id4, new NodeResponse(id1, "GET", uri, clientResponse, TimeUnit.MILLISECONDS.toNanos(120), "1")); final AsyncClusterResponse response = new AsyncClusterResponse() { @Override public String getRequestIdentifier() { return "1"; } @Override public String getMethod() { return "GET"; } @Override public String getURIPath() { return null; } @Override public Set<NodeIdentifier> getNodesInvolved() { return new HashSet<>(responses.keySet()); } @Override public Set<NodeIdentifier> getCompletedNodeIdentifiers() { return getNodesInvolved(); } @Override public boolean isComplete() { return true; } @Override public boolean isOlderThan(long time, TimeUnit timeUnit) { return true; } @Override public NodeResponse getMergedResponse() { return null; } @Override public NodeResponse awaitMergedResponse() throws InterruptedException { return null; } @Override public NodeResponse awaitMergedResponse(long timeout, TimeUnit timeUnit) throws InterruptedException { return null; } @Override public NodeResponse getNodeResponse(NodeIdentifier nodeId) { return responses.get(nodeId); } @Override public Set<NodeResponse> getCompletedNodeResponses() { return new HashSet<>(responses.values()); } }; Set<NodeIdentifier> slowResponses = ResponseUtils.findLongResponseTimes(response, 1.5D); assertTrue(slowResponses.isEmpty()); responses.put(id4, new NodeResponse(id1, "GET", uri, clientResponse, TimeUnit.MILLISECONDS.toNanos(2500), "1")); slowResponses = ResponseUtils.findLongResponseTimes(response, 1.5D); assertEquals(1, slowResponses.size()); assertEquals(id4, slowResponses.iterator().next()); }
public static Set<NodeIdentifier> findLongResponseTimes(final AsyncClusterResponse response, final double stdDeviationMultiple) { final Set<NodeIdentifier> slowResponses = new HashSet<>(); if (response.isOlderThan(1, TimeUnit.SECONDS)) { final Set<NodeIdentifier> completedIds = response.getCompletedNodeIdentifiers(); if (completedIds.size() < 2) { return slowResponses; } long requestMillisSum = 0L; int numNodes = 0; for (final NodeIdentifier nodeId : completedIds) { final long requestMillis = response.getNodeResponse(nodeId).getRequestDuration(TimeUnit.NANOSECONDS); if (requestMillis < 0) { continue; } requestMillisSum += requestMillis; numNodes++; } if (numNodes < 2) { return slowResponses; } final double mean = requestMillisSum / numNodes; double differenceSquaredSum = 0D; for (final NodeIdentifier nodeId : completedIds) { final long requestMillis = response.getNodeResponse(nodeId).getRequestDuration(TimeUnit.NANOSECONDS); final double differenceSquared = Math.pow(mean - requestMillis, 2); differenceSquaredSum += differenceSquared; } final double meanOfDifferenceSquared = differenceSquaredSum / numNodes; final double stdDev = Math.pow(meanOfDifferenceSquared, 0.5D); final double longTimeThreshold = mean + stdDev * stdDeviationMultiple; for (final NodeIdentifier nodeId : completedIds) { final long requestMillis = response.getNodeResponse(nodeId).getRequestDuration(TimeUnit.NANOSECONDS); if (requestMillis > longTimeThreshold) { slowResponses.add(nodeId); } } } return slowResponses; }
ResponseUtils { public static Set<NodeIdentifier> findLongResponseTimes(final AsyncClusterResponse response, final double stdDeviationMultiple) { final Set<NodeIdentifier> slowResponses = new HashSet<>(); if (response.isOlderThan(1, TimeUnit.SECONDS)) { final Set<NodeIdentifier> completedIds = response.getCompletedNodeIdentifiers(); if (completedIds.size() < 2) { return slowResponses; } long requestMillisSum = 0L; int numNodes = 0; for (final NodeIdentifier nodeId : completedIds) { final long requestMillis = response.getNodeResponse(nodeId).getRequestDuration(TimeUnit.NANOSECONDS); if (requestMillis < 0) { continue; } requestMillisSum += requestMillis; numNodes++; } if (numNodes < 2) { return slowResponses; } final double mean = requestMillisSum / numNodes; double differenceSquaredSum = 0D; for (final NodeIdentifier nodeId : completedIds) { final long requestMillis = response.getNodeResponse(nodeId).getRequestDuration(TimeUnit.NANOSECONDS); final double differenceSquared = Math.pow(mean - requestMillis, 2); differenceSquaredSum += differenceSquared; } final double meanOfDifferenceSquared = differenceSquaredSum / numNodes; final double stdDev = Math.pow(meanOfDifferenceSquared, 0.5D); final double longTimeThreshold = mean + stdDev * stdDeviationMultiple; for (final NodeIdentifier nodeId : completedIds) { final long requestMillis = response.getNodeResponse(nodeId).getRequestDuration(TimeUnit.NANOSECONDS); if (requestMillis > longTimeThreshold) { slowResponses.add(nodeId); } } } return slowResponses; } }
ResponseUtils { public static Set<NodeIdentifier> findLongResponseTimes(final AsyncClusterResponse response, final double stdDeviationMultiple) { final Set<NodeIdentifier> slowResponses = new HashSet<>(); if (response.isOlderThan(1, TimeUnit.SECONDS)) { final Set<NodeIdentifier> completedIds = response.getCompletedNodeIdentifiers(); if (completedIds.size() < 2) { return slowResponses; } long requestMillisSum = 0L; int numNodes = 0; for (final NodeIdentifier nodeId : completedIds) { final long requestMillis = response.getNodeResponse(nodeId).getRequestDuration(TimeUnit.NANOSECONDS); if (requestMillis < 0) { continue; } requestMillisSum += requestMillis; numNodes++; } if (numNodes < 2) { return slowResponses; } final double mean = requestMillisSum / numNodes; double differenceSquaredSum = 0D; for (final NodeIdentifier nodeId : completedIds) { final long requestMillis = response.getNodeResponse(nodeId).getRequestDuration(TimeUnit.NANOSECONDS); final double differenceSquared = Math.pow(mean - requestMillis, 2); differenceSquaredSum += differenceSquared; } final double meanOfDifferenceSquared = differenceSquaredSum / numNodes; final double stdDev = Math.pow(meanOfDifferenceSquared, 0.5D); final double longTimeThreshold = mean + stdDev * stdDeviationMultiple; for (final NodeIdentifier nodeId : completedIds) { final long requestMillis = response.getNodeResponse(nodeId).getRequestDuration(TimeUnit.NANOSECONDS); if (requestMillis > longTimeThreshold) { slowResponses.add(nodeId); } } } return slowResponses; }  }
ResponseUtils { public static Set<NodeIdentifier> findLongResponseTimes(final AsyncClusterResponse response, final double stdDeviationMultiple) { final Set<NodeIdentifier> slowResponses = new HashSet<>(); if (response.isOlderThan(1, TimeUnit.SECONDS)) { final Set<NodeIdentifier> completedIds = response.getCompletedNodeIdentifiers(); if (completedIds.size() < 2) { return slowResponses; } long requestMillisSum = 0L; int numNodes = 0; for (final NodeIdentifier nodeId : completedIds) { final long requestMillis = response.getNodeResponse(nodeId).getRequestDuration(TimeUnit.NANOSECONDS); if (requestMillis < 0) { continue; } requestMillisSum += requestMillis; numNodes++; } if (numNodes < 2) { return slowResponses; } final double mean = requestMillisSum / numNodes; double differenceSquaredSum = 0D; for (final NodeIdentifier nodeId : completedIds) { final long requestMillis = response.getNodeResponse(nodeId).getRequestDuration(TimeUnit.NANOSECONDS); final double differenceSquared = Math.pow(mean - requestMillis, 2); differenceSquaredSum += differenceSquared; } final double meanOfDifferenceSquared = differenceSquaredSum / numNodes; final double stdDev = Math.pow(meanOfDifferenceSquared, 0.5D); final double longTimeThreshold = mean + stdDev * stdDeviationMultiple; for (final NodeIdentifier nodeId : completedIds) { final long requestMillis = response.getNodeResponse(nodeId).getRequestDuration(TimeUnit.NANOSECONDS); if (requestMillis > longTimeThreshold) { slowResponses.add(nodeId); } } } return slowResponses; }  static Set<NodeIdentifier> findLongResponseTimes(final AsyncClusterResponse response, final double stdDeviationMultiple); }
ResponseUtils { public static Set<NodeIdentifier> findLongResponseTimes(final AsyncClusterResponse response, final double stdDeviationMultiple) { final Set<NodeIdentifier> slowResponses = new HashSet<>(); if (response.isOlderThan(1, TimeUnit.SECONDS)) { final Set<NodeIdentifier> completedIds = response.getCompletedNodeIdentifiers(); if (completedIds.size() < 2) { return slowResponses; } long requestMillisSum = 0L; int numNodes = 0; for (final NodeIdentifier nodeId : completedIds) { final long requestMillis = response.getNodeResponse(nodeId).getRequestDuration(TimeUnit.NANOSECONDS); if (requestMillis < 0) { continue; } requestMillisSum += requestMillis; numNodes++; } if (numNodes < 2) { return slowResponses; } final double mean = requestMillisSum / numNodes; double differenceSquaredSum = 0D; for (final NodeIdentifier nodeId : completedIds) { final long requestMillis = response.getNodeResponse(nodeId).getRequestDuration(TimeUnit.NANOSECONDS); final double differenceSquared = Math.pow(mean - requestMillis, 2); differenceSquaredSum += differenceSquared; } final double meanOfDifferenceSquared = differenceSquaredSum / numNodes; final double stdDev = Math.pow(meanOfDifferenceSquared, 0.5D); final double longTimeThreshold = mean + stdDev * stdDeviationMultiple; for (final NodeIdentifier nodeId : completedIds) { final long requestMillis = response.getNodeResponse(nodeId).getRequestDuration(TimeUnit.NANOSECONDS); if (requestMillis > longTimeThreshold) { slowResponses.add(nodeId); } } } return slowResponses; }  static Set<NodeIdentifier> findLongResponseTimes(final AsyncClusterResponse response, final double stdDeviationMultiple);  }