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