Search in sources :

Example 1 with BackendResponse

use of perf.test.utils.BackendResponse in project WSPerfLab by Netflix-Skunkworks.

the class TestRouteBasic method handle.

public Observable<Void> handle(HttpServerRequest<ByteBuf> request, HttpServerResponse<ByteBuf> response) {
    long startTime = System.currentTimeMillis();
    List<String> _id = request.getQueryParameters().get("id");
    if (_id == null || _id.size() != 1) {
        return writeError(request, response, "Please provide a numerical 'id' value. It can be a random number (uuid).");
    }
    long id = Long.parseLong(String.valueOf(_id.get(0)));
    Observable<List<BackendResponse>> acd = getDataFromBackend("/mock.json?numItems=2&itemSize=50&delay=50&id=" + id).doOnError(Throwable::printStackTrace).<// Eclipse 20140224-0627 can't infer without this type hint even though the Java 8 compiler can
    List<BackendResponse>>flatMap(responseA -> {
        Observable<BackendResponse> responseC = getDataFromBackend("/mock.json?numItems=1&itemSize=5000&delay=80&id=" + responseA.getResponseKey());
        Observable<BackendResponse> responseD = getDataFromBackend("/mock.json?numItems=1&itemSize=1000&delay=1&id=" + responseA.getResponseKey());
        return Observable.zip(Observable.just(responseA), responseC, responseD, Arrays::asList);
    }).doOnError(Throwable::printStackTrace);
    Observable<List<BackendResponse>> be = getDataFromBackend("/mock.json?numItems=25&itemSize=30&delay=150&id=" + id).<// Eclipse 20140224-0627 can't infer without this type hint even though the Java 8 compiler can
    List<BackendResponse>>flatMap(responseB -> {
        Observable<BackendResponse> responseE = getDataFromBackend("/mock.json?numItems=100&itemSize=30&delay=4&id=" + responseB.getResponseKey());
        return Observable.zip(Observable.just(responseB), responseE, Arrays::asList);
    }).doOnError(Throwable::printStackTrace);
    return Observable.zip(acd, be, (_acd, _be) -> {
        BackendResponse responseA = _acd.get(0);
        BackendResponse responseB = _be.get(0);
        BackendResponse responseC = _acd.get(1);
        BackendResponse responseD = _acd.get(2);
        BackendResponse responseE = _be.get(1);
        return new BackendResponse[] { responseA, responseB, responseC, responseD, responseE };
    }).flatMap(backendResponses -> {
        try {
            ByteArrayOutputStream responseStream = ServiceResponseBuilder.buildTestAResponse(jsonFactory, backendResponses);
            response.getHeaders().addHeader("Content-Type", "application/json");
            addResponseHeaders(response, startTime);
            int contentLength = responseStream.size();
            response.getHeaders().addHeader("Content-Length", contentLength);
            return response.writeBytesAndFlush(responseStream.toByteArray());
        } catch (Exception e) {
            return writeError(request, response, "Failed: " + e.getMessage());
        }
    }).doOnError(Throwable::printStackTrace);
}
Also used : HttpServerResponse(io.reactivex.netty.protocol.http.server.HttpServerResponse) Arrays(java.util.Arrays) ByteArrayOutputStream(java.io.ByteArrayOutputStream) RxNetty(io.reactivex.netty.RxNetty) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) BackendResponse(perf.test.utils.BackendResponse) ServiceResponseBuilder(perf.test.utils.ServiceResponseBuilder) JsonFactory(org.codehaus.jackson.JsonFactory) Observable(rx.Observable) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) HttpClientResponse(io.reactivex.netty.protocol.http.client.HttpClientResponse) ByteBuf(io.netty.buffer.ByteBuf) LogLevel(io.netty.handler.logging.LogLevel) JsonParseException(perf.test.utils.JsonParseException) HttpServerRequest(io.reactivex.netty.protocol.http.server.HttpServerRequest) Map(java.util.Map) HttpClient(io.reactivex.netty.protocol.http.client.HttpClient) ByteBufInputStream(io.netty.buffer.ByteBufInputStream) HttpClientRequest(io.reactivex.netty.protocol.http.client.HttpClientRequest) HttpClientBuilder(io.reactivex.netty.protocol.http.client.HttpClientBuilder) BackendResponse(perf.test.utils.BackendResponse) List(java.util.List) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Observable(rx.Observable) JsonParseException(perf.test.utils.JsonParseException)

Example 2 with BackendResponse

use of perf.test.utils.BackendResponse in project WSPerfLab by Netflix-Skunkworks.

the class TestCaseAServlet method get.

