为什么通道管道中包含的压缩器和解压缩器的运行时间太长



在 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; 
} 

可能是什么原因?。

我将扩展信息。

类 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 时,执行时间要少得多。

最新更新