Example usage for org.springframework.util StopWatch shortSummary

List of usage examples for org.springframework.util StopWatch shortSummary

Introduction

In this page you can find the example usage for org.springframework.util StopWatch shortSummary.

Prototype

public String shortSummary() 

Source Link

Document

Get a short description of the total running time.

Usage

From source file:my.adam.smo.server.SocketServer.java

@Inject
public SocketServer(@Value("${server_worker_threads:10}") int workerCount) {
    bootstrap.setFactory(new NioServerSocketChannelFactory(Executors.newCachedThreadPool(),
            Executors.newCachedThreadPool(), workerCount));

    ChannelPipelineFactory pipelineFactory = new ChannelPipelineFactory() {
        @Override//from   ww w .jav a 2  s  .com
        public ChannelPipeline getPipeline() throws Exception {
            ChannelPipeline p = Channels.pipeline();
            if (enableTrafficLogging) {
                InternalLoggerFactory.setDefaultFactory(new Slf4JLoggerFactory());
                p.addLast("logger", new LoggingHandler(InternalLogLevel.DEBUG));
            }

            p.addLast("frameEncoder", new LengthFieldPrepender(4));//DownstreamHandler
            p.addLast("protobufEncoder", new ProtobufEncoder());//DownstreamHandler

            p.addLast("frameDecoder", new LengthFieldBasedFrameDecoder(MAX_FRAME_BYTES_LENGTH, 0, 4, 0, 4));//UpstreamHandler
            p.addLast("protobufDecoder", new ProtobufDecoder(RPCommunication.Request.getDefaultInstance()));//UpstreamHandler
            p.addLast("handler", new SimpleChannelUpstreamHandler() {
                @Override
                public void messageReceived(ChannelHandlerContext ctx, final MessageEvent e) throws Exception {
                    StopWatch stopWatch = new StopWatch("messageReceived");
                    stopWatch.start();

                    RPCommunication.Request request = (RPCommunication.Request) e.getMessage();
                    logger.trace("received request:" + request.toString());

                    if (enableAsymmetricEncryption) {
                        request = getAsymDecryptedRequest(request);
                        logger.trace("asymmetric encryption enabled, decrypted request: " + request.toString());
                    }

                    if (enableSymmetricEncryption) {
                        request = getDecryptedRequest(request);
                        logger.trace("symmetric encryption enabled, decrypted request: " + request.toString());
                    }

                    final RPCommunication.Request protoRequest = request;

                    RpcController dummyController = new DummyRpcController();
                    Service service = serviceMap.get(request.getServiceName());

                    logger.trace("got service: " + service + " for name " + request.getServiceName());

                    Descriptors.MethodDescriptor methodToCall = service.getDescriptorForType()
                            .findMethodByName(request.getMethodName());

                    logger.trace("got method: " + methodToCall + " for name " + request.getMethodName());

                    Message methodArguments = service.getRequestPrototype(methodToCall).newBuilderForType()
                            .mergeFrom(request.getMethodArgument()).build();

                    logger.trace("get method arguments from request " + methodArguments.toString());

                    RpcCallback<Message> callback = new RpcCallback<Message>() {
                        @Override
                        public void run(Message parameter) {
                            RPCommunication.Response response = RPCommunication.Response.newBuilder()
                                    .setResponse(parameter.toByteString())
                                    .setRequestId(protoRequest.getRequestId()).build();

                            //encryption
                            if (enableSymmetricEncryption) {
                                response = getEncryptedResponse(response);
                                logger.trace("symmetric encryption enabled, encrypted response: "
                                        + response.toString());
                            }

                            if (enableAsymmetricEncryption) {
                                response = getAsymEncryptedResponse(response);
                                logger.trace("asymmetric encryption enabled, encrypted response: "
                                        + response.toString());
                            }

                            e.getChannel().write(response);
                            logger.trace("finishing call, response sent");
                        }
                    };
                    logger.trace("calling " + methodToCall.getFullName());
                    service.callMethod(methodToCall, dummyController, methodArguments, callback);
                    stopWatch.stop();
                    logger.trace(stopWatch.shortSummary());
                }

                @Override
                public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) throws Exception {
                    if (!standardExceptionHandling(ctx, e)) {
                        super.exceptionCaught(ctx, e);
                    }
                }
            });
            return p;
        }
    };
    bootstrap.setPipelineFactory(pipelineFactory);
}

From source file:my.adam.smo.server.HTTPServer.java

