View Javadoc

    1   /*
    2    *  Licensed to the Apache Software Foundation (ASF) under one
    3    *  or more contributor license agreements.  See the NOTICE file
    4    *  distributed with this work for additional information
    5    *  regarding copyright ownership.  The ASF licenses this file
    6    *  to you under the Apache License, Version 2.0 (the
    7    *  "License"); you may not use this file except in compliance
    8    *  with the License.  You may obtain a copy of the License at
    9    *
    10   *    http://www.apache.org/licenses/LICENSE-2.0
    11   *
    12   *  Unless required by applicable law or agreed to in writing,
    13   *  software distributed under the License is distributed on an
    14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    15   *  KIND, either express or implied.  See the License for the
    16   *  specific language governing permissions and limitations
    17   *  under the License.
    18   *
    19   */
    20  package org.apache.mina.core;
    21
    22  import static org.junit.Assert.assertTrue;
    23
    24  import java.io.IOException;
    25  import java.net.ServerSocket;
    26  import java.util.Arrays;
    27  import java.util.Collection;
    28  import java.util.NoSuchElementException;
    29  import java.util.concurrent.CountDownLatch;
    30  import java.util.concurrent.TimeUnit;
    31
    32  import org.apache.mina.core.BenchmarkFactory.Type;
    33  import org.junit.After;
    34  import org.junit.Before;
    35  import org.junit.Test;
    36  import org.junit.runner.RunWith;
    37  import org.junit.runners.Parameterized;
    38  import org.junit.runners.Parameterized.Parameters;
    39
    40  /**
    41   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
    42   */
    43  @RunWith(Parameterized.class)
    44  public abstract class BenchmarkBinaryTest {
    45      private int numberOfMessages;
    46
    47      private int port;
    48
    49      private BenchmarkServer server;
    50
    51      private BenchmarkClient client;
    52
    53      private int messageSize;
    54
    55      private int timeout;
    56
    57      private byte[] data;
    58
    59      public BenchmarkBinaryTest(int numberOfMessages, int messageSize, int timeout) {
    60          this.numberOfMessages = numberOfMessages;
    61          this.messageSize = messageSize;
    62          this.timeout = timeout;
    63      }
    64
    65      public abstract Type getClientType();
    66
    67      public abstract Type getServerType();
    68
    69      @Parameters(name = "{0} messages of size {1}")
    70      public static Collection<Object[]> getParameters() {
    71          Object[][] parameters = new Object[][] { { 100000, 10, 2 * 60 }, { 100000, 1 * 1024, 2 * 60 },
    72                  { 100000, 10 * 1024, 2 * 60 }, { 100, 64 * 1024 * 1024, 10 * 60 } };
    73          return Arrays.asList(parameters);
    74      }
    75
    76      public static int getNextAvailable() {
    77          ServerSocket serverSocket = null;
    78
    79          try {
    80              // Here, we simply return an available port found by the system
    81              serverSocket = new ServerSocket(0);
    82              int port = serverSocket.getLocalPort();
    83
    84              // Don't forget to close the socket...
    85              serverSocket.close();
    86
    87              return port;
    88          } catch (IOException ioe) {
    89              throw new NoSuchElementException(ioe.getMessage());
    90          }
    91      }
    92
    93      @Before
    94      public void init() throws IOException {
    95          port = getNextAvailable();
    96          server = BenchmarkServerFactory.INSTANCE.get(getServerType());
    97          server.start(port);
    98          client = BenchmarkClientFactory.INSTANCE.get(getClientType());
    99          data = new byte[messageSize + 4];
    100         data[0] = (byte) (messageSize >>> 24 & 255);
    101         data[1] = (byte) (messageSize >>> 16 & 255);
    102         data[2] = (byte) (messageSize >>> 8 & 255);
    103         data[3] = (byte) (messageSize & 255);
    104     }
    105
    106     @After
    107     public void shutdown() throws IOException {
    108         client.stop();
    109         server.stop();
    110     }
    111
    112     /**
    113      * Send "numberOfMessages" messages to a server. Currently, 1 million, with two different
    114      * size, 10Ko and 64Ko.
    115      */
    116     @Test
    117     public void benchmark() throws IOException, InterruptedException {
    118         CountDownLatch counter = new CountDownLatch(numberOfMessages);
    119
    120         client.start(port, counter, data);
    121         boolean result = counter.await(timeout, TimeUnit.SECONDS);
    122         assertTrue("Still " + counter.getCount() + " messages to send on a total of " + numberOfMessages, result);
    123     }
    124 }