public static int getHttpStatusCode(String url) throws Exception { URLConnection connection = connectToUrl(url); long startTime = System.currentTimeMillis(); int status = ((HttpURLConnection) connection).getResponseCode(); // read fully if possible, then close everything, trying to prevent cached threads at server consumeAndCloseQuietly((HttpURLConnection) connection); if (LOG.isDebugEnabled()) LOG.debug("connection to {} ({}ms) gives {}", new Object[] { url, (System.currentTimeMillis()-startTime), status }); return status; }
public static String getErrorContent(String url) { try { HttpURLConnection connection = (HttpURLConnection) connectToUrl(url); long startTime = System.currentTimeMillis(); String err; int status; try { InputStream errStream = connection.getErrorStream(); err = Streams.readFullyStringAndClose(errStream); status = connection.getResponseCode(); } finally { closeQuietly(connection); } if (LOG.isDebugEnabled()) LOG.debug("read of err {} ({}ms) complete; http code {}", new Object[] { url, Time.makeTimeStringRounded(System.currentTimeMillis()-startTime), status}); return err; } catch (Exception e) { throw Exceptions.propagate(e); } }
@Override public Void call() throws Exception { String url = entity.getAttribute(WebAppService.ROOT_URL) + "does_not_exist"; final int desiredMsgsPerSec = 10; Stopwatch stopwatch = Stopwatch.createStarted(); final AtomicInteger reqsSent = new AtomicInteger(); final Integer preRequestCount = entity.getAttribute(WebAppService.REQUEST_COUNT); // need to maintain n requests per second for the duration of the window size log.info("Applying load for "+WebAppServiceMethods.DEFAULT_WINDOW_DURATION); while (stopwatch.elapsed(TimeUnit.MILLISECONDS) < WebAppServiceMethods.DEFAULT_WINDOW_DURATION.toMilliseconds()) { long preReqsTime = stopwatch.elapsed(TimeUnit.MILLISECONDS); for (int i = 0; i < desiredMsgsPerSec; i++) { connectToUrl(url); } Time.sleep(1000 - (stopwatch.elapsed(TimeUnit.MILLISECONDS)-preReqsTime)); reqsSent.addAndGet(desiredMsgsPerSec); } Asserts.succeedsEventually(MutableMap.of("timeout", 4000), new Runnable() { @Override public void run() { Double avgReqs = entity.getAttribute(WebAppService.REQUESTS_PER_SECOND_IN_WINDOW); Integer requestCount = entity.getAttribute(WebAppService.REQUEST_COUNT); log.info("avg-requests="+avgReqs+"; total-requests="+requestCount); assertEquals(avgReqs.doubleValue(), desiredMsgsPerSec, 3.0d); assertEquals(requestCount.intValue(), preRequestCount+reqsSent.get()); }}); return null; }});
URLConnection connection = HttpTestUtils.connectToUrl(url); int status = ((HttpURLConnection) connection).getResponseCode(); log.info("connection to {} gives {}", url, status);
HttpTestUtils.connectToUrl(url); EntityAsserts.assertAttributeEventually(entity, WebAppServiceMetrics.REQUEST_COUNT, new Predicate<Integer>() { @Override public boolean apply(Integer input) {
connectToUrl(tc.getAttribute(TomcatServerImpl.ROOT_URL));