Search in sources :

Example 26 with CyclicBarrier

use of java.util.concurrent.CyclicBarrier in project jetty.project by eclipse.

the class SameNodeLoadTest method testLoad.

@Test
@Slow
public void testLoad() throws Exception {
    DefaultSessionCacheFactory cacheFactory = new DefaultSessionCacheFactory();
    cacheFactory.setEvictionPolicy(SessionCache.NEVER_EVICT);
    SessionDataStoreFactory storeFactory = new TestSessionDataStoreFactory();
    String contextPath = "";
    String servletMapping = "/server";
    TestServer server1 = new TestServer(0, -1, 4, cacheFactory, storeFactory);
    server1.addContext(contextPath).addServlet(TestServlet.class, servletMapping);
    try {
        server1.start();
        int port1 = server1.getPort();
        HttpClient client = new HttpClient();
        client.start();
        try {
            String url = "http://localhost:" + port1 + contextPath + servletMapping;
            //create session via first server
            ContentResponse response1 = client.GET(url + "?action=init");
            assertEquals(HttpServletResponse.SC_OK, response1.getStatus());
            String sessionCookie = response1.getHeaders().get("Set-Cookie");
            assertTrue(sessionCookie != null);
            // Mangle the cookie, replacing Path with $Path, etc.
            sessionCookie = sessionCookie.replaceFirst("(\\W)(P|p)ath=", "$1\\$Path=");
            //simulate 10 clients making 100 requests each
            ExecutorService executor = Executors.newCachedThreadPool();
            int clientsCount = 10;
            CyclicBarrier barrier = new CyclicBarrier(clientsCount + 1);
            int requestsCount = 100;
            Worker[] workers = new Worker[clientsCount];
            for (int i = 0; i < clientsCount; ++i) {
                workers[i] = new Worker(barrier, client, requestsCount, sessionCookie, url);
                executor.execute(workers[i]);
            }
            // Wait for all workers to be ready
            barrier.await();
            long start = System.nanoTime();
            // Wait for all workers to be done
            barrier.await();
            long end = System.nanoTime();
            long elapsed = TimeUnit.NANOSECONDS.toMillis(end - start);
            System.err.println("Elapsed ms:" + elapsed);
            executor.shutdownNow();
            // Perform one request to get the result
            Request request = client.newRequest(url + "?action=result");
            request.header("Cookie", sessionCookie);
            ContentResponse response2 = request.send();
            assertEquals(HttpServletResponse.SC_OK, response2.getStatus());
            String response = response2.getContentAsString();
            assertEquals(response.trim(), String.valueOf(clientsCount * requestsCount));
        } finally {
            client.stop();
        }
    } finally {
        server1.stop();
    }
}
Also used : ContentResponse(org.eclipse.jetty.client.api.ContentResponse) Request(org.eclipse.jetty.client.api.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) CyclicBarrier(java.util.concurrent.CyclicBarrier) HttpClient(org.eclipse.jetty.client.HttpClient) ExecutorService(java.util.concurrent.ExecutorService) Test(org.junit.Test) Slow(org.eclipse.jetty.toolchain.test.annotation.Slow)

Example 27 with CyclicBarrier

use of java.util.concurrent.CyclicBarrier in project jetty.project by eclipse.

the class StatisticsHandlerTest method testSuspendResume.

