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.ByteArrayOutputStream;
30  import java.io.PrintStream;
31  import java.nio.charset.StandardCharsets;
32  
33  import org.apache.hc.core5.http.HttpVersion;
34  import org.hamcrest.CoreMatchers;
35  import org.hamcrest.MatcherAssert;
36  import org.junit.Test;
37  
38  public class ResultFormatterTest {
39  
40      @Test
41      public void testBasics() throws Exception {
42          final Results results = new Results(
43                  "TestServer/1.1",
44                  HttpVersion.HTTP_1_1,
45                  "localhost",
46                  8080,
47                  "/index.html",
48                  2924,
49                  5,
50                  3399,
51                  20000,
52                  0,
53                  20000,
54                  62640000,
55                  0,
56                  50000000);
57          final ByteArrayOutputStream buf = new ByteArrayOutputStream();
58          ResultFormatter.print(new PrintStream(buf, true, StandardCharsets.US_ASCII.name()), results);
59          MatcherAssert.assertThat(new String(buf.toByteArray(), StandardCharsets.US_ASCII).replace("\r\n", "\n"),
60                  CoreMatchers.equalTo(
61                  "Server Software:\t\tTestServer/1.1\n" +
62                          "Protocol version:\t\tHTTP/1.1\n" +
63                          "Server Hostname:\t\tlocalhost\n" +
64                          "Server Port:\t\t\t8080\n" +
65                          "Document Path:\t\t\t/index.html\n" +
66                          "Document Length:\t\t2924 bytes\n" +
67                          "\n" +
68                          "Concurrency Level:\t\t5\n" +
69                          "Time taken for tests:\t3.399000 seconds\n" +
70                          "Complete requests:\t\t20000\n" +
71                          "Failed requests:\t\t0\n" +
72                          "Kept alive:\t\t\t\t20000\n" +
73                          "Total transferred:\t\t62640000 bytes\n" +
74                          "Content transferred:\t50000000 bytes\n" +
75                          "Requests per second:\t5,884.08 [#/sec] (mean)\n" +
76                          "Time per request:\t\t0.850 [ms] (mean)\n" +
77                          "Time per request:\t\t0.170 [ms] (mean, across all concurrent requests)\n" +
78                          "Transfer rate:\t\t\t17,997.02 [Kbytes/sec] received\n"
79          ));
80       }
81  
82  }