List of usage examples for java.nio.channels ByteChannel ByteChannel
ByteChannel
From source file:org.apache.hc.core5.benchmark.HttpBenchmark.java
public Results execute() throws Exception { final HttpProcessorBuilder builder = HttpProcessorBuilder.create().addAll(new H2RequestContent(), new H2RequestTargetHost(), new H2RequestConnControl(), new RequestUserAgent("HttpCore-AB/5.0")); if (this.config.isUseExpectContinue()) { builder.add(new RequestExpectContinue()); }// w ww. ja v a 2 s .c o m final SSLContext sslContext; if ("https".equals(config.getUri().getScheme())) { final SSLContextBuilder sslContextBuilder = new SSLContextBuilder(); sslContextBuilder.setProtocol("SSL"); if (config.isDisableSSLVerification()) { sslContextBuilder.loadTrustMaterial(null, new TrustStrategy() { @Override public boolean isTrusted(final X509Certificate[] chain, final String authType) throws CertificateException { return true; } }); } else if (config.getTrustStorePath() != null) { sslContextBuilder.loadTrustMaterial(new File(config.getTrustStorePath()), config.getTrustStorePassword() != null ? config.getTrustStorePassword().toCharArray() : null); } if (config.getIdentityStorePath() != null) { sslContextBuilder.loadKeyMaterial(new File(config.getIdentityStorePath()), config.getIdentityStorePassword() != null ? config.getIdentityStorePassword().toCharArray() : null, config.getIdentityStorePassword() != null ? config.getIdentityStorePassword().toCharArray() : null); } sslContext = sslContextBuilder.build(); } else { sslContext = SSLContexts.createSystemDefault(); } final HttpVersionPolicy versionPolicy; if (config.isForceHttp2()) { versionPolicy = HttpVersionPolicy.FORCE_HTTP_2; } else { if (sslContext != null) { versionPolicy = HttpVersionPolicy.NEGOTIATE; } else { versionPolicy = HttpVersionPolicy.FORCE_HTTP_1; } } final Stats stats = new Stats(); try (final HttpAsyncRequester requester = H2RequesterBootstrap.bootstrap().setHttpProcessor(builder.build()) .setTlsStrategy(new BasicClientTlsStrategy(sslContext)).setVersionPolicy(versionPolicy) .setIOSessionDecorator(new Decorator<IOSession>() { @Override public IOSession decorate(final IOSession ioSession) { return new IOSession() { public String getId() { return ioSession.getId(); } public Lock lock() { return ioSession.lock(); } public void enqueue(final Command command, final Command.Priority priority) { ioSession.enqueue(command, priority); } public boolean hasCommands() { return ioSession.hasCommands(); } public Command poll() { return ioSession.poll(); } public ByteChannel channel() { return new ByteChannel() { @Override public int read(final ByteBuffer dst) throws IOException { final int bytesRead = ioSession.channel().read(dst); if (bytesRead > 0) { stats.incTotalBytesRecv(bytesRead); } return bytesRead; } @Override public int write(final ByteBuffer src) throws IOException { final int bytesWritten = ioSession.channel().write(src); if (bytesWritten > 0) { stats.incTotalBytesSent(bytesWritten); } return bytesWritten; } @Override public boolean isOpen() { return ioSession.channel().isOpen(); } @Override public void close() throws IOException { ioSession.channel().close(); } }; } public SocketAddress getRemoteAddress() { return ioSession.getRemoteAddress(); } public SocketAddress getLocalAddress() { return ioSession.getLocalAddress(); } public int getEventMask() { return ioSession.getEventMask(); } public void setEventMask(final int ops) { ioSession.setEventMask(ops); } public void setEvent(final int op) { ioSession.setEvent(op); } public void clearEvent(final int op) { ioSession.clearEvent(op); } public void close() { ioSession.close(); } public int getStatus() { return ioSession.getStatus(); } public boolean isClosed() { return ioSession.isClosed(); } public Timeout getSocketTimeout() { return ioSession.getSocketTimeout(); } public void setSocketTimeout(final Timeout timeout) { ioSession.setSocketTimeout(timeout); } public long getLastReadTime() { return ioSession.getLastReadTime(); } public long getLastWriteTime() { return ioSession.getLastWriteTime(); } public void updateReadTime() { ioSession.updateReadTime(); } public void updateWriteTime() { ioSession.updateWriteTime(); } public void close(final CloseMode closeMode) { ioSession.close(closeMode); } }; } }).setStreamListener(new Http1StreamListener() { @Override public void onRequestHead(final HttpConnection connection, final HttpRequest request) { if (config.getVerbosity() >= 3) { System.out.println(">> " + request.getMethod() + " " + request.getRequestUri()); final Header[] headers = request.getHeaders(); for (final Header header : headers) { System.out.println(">> " + header.toString()); } System.out.println(); } } @Override public void onResponseHead(final HttpConnection connection, final HttpResponse response) { if (config.getVerbosity() >= 3) { System.out.println("<< " + response.getCode() + " " + response.getReasonPhrase()); final Header[] headers = response.getHeaders(); for (final Header header : headers) { System.out.println("<< " + header.toString()); } System.out.println(); } } @Override public void onExchangeComplete(final HttpConnection connection, final boolean keepAlive) { if (keepAlive) { stats.incKeepAliveCount(); } } }).setStreamListener(new Http2StreamListener() { private final FramePrinter framePrinter = new FramePrinter(); @Override public void onHeaderInput(final HttpConnection connection, final int streamId, final List<? extends Header> headers) { if (config.getVerbosity() >= 3) { for (final Header header : headers) { System.out.println(">> " + header.toString()); } System.out.println(); } } @Override public void onHeaderOutput(final HttpConnection connection, final int streamId, final List<? extends Header> headers) { if (config.getVerbosity() >= 3) { for (final Header header : headers) { System.out.println("<< " + header.toString()); } System.out.println(); } } @Override public void onFrameInput(final HttpConnection connection, final int streamId, final RawFrame frame) { if (config.getVerbosity() >= 4) { System.out.print(">> "); try { framePrinter.printFrameInfo(frame, System.out); } catch (final IOException ignore) { } System.out.println(); } } @Override public void onFrameOutput(final HttpConnection connection, final int streamId, final RawFrame frame) { if (config.getVerbosity() >= 4) { System.out.print("<< "); try { framePrinter.printFrameInfo(frame, System.out); } catch (final IOException ignore) { } System.out.println(); } } @Override public void onInputFlowControl(final HttpConnection connection, final int streamId, final int delta, final int actualSize) { if (config.getVerbosity() >= 5) { System.out.println(">> stream " + streamId + ": " + actualSize + " " + delta); } } @Override public void onOutputFlowControl(final HttpConnection connection, final int streamId, final int delta, final int actualSize) { if (config.getVerbosity() >= 5) { System.out.println("<< stream " + streamId + ": " + actualSize + " " + delta); } } }).setIOReactorConfig(IOReactorConfig.custom().setSoTimeout(config.getSocketTimeout()).build()) .create()) { requester.setDefaultMaxPerRoute(config.getConcurrencyLevel()); requester.setMaxTotal(config.getConcurrencyLevel() * 2); requester.start(); return doExecute(requester, stats); } }