@Test
public void testSuspendResume() throws Exception {
    final long dispatchTime = 10;
    final long requestTime = 50;
    final AtomicReference<AsyncContext> asyncHolder = new AtomicReference<>();
    final CyclicBarrier[] barrier = { new CyclicBarrier(2), new CyclicBarrier(2), new CyclicBarrier(2) };
    _statsHandler.setHandler(new AbstractHandler() {

        @Override
        public void handle(String path, Request request, HttpServletRequest httpRequest, HttpServletResponse httpResponse) throws IOException, ServletException {
            request.setHandled(true);
            try {
                barrier[0].await();
                Thread.sleep(dispatchTime);
                if (asyncHolder.get() == null)
                    asyncHolder.set(request.startAsync());
            } catch (Exception x) {
                throw new ServletException(x);
            } finally {
                try {
                    barrier[1].await();
                } catch (Exception ignored) {
                }
            }
        }
    });
    _server.start();
    String request = "GET / HTTP/1.1\r\n" + "Host: localhost\r\n" + "\r\n";
    _connector.executeRequest(request);
    barrier[0].await();
    assertEquals(1, _statistics.getConnections());
    assertEquals(1, _statsHandler.getRequests());
    assertEquals(1, _statsHandler.getRequestsActive());
    assertEquals(1, _statsHandler.getDispatched());
    assertEquals(1, _statsHandler.getDispatchedActive());
    barrier[1].await();
    assertTrue(_latchHandler.await());
    assertNotNull(asyncHolder.get());
    assertEquals(1, _statsHandler.getRequests());
    assertEquals(1, _statsHandler.getRequestsActive());
    assertEquals(1, _statsHandler.getDispatched());
    assertEquals(0, _statsHandler.getDispatchedActive());
    _latchHandler.reset();
    barrier[0].reset();
    barrier[1].reset();
    Thread.sleep(requestTime);
    asyncHolder.get().addListener(new AsyncListener() {

        @Override
        public void onTimeout(AsyncEvent event) throws IOException {
        }

        @Override
        public void onStartAsync(AsyncEvent event) throws IOException {
        }

        @Override
        public void onError(AsyncEvent event) throws IOException {
        }

        @Override
        public void onComplete(AsyncEvent event) throws IOException {
            try {
                barrier[2].await();
            } catch (Exception ignored) {
            }
        }
    });
    asyncHolder.get().dispatch();
    // entered app handler
    barrier[0].await();
    assertEquals(1, _statistics.getConnections());
    assertEquals(1, _statsHandler.getRequests());
    assertEquals(1, _statsHandler.getRequestsActive());
    assertEquals(2, _statsHandler.getDispatched());
    assertEquals(1, _statsHandler.getDispatchedActive());
    // exiting app handler
    barrier[1].await();
    // exited stats handler
    assertTrue(_latchHandler.await());
    // onComplete called
    barrier[2].await();
    assertEquals(1, _statsHandler.getRequests());
    assertEquals(0, _statsHandler.getRequestsActive());
    assertEquals(2, _statsHandler.getDispatched());
    assertEquals(0, _statsHandler.getDispatchedActive());
    assertEquals(1, _statsHandler.getAsyncRequests());
    assertEquals(1, _statsHandler.getAsyncDispatches());
    assertEquals(0, _statsHandler.getExpires());
    assertEquals(1, _statsHandler.getResponses2xx());
    assertThat(_statsHandler.getRequestTimeTotal(), greaterThanOrEqualTo(requestTime * 3 / 4));
    assertEquals(_statsHandler.getRequestTimeTotal(), _statsHandler.getRequestTimeMax());
    assertEquals(_statsHandler.getRequestTimeTotal(), _statsHandler.getRequestTimeMean(), 0.01);
    assertThat(_statsHandler.getDispatchedTimeTotal(), greaterThanOrEqualTo(dispatchTime * 2 * 3 / 4));
    assertTrue(_statsHandler.getDispatchedTimeMean() + dispatchTime <= _statsHandler.getDispatchedTimeTotal());
    assertTrue(_statsHandler.getDispatchedTimeMax() + dispatchTime <= _statsHandler.getDispatchedTimeTotal());
}
Also used : Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) AtomicReference(java.util.concurrent.atomic.AtomicReference) AsyncContext(javax.servlet.AsyncContext) IOException(java.io.IOException) AsyncEvent(javax.servlet.AsyncEvent) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) CyclicBarrier(java.util.concurrent.CyclicBarrier) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) AsyncListener(javax.servlet.AsyncListener) Test(org.junit.Test)

Example 28 with CyclicBarrier

use of java.util.concurrent.CyclicBarrier in project jetty.project by eclipse.

the class StatisticsHandlerTest method testTwoRequests.