@Inject
public HTTPServer(@Value("${server_worker_threads:10}") int workerCount) {
    bootstrap.setFactory(new NioServerSocketChannelFactory(Executors.newCachedThreadPool(),
            Executors.newCachedThreadPool(), workerCount));

    ChannelPipelineFactory pipelineFactory = new ChannelPipelineFactory() {
        @Override//from  w  w w .  j  ava2  s  .com
        public ChannelPipeline getPipeline() throws Exception {
            ChannelPipeline p = Channels.pipeline();

            if (enableTrafficLogging) {
                InternalLoggerFactory.setDefaultFactory(new Slf4JLoggerFactory());
                p.addLast("logger", new LoggingHandler(InternalLogLevel.DEBUG));
            }

            p.addLast("codec", new HttpServerCodec());
            p.addLast("chunkAggregator", new HttpChunkAggregator(MAX_CONTENT_LENGTH));
            p.addLast("chunkedWriter", new ChunkedWriteHandler());
            p.addLast("compressor", new HttpContentCompressor());

            p.addLast("handler", new SimpleChannelUpstreamHandler() {
                @Override
                public void messageReceived(ChannelHandlerContext ctx, final MessageEvent e) throws Exception {
                    StopWatch stopWatch = new StopWatch("messageReceived");
                    stopWatch.start();

                    final DefaultHttpRequest httpRequest = (DefaultHttpRequest) e.getMessage();
                    ChannelBuffer cb = Base64.decode(httpRequest.getContent(), Base64Dialect.STANDARD);

                    RPCommunication.Request request = RPCommunication.Request
                            .parseFrom(cb.copy(0, cb.readableBytes()).array());
                    logger.trace("received request:" + request.toString());

                    if (enableAsymmetricEncryption) {
                        request = getAsymDecryptedRequest(request);
                        logger.trace("asymmetric encryption enabled, decrypted request: " + request.toString());
                    }

                    if (enableSymmetricEncryption) {
                        request = getDecryptedRequest(request);
                        logger.trace("symmetric encryption enabled, decrypted request: " + request.toString());
                    }

                    final RPCommunication.Request protoRequest = request;

                    RpcController dummyController = new DummyRpcController();
                    Service service = serviceMap.get(request.getServiceName());

                    logger.trace("got service: " + service + " for name " + request.getServiceName());

                    Descriptors.MethodDescriptor methodToCall = service.getDescriptorForType()
                            .findMethodByName(request.getMethodName());

                    logger.trace("got method: " + methodToCall + " for name " + request.getMethodName());

                    Message methodArguments = service.getRequestPrototype(methodToCall).newBuilderForType()
                            .mergeFrom(request.getMethodArgument()).build();

                    logger.trace("get method arguments from request " + methodArguments.toString());

                    RpcCallback<Message> callback = new RpcCallback<Message>() {
                        @Override
                        public void run(Message parameter) {
                            HttpResponse httpResponse = new DefaultHttpResponse(HttpVersion.HTTP_1_1,
                                    HttpResponseStatus.OK);

                            ByteArrayOutputStream paramOutputStream = new ByteArrayOutputStream();
                            CodedOutputStream paramCodedOutputStream = CodedOutputStream
                                    .newInstance(paramOutputStream);
                            try {
                                parameter.writeTo(paramCodedOutputStream);
                                paramCodedOutputStream.flush();
                            } catch (IOException e1) {
                                logger.error("failed to write to output stream");
                            }

                            RPCommunication.Response response = RPCommunication.Response.newBuilder()
                                    .setResponse(ByteString.copyFrom(paramOutputStream.toByteArray()))
                                    .setRequestId(protoRequest.getRequestId()).build();

                            if (enableSymmetricEncryption) {
                                response = getEncryptedResponse(response);
                                logger.trace("symmetric encryption enabled, encrypted response: "
                                        + response.toString());
                            }

                            if (enableAsymmetricEncryption) {
                                response = getAsymEncryptedResponse(response);
                                logger.trace("asymmetric encryption enabled, encrypted response: "
                                        + response.toString());
                            }

                            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
                            CodedOutputStream codedOutputStream = CodedOutputStream.newInstance(outputStream);
                            try {
                                response.writeTo(codedOutputStream);
                                codedOutputStream.flush();
                            } catch (IOException e1) {
                                logger.error("unable to write to output stream", e1);
                            }

                            byte[] arr = outputStream.toByteArray();

                            ChannelBuffer resp = Base64.encode(ChannelBuffers.copiedBuffer(arr),
                                    Base64Dialect.STANDARD);

                            httpResponse.setContent(resp);
                            httpResponse.addHeader(HttpHeaders.Names.CONTENT_LENGTH, resp.readableBytes());
                            httpResponse.addHeader(HttpHeaders.Names.CONTENT_TYPE,
                                    HttpHeaders.Values.APPLICATION_X_WWW_FORM_URLENCODED);

                            e.getChannel().write(httpResponse);
                            logger.trace("finishing call, httpResponse sent");
                        }
                    };
                    logger.trace("calling " + methodToCall.getFullName());
                    service.callMethod(methodToCall, dummyController, methodArguments, callback);
                    stopWatch.stop();
                    logger.trace(stopWatch.shortSummary());
                }

                @Override
                public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) throws Exception {
                    if (!standardExceptionHandling(ctx, e)) {
                        super.exceptionCaught(ctx, e);
                    }
                }
            });
            return p;
        }
    };
    bootstrap.setPipelineFactory(pipelineFactory);
}

From source file:org.springframework.aop.interceptor.PerformanceMonitorInterceptor.java

@Override
protected Object invokeUnderTrace(MethodInvocation invocation, Log logger) throws Throwable {
    String name = createInvocationTraceName(invocation);
    StopWatch stopWatch = new StopWatch(name);
    stopWatch.start(name);// www .  ja  va2  s . c  o  m
    try {
        return invocation.proceed();
    } finally {
        stopWatch.stop();
        writeToLog(logger, stopWatch.shortSummary());
    }
}

From source file:org.springframework.batch.core.scope.StepScopePerformanceTests.java

private int doTest(String name, String test) throws Exception {
    @SuppressWarnings("unchecked")
    ItemStreamReader<String> reader = (ItemStreamReader<String>) applicationContext.getBean(name);
    reader.open(new ExecutionContext());
    StopWatch stopWatch = new StopWatch(test);
    stopWatch.start();//from w  w w. j  a v  a2 s .c o m
    int count = 0;
    while (reader.read() != null) {
        // do nothing
        count++;
    }
    stopWatch.stop();
    reader.close();
    logger.info(stopWatch.shortSummary());
    return count;
}