Example usage for io.netty.buffer PooledByteBufAllocator buffer

List of usage examples for io.netty.buffer PooledByteBufAllocator buffer

Introduction

In this page you can find the example usage for io.netty.buffer PooledByteBufAllocator buffer.

Prototype

@Override
    public ByteBuf buffer(int initialCapacity) 

Source Link

Usage

From source file:com.weibo.api.motan.transport.netty4.http.NettyHttpRequestHandlerTest.java

License:Apache License

private FullHttpRequest buildHttpRequest(String requestPath) throws Exception {
    PooledByteBufAllocator allocator = new PooledByteBufAllocator();
    ByteBuf buf = allocator.buffer(0);
    FullHttpRequest httpReqeust = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, requestPath,
            buf);/*w  ww  .  ja  v  a  2 s  .c o m*/
    return httpReqeust;
}

From source file:io.grpc.benchmarks.driver.LoadClient.java

License:Apache License

LoadClient(Control.ClientConfig config) throws Exception {
    log.log(Level.INFO, "Client Config \n" + config.toString());
    this.config = config;
    // Create the channels
    channels = new ManagedChannel[config.getClientChannels()];
    for (int i = 0; i < config.getClientChannels(); i++) {
        channels[i] = Utils.newClientChannel(Epoll.isAvailable() ? Transport.NETTY_EPOLL : Transport.NETTY_NIO,
                Utils.parseSocketAddress(config.getServerTargets(i % config.getServerTargetsCount())),
                config.hasSecurityParams(),
                config.hasSecurityParams() && config.getSecurityParams().getUseTestCa(),
                config.hasSecurityParams() ? config.getSecurityParams().getServerHostOverride() : null,
                Utils.DEFAULT_FLOW_CONTROL_WINDOW, false);
    }//ww  w . java  2 s  .c  o  m

    // Create a stub per channel
    if (config.getClientType() == Control.ClientType.ASYNC_CLIENT) {
        asyncStubs = new BenchmarkServiceGrpc.BenchmarkServiceStub[channels.length];
        for (int i = 0; i < channels.length; i++) {
            asyncStubs[i] = BenchmarkServiceGrpc.newStub(channels[i]);
        }
    } else {
        blockingStubs = new BenchmarkServiceGrpc.BenchmarkServiceBlockingStub[channels.length];
        for (int i = 0; i < channels.length; i++) {
            blockingStubs[i] = BenchmarkServiceGrpc.newBlockingStub(channels[i]);
        }
    }

    // Determine no of threads
    if (config.getClientType() == Control.ClientType.SYNC_CLIENT) {
        threadCount = config.getOutstandingRpcsPerChannel() * config.getClientChannels();
    } else {
        threadCount = config.getAsyncClientThreads() == 0 ? Runtime.getRuntime().availableProcessors()
                : config.getAsyncClientThreads();
    }
    // Use a fixed sized pool of daemon threads.
    fixedThreadPool = Executors.newFixedThreadPool(threadCount,
            new DefaultThreadFactory("client-worker", true));

    // Create the load distribution
    switch (config.getLoadParams().getLoadCase()) {
    case CLOSED_LOOP:
        distribution = null;
        break;
    case LOAD_NOT_SET:
        distribution = null;
        break;
    case POISSON:
        // Mean of exp distribution per thread is <no threads> / <offered load per second>
        distribution = new ExponentialDistribution(
                threadCount / config.getLoadParams().getPoisson().getOfferedLoad());
        break;
    default:
        throw new IllegalArgumentException("Scenario not implemented");
    }

    // Create payloads
    switch (config.getPayloadConfig().getPayloadCase()) {
    case SIMPLE_PARAMS: {
        Payloads.SimpleProtoParams simpleParams = config.getPayloadConfig().getSimpleParams();
        simpleRequest = Utils.makeRequest(Messages.PayloadType.COMPRESSABLE, simpleParams.getReqSize(),
                simpleParams.getRespSize());
        break;
    }
    case BYTEBUF_PARAMS: {
        PooledByteBufAllocator alloc = PooledByteBufAllocator.DEFAULT;
        genericRequest = alloc.buffer(config.getPayloadConfig().getBytebufParams().getRespSize());
        if (genericRequest.capacity() > 0) {
            genericRequest.writerIndex(genericRequest.capacity() - 1);
        }
        break;
    }
    default: {
        // Not implemented yet
        throw new IllegalArgumentException("Scenario not implemented");
    }
    }

    List<OperatingSystemMXBean> beans = ManagementFactory.getPlatformMXBeans(OperatingSystemMXBean.class);
    if (!beans.isEmpty()) {
        osBean = beans.get(0);
    } else {
        osBean = null;
    }

    // Create the histogram recorder
    recorder = new Recorder((long) config.getHistogramParams().getMaxPossible(), 3);
}

From source file:io.grpc.benchmarks.driver.LoadServer.java

License:Apache License

LoadServer(Control.ServerConfig config) throws Exception {
    log.log(Level.INFO, "Server Config \n" + config.toString());
    port = config.getPort() == 0 ? Utils.pickUnusedPort() : config.getPort();
    ServerBuilder<?> serverBuilder = ServerBuilder.forPort(port);
    int asyncThreads = config.getAsyncServerThreads() == 0 ? Runtime.getRuntime().availableProcessors()
            : config.getAsyncServerThreads();
    // The concepts of sync & async server are quite different in the C impl and the names
    // chosen for the enum are based on that implementation. We use 'sync' to mean
    // the direct executor case in Java even though the service implementations are always
    // fully async.
    switch (config.getServerType()) {
    case ASYNC_SERVER: {
        serverBuilder.executor(getExecutor(asyncThreads));
        break;//from   www  .  j  a va  2  s  .  c om
    }
    case SYNC_SERVER: {
        serverBuilder.directExecutor();
        break;
    }
    case ASYNC_GENERIC_SERVER: {
        serverBuilder.executor(getExecutor(asyncThreads));
        // Create buffers for the generic service
        PooledByteBufAllocator alloc = PooledByteBufAllocator.DEFAULT;
        genericResponse = alloc.buffer(config.getPayloadConfig().getBytebufParams().getRespSize());
        if (genericResponse.capacity() > 0) {
            genericResponse.writerIndex(genericResponse.capacity() - 1);
        }
        break;
    }
    default: {
        throw new IllegalArgumentException();
    }
    }
    if (config.hasSecurityParams()) {
        File cert = TestUtils.loadCert("server1.pem");
        File key = TestUtils.loadCert("server1.key");
        serverBuilder.useTransportSecurity(cert, key);
    }
    benchmarkService = new AsyncServer.BenchmarkServiceImpl();
    if (config.getServerType() == Control.ServerType.ASYNC_GENERIC_SERVER) {
        serverBuilder.addService(ServerServiceDefinition
                .builder(new ServiceDescriptor(BenchmarkServiceGrpc.SERVICE_NAME,
                        GENERIC_STREAMING_PING_PONG_METHOD))
                .addMethod(GENERIC_STREAMING_PING_PONG_METHOD, new GenericServiceCallHandler()).build());
    } else {
        serverBuilder.addService(benchmarkService);
    }
    server = serverBuilder.build();

    List<OperatingSystemMXBean> beans = ManagementFactory.getPlatformMXBeans(OperatingSystemMXBean.class);
    if (!beans.isEmpty()) {
        osBean = beans.get(0);
    } else {
        osBean = null;
    }
}