@Test
public void testTwoRequests() throws Exception {
    final CyclicBarrier[] barrier = { new CyclicBarrier(3), new CyclicBarrier(3) };
    _latchHandler.reset(2);
    _statsHandler.setHandler(new AbstractHandler() {

        @Override
        public void handle(String path, Request request, HttpServletRequest httpRequest, HttpServletResponse httpResponse) throws IOException, ServletException {
            request.setHandled(true);
            try {
                barrier[0].await();
                barrier[1].await();
            } catch (Exception x) {
                Thread.currentThread().interrupt();
                throw (IOException) new IOException().initCause(x);
            }
        }
    });
    _server.start();
    String request = "GET / HTTP/1.1\r\n" + "Host: localhost\r\n" + "\r\n";
    _connector.executeRequest(request);
    _connector.executeRequest(request);
    barrier[0].await();
    assertEquals(2, _statistics.getConnections());
    assertEquals(2, _statsHandler.getRequests());
    assertEquals(2, _statsHandler.getRequestsActive());
    assertEquals(2, _statsHandler.getRequestsActiveMax());
    assertEquals(2, _statsHandler.getDispatched());
    assertEquals(2, _statsHandler.getDispatchedActive());
    assertEquals(2, _statsHandler.getDispatchedActiveMax());
    barrier[1].await();
    assertTrue(_latchHandler.await());
    assertEquals(2, _statsHandler.getRequests());
    assertEquals(0, _statsHandler.getRequestsActive());
    assertEquals(2, _statsHandler.getRequestsActiveMax());
    assertEquals(2, _statsHandler.getDispatched());
    assertEquals(0, _statsHandler.getDispatchedActive());
    assertEquals(2, _statsHandler.getDispatchedActiveMax());
    assertEquals(0, _statsHandler.getAsyncRequests());
    assertEquals(0, _statsHandler.getAsyncDispatches());
    assertEquals(0, _statsHandler.getExpires());
    assertEquals(2, _statsHandler.getResponses2xx());
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) CyclicBarrier(java.util.concurrent.CyclicBarrier) Test(org.junit.Test)

Example 29 with CyclicBarrier

use of java.util.concurrent.CyclicBarrier in project jetty.project by eclipse.

the class StatisticsHandlerTest method testSuspendComplete.

@Test
public void testSuspendComplete() throws Exception {
    final long dispatchTime = 10;
    final AtomicReference<AsyncContext> asyncHolder = new AtomicReference<>();
    final CyclicBarrier[] barrier = { new CyclicBarrier(2), new CyclicBarrier(2) };
    final CountDownLatch latch = new CountDownLatch(1);
    _statsHandler.setHandler(new AbstractHandler() {

        @Override
        public void handle(String path, Request request, HttpServletRequest httpRequest, HttpServletResponse httpResponse) throws IOException, ServletException {
            request.setHandled(true);
            try {
                barrier[0].await();
                Thread.sleep(dispatchTime);
                if (asyncHolder.get() == null) {
                    AsyncContext async = request.startAsync();
                    asyncHolder.set(async);
                }
            } catch (Exception x) {
                throw new ServletException(x);
            } finally {
                try {
                    barrier[1].await();
                } catch (Exception ignored) {
                }
            }
        }
    });
    _server.start();
    String request = "GET / HTTP/1.1\r\n" + "Host: localhost\r\n" + "\r\n";
    _connector.executeRequest(request);
    barrier[0].await();
    assertEquals(1, _statistics.getConnections());
    assertEquals(1, _statsHandler.getRequests());
    assertEquals(1, _statsHandler.getRequestsActive());
    assertEquals(1, _statsHandler.getDispatched());
    assertEquals(1, _statsHandler.getDispatchedActive());
    barrier[1].await();
    assertTrue(_latchHandler.await());
    assertNotNull(asyncHolder.get());
    assertEquals(1, _statsHandler.getRequests());
    assertEquals(1, _statsHandler.getRequestsActive());
    assertEquals(1, _statsHandler.getDispatched());
    assertEquals(0, _statsHandler.getDispatchedActive());
    asyncHolder.get().addListener(new AsyncListener() {

        @Override
        public void onTimeout(AsyncEvent event) throws IOException {
        }

        @Override
        public void onStartAsync(AsyncEvent event) throws IOException {
        }

        @Override
        public void onError(AsyncEvent event) throws IOException {
        }

        @Override
        public void onComplete(AsyncEvent event) throws IOException {
            try {
                latch.countDown();
            } catch (Exception ignored) {
            }
        }
    });
    long requestTime = 20;
    Thread.sleep(requestTime);
    asyncHolder.get().complete();
    latch.await();
    assertEquals(1, _statsHandler.getRequests());
    assertEquals(0, _statsHandler.getRequestsActive());
    assertEquals(1, _statsHandler.getDispatched());
    assertEquals(0, _statsHandler.getDispatchedActive());
    assertEquals(1, _statsHandler.getAsyncRequests());
    assertEquals(0, _statsHandler.getAsyncDispatches());
    assertEquals(0, _statsHandler.getExpires());
    assertEquals(1, _statsHandler.getResponses2xx());
    assertTrue(_statsHandler.getRequestTimeTotal() >= (dispatchTime + requestTime) * 3 / 4);
    assertEquals(_statsHandler.getRequestTimeTotal(), _statsHandler.getRequestTimeMax());
    assertEquals(_statsHandler.getRequestTimeTotal(), _statsHandler.getRequestTimeMean(), 0.01);
    assertTrue(_statsHandler.getDispatchedTimeTotal() >= dispatchTime * 3 / 4);
    assertTrue(_statsHandler.getDispatchedTimeTotal() < _statsHandler.getRequestTimeTotal());
    assertEquals(_statsHandler.getDispatchedTimeTotal(), _statsHandler.getDispatchedTimeMax());
    assertEquals(_statsHandler.getDispatchedTimeTotal(), _statsHandler.getDispatchedTimeMean(), 0.01);
}
Also used : Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) AtomicReference(java.util.concurrent.atomic.AtomicReference) AsyncContext(javax.servlet.AsyncContext) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) AsyncEvent(javax.servlet.AsyncEvent) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) CyclicBarrier(java.util.concurrent.CyclicBarrier) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) AsyncListener(javax.servlet.AsyncListener) Test(org.junit.Test)

