View Javadoc
1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  package org.apache.hc.core5.benchmark;
28  
29  import java.io.IOException;
30  import java.net.InetSocketAddress;
31  import java.util.Arrays;
32  import java.util.Collection;
33  import java.util.concurrent.Future;
34  
35  import org.apache.hc.core5.http.EntityDetails;
36  import org.apache.hc.core5.http.HttpException;
37  import org.apache.hc.core5.http.HttpRequest;
38  import org.apache.hc.core5.http.HttpStatus;
39  import org.apache.hc.core5.http.HttpVersion;
40  import org.apache.hc.core5.http.Message;
41  import org.apache.hc.core5.http.Method;
42  import org.apache.hc.core5.http.URIScheme;
43  import org.apache.hc.core5.http.impl.bootstrap.HttpAsyncServer;
44  import org.apache.hc.core5.http.nio.AsyncRequestConsumer;
45  import org.apache.hc.core5.http.nio.AsyncServerRequestHandler;
46  import org.apache.hc.core5.http.nio.entity.NoopEntityConsumer;
47  import org.apache.hc.core5.http.nio.support.AsyncResponseBuilder;
48  import org.apache.hc.core5.http.nio.support.BasicRequestConsumer;
49  import org.apache.hc.core5.http.protocol.HttpContext;
50  import org.apache.hc.core5.http2.HttpVersionPolicy;
51  import org.apache.hc.core5.http2.impl.nio.bootstrap.H2ServerBootstrap;
52  import org.apache.hc.core5.io.CloseMode;
53  import org.apache.hc.core5.net.URIBuilder;
54  import org.apache.hc.core5.reactor.ListenerEndpoint;
55  import org.junit.After;
56  import org.junit.Assert;
57  import org.junit.Before;
58  import org.junit.Test;
59  import org.junit.runner.RunWith;
60  import org.junit.runners.Parameterized;
61  
62  @RunWith(Parameterized.class)
63  public class BenchmarkToolTest {
64  
65      @Parameterized.Parameters(name = "{0}")
66      public static Collection<Object[]> protocols() {
67          return Arrays.asList(new Object[][]{
68                  { HttpVersionPolicy.NEGOTIATE },
69                  { HttpVersionPolicy.FORCE_HTTP_2 }
70          });
71      }
72  
73      private final HttpVersionPolicy versionPolicy;
74      private HttpAsyncServer server;
75      private InetSocketAddress address;
76  
77      public BenchmarkToolTest(final HttpVersionPolicy versionPolicy) {
78          this.versionPolicy = versionPolicy;
79      }
80  
81      @Before
82      public void setup() throws Exception {
83          server = H2ServerBootstrap.bootstrap()
84                  .register("/", new AsyncServerRequestHandler<Message<HttpRequest, Void>>() {
85  
86                      @Override
87                      public AsyncRequestConsumer<Message<HttpRequest, Void>> prepare(
88                              final HttpRequest request,
89                              final EntityDetails entityDetails,
90                              final HttpContext context) throws HttpException {
91                          return new BasicRequestConsumer<>(entityDetails != null ? new NoopEntityConsumer() : null);
92                      }
93  
94                      @Override
95                      public void handle(
96                              final Message<HttpRequest, Void> requestObject,
97                              final ResponseTrigger responseTrigger,
98                              final HttpContext context) throws HttpException, IOException {
99                          responseTrigger.submitResponse(
100                                 AsyncResponseBuilder.create(HttpStatus.SC_OK)
101                                         .setEntity("0123456789ABCDEF")
102                                         .build(),
103                                 context);
104                     }
105 
106                 })
107                 .setVersionPolicy(versionPolicy)
108                 .create();
109         server.start();
110         final Future<ListenerEndpoint> future = server.listen(new InetSocketAddress(0), URIScheme.HTTP);
111         final ListenerEndpoint listener = future.get();
112         address = (InetSocketAddress) listener.getAddress();
113     }
114 
115     @After
116     public void shutdown() throws Exception {
117         if (server != null) {
118             server.close(CloseMode.IMMEDIATE);
119         }
120     }
121 
122     @Test
123     public void testBasics() throws Exception {
124         final BenchmarkConfig config = BenchmarkConfig.custom()
125                 .setKeepAlive(true)
126                 .setMethod(Method.POST.name())
127                 .setPayloadText("0123456789ABCDEF")
128                 .setUri(new URIBuilder()
129                         .setScheme(URIScheme.HTTP.id)
130                         .setHost("localhost")
131                         .setPort(address .getPort())
132                         .build())
133                 .setConcurrencyLevel(3)
134                 .setForceHttp2(versionPolicy == HttpVersionPolicy.FORCE_HTTP_2)
135                 .setRequests(100)
136                 .build();
137         final HttpBenchmark httpBenchmark = new HttpBenchmark(config);
138         final Results results = httpBenchmark.execute();
139         Assert.assertNotNull(results);
140         Assert.assertEquals(100, results.getSuccessCount());
141         Assert.assertEquals(0, results.getFailureCount());
142         Assert.assertEquals(16, results.getContentLength());
143         Assert.assertEquals(3, results.getConcurrencyLevel());
144         Assert.assertEquals(100 * 16, results.getTotalContentBytesRecvd());
145         if (versionPolicy == HttpVersionPolicy.FORCE_HTTP_2) {
146             Assert.assertEquals(HttpVersion.HTTP_2, results.getProtocolVersion());
147         }
148     }
149 
150 }