为什么ChannelPipeline中包含的压缩器和解压缩器的运行时间太长?

时间:2012-12-03 10:34:54

标签: netty pipeline

与特定类中引入的两种方法的执行时间相比,在ChannelPipeline中引入了解压缩程序和压缩程序但运行时太大

@Override 
public ChannelPipeline getPipeline() throws Exception { 
    ChannelPipeline pipeline = pipeline(); 
    pipeline.addLast("decoder",new IcapRequestDecoder(maxInitialLineLength, maxIcapHeaderSize, maxHttpHeaderSize, maxChunkSize)); 
    pipeline.addLast("chunkAggregator",new IcapChunkAggregator(maxContentLength)); 
    pipeline.addLast("decompressor",new IcapContentDecompressor()); 
    pipeline.addLast("encoder",new IcapResponseEncoder()); 
    pipeline.addLast("chunkSeparator",new IcapChunkSeparator(maxContentLength)); 
    pipeline.addLast("handler", handler); 
    pipeline.addLast("compressor",new IcapContentCompressor()); 
    return pipeline; 
} 

可能是什么原因?。

1 个答案:

答案 0 :(得分:0)

我将扩展信息。

IcapContentCompress类:

public class IcapContentCompressor extends SimpleChannelDownstreamHandler {

...

/**
 * Invoked when {@link Channel#write(Object)} is called.
 */
@Override
public void writeRequested(ChannelHandlerContext ctx, MessageEvent e) throws Exception {        

            ...

    IcapResponse icapResponse = (IcapResponse) msg;

    // Get response message
    HttpResponse httpMessage = icapResponse.getHttpResponse();

    ...

    boolean compressed = isCompressed(httpMessage);
    if (compressed) {           
        String acceptEncoding = httpMessage.getHeader(HttpHeaders.Names.CONTENT_ENCODING);
        compress(httpMessage, acceptEncoding);
    }

    }       

    ctx.sendDownstream(e);

}

方法压缩:

private void compress(HttpResponse httpMessage, String acceptEncoding) throws Exception {                       
    ZlibWrapper wrapper = determineWrapper(acceptEncoding);
    if (wrapper == null) {
       return;
    }

    EncoderEmbedder<ChannelBuffer>  encoder = new EncoderEmbedder<ChannelBuffer>(new ZlibEncoder(wrapper, compressionLevel, windowBits, memLevel));

    httpMessage.setHeader(
            HttpHeaders.Names.CONTENT_ENCODING,
            getTargetContentEncoding(wrapper));

    ChannelBuffer contentCompressed = ChannelBuffers.wrappedBuffer(
                                            encode(httpMessage.getContent(), encoder), 
                                            finishEncode(encoder));

    // Replace the content.
    httpMessage.setContent(contentCompressed);  
}   

当我们在特定类中使用相同的方法而不在ChannelPipeline类中引入时,执行时间要少得多。