Example 30 with CyclicBarrier

use of java.util.concurrent.CyclicBarrier in project jetty.project by eclipse.

the class CounterStatisticTest method testCounterContended.

@Test
public void testCounterContended() throws Exception {
    final CounterStatistic counter = new CounterStatistic();
    final int N = 100;
    final int L = 1000;
    final Thread[] threads = new Thread[N];
    final CyclicBarrier incBarrier = new CyclicBarrier(N);
    final CountDownLatch decBarrier = new CountDownLatch(N / 2);
    for (int i = N; i-- > 0; ) {
        final int I = i;
        threads[i] = (i >= N / 2) ? new Thread() {

            @Override
            public void run() {
                try {
                    incBarrier.await();
                    decBarrier.await();
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
                Random random = new Random();
                for (int l = L; l-- > 0; ) {
                    counter.decrement();
                    if (random.nextInt(5) == 0)
                        Thread.yield();
                }
            }
        } : new Thread() {

            @Override
            public void run() {
                try {
                    incBarrier.await();
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
                Random random = new Random();
                for (int l = L; l-- > 0; ) {
                    counter.increment();
                    if (l == L / 2)
                        decBarrier.countDown();
                    if (random.nextInt(5) == 0)
                        Thread.yield();
                }
            }
        };
        threads[i].start();
    }
    for (int i = N; i-- > 0; ) threads[i].join();
    assertThat(counter.getCurrent(), equalTo(0L));
    assertThat(counter.getTotal(), equalTo(N * L / 2L));
    assertThat(counter.getMax(), greaterThanOrEqualTo((N / 2) * (L / 2L)));
}
Also used : Random(java.util.Random) CountDownLatch(java.util.concurrent.CountDownLatch) CyclicBarrier(java.util.concurrent.CyclicBarrier) Test(org.junit.Test)

Aggregations

CyclicBarrier (java.util.concurrent.CyclicBarrier)322 Test (org.junit.Test)112 CountDownLatch (java.util.concurrent.CountDownLatch)89 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)77 IOException (java.io.IOException)65 ArrayList (java.util.ArrayList)65 BrokenBarrierException (java.util.concurrent.BrokenBarrierException)64 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)63 ExecutorService (java.util.concurrent.ExecutorService)39 AtomicReference (java.util.concurrent.atomic.AtomicReference)36 Ignite (org.apache.ignite.Ignite)33 IgniteException (org.apache.ignite.IgniteException)32 Test (org.testng.annotations.Test)26 IgniteCache (org.apache.ignite.IgniteCache)25 List (java.util.List)24 TimeoutException (java.util.concurrent.TimeoutException)22 ExecutionException (java.util.concurrent.ExecutionException)21 Transaction (org.apache.ignite.transactions.Transaction)21 Callable (java.util.concurrent.Callable)19 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)19