public BackendResponse get(String url) {
    String uri = backendMockUriPrefix + url;
    HttpGet httpGet = new HttpGet(uri);
    try {
        HttpResponse response = client.execute(httpGet);
        if (response.getStatusLine().getStatusCode() != 200) {
            throw new RuntimeException("Failure: " + response.getStatusLine());
        }
        HttpEntity entity = response.getEntity();
        BackendResponse backendResponse = BackendResponse.fromJson(jsonFactory, entity.getContent());
        // ensure it is fully consumed
        EntityUtils.consume(entity);
        return backendResponse;
    } catch (Exception e) {
        throw new RuntimeException("Failure retrieving: " + uri, e);
    } finally {
        httpGet.releaseConnection();
    }
}
Also used : HttpEntity(org.apache.http.HttpEntity) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) BackendResponse(perf.test.utils.BackendResponse) ServletException(javax.servlet.ServletException) IOException(java.io.IOException)

Example 3 with BackendResponse

use of perf.test.utils.BackendResponse in project WSPerfLab by Netflix-Skunkworks.

the class TestCaseAServlet method doGet.

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    long startTime = System.currentTimeMillis();
    try {
        Object _id = request.getParameter("id");
        if (_id == null) {
            response.getWriter().println("Please provide a numerical 'id' value. It can be a random number (uuid).");
            response.setStatus(500);
            return;
        }
        final long id = Long.parseLong(String.valueOf(_id));
        try {
            /* First 2 requests (A, B) in parallel */
            final Future<BackendResponse> aResponseFuture = queueGet("/mock.json?type=A&numItems=2&itemSize=50&delay=50&id=" + id);
            final Future<BackendResponse> bResponseFuture = queueGet("/mock.json?type=B&numItems=25&itemSize=30&delay=150&id=" + id);
            /* When response A received perform C & D */
            // spawned in another thread so we don't block the ability to B/E to proceed in parallel
            Future<BackendResponse[]> aGroupResponses = executor.submit(new Callable<BackendResponse[]>() {

                @Override
                public BackendResponse[] call() throws Exception {
                    BackendResponse aResponse = aResponseFuture.get();
                    final Future<BackendResponse> cResponse = queueGet("/mock.json?type=C&numItems=1&itemSize=5000&delay=80&id=" + aResponse.getResponseKey());
                    final Future<BackendResponse> dResponse = queueGet("/mock.json?type=D&numItems=1&itemSize=1000&delay=1&id=" + aResponse.getResponseKey());
                    return new BackendResponse[] { aResponse, cResponse.get(), dResponse.get() };
                }
            });
            /* When response B is received perform E */
            BackendResponse b = bResponseFuture.get();
            BackendResponse e = get("/mock.json?type=E&numItems=100&itemSize=30&delay=4&id=" + b.getResponseKey());
            /*
                 * Parse JSON so we can extract data and combine data into a single response.
                 * 
                 * This simulates what real web-services do most of the time.
                 */
            BackendResponse a = aGroupResponses.get()[0];
            BackendResponse c = aGroupResponses.get()[1];
            BackendResponse d = aGroupResponses.get()[2];
            //EventLogger.log(requestId, "build-response-start");
            ByteArrayOutputStream bos = ServiceResponseBuilder.buildTestAResponse(jsonFactory, a, b, c, d, e);
            //EventLogger.log(requestId, "build-response-end");
            // output to stream
            //EventLogger.log(requestId, "flush-response-start");
            response.getWriter().write(bos.toString());
        //EventLogger.log(requestId, "flush-response-end");
        } catch (Exception e) {
            // error that needs to be returned
            response.setStatus(500);
            response.getWriter().println("Error: " + e.getMessage());
            e.printStackTrace();
        }
    } finally {
        ServiceResponseBuilder.addResponseHeaders(response, startTime);
    }
}
Also used : BackendResponse(perf.test.utils.BackendResponse) Future(java.util.concurrent.Future) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ServletException(javax.servlet.ServletException) IOException(java.io.IOException)

Aggregations

BackendResponse (perf.test.utils.BackendResponse)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 IOException (java.io.IOException)2 ServletException (javax.servlet.ServletException)2 ByteBuf (io.netty.buffer.ByteBuf)1 ByteBufInputStream (io.netty.buffer.ByteBufInputStream)1 HttpResponseStatus (io.netty.handler.codec.http.HttpResponseStatus)1 LogLevel (io.netty.handler.logging.LogLevel)1 RxNetty (io.reactivex.netty.RxNetty)1 HttpClient (io.reactivex.netty.protocol.http.client.HttpClient)1 HttpClientBuilder (io.reactivex.netty.protocol.http.client.HttpClientBuilder)1 HttpClientRequest (io.reactivex.netty.protocol.http.client.HttpClientRequest)1 HttpClientResponse (io.reactivex.netty.protocol.http.client.HttpClientResponse)1 HttpServerRequest (io.reactivex.netty.protocol.http.server.HttpServerRequest)1 HttpServerResponse (io.reactivex.netty.protocol.http.server.HttpServerResponse)1 Arrays (java.util.Arrays)1 List (java.util.List)1 Map (java.util.Map)1 Future (java.util.concurrent.Future)1 TimeUnit (java.util.concurrent.TimeUnit)1