View Javadoc

    1   package org.apache.turbine.pipeline;
    2
    3
    4   /*
    5    * Licensed to the Apache Software Foundation (ASF) under one
    6    * or more contributor license agreements.  See the NOTICE file
    7    * distributed with this work for additional information
    8    * regarding copyright ownership.  The ASF licenses this file
    9    * to you under the Apache License, Version 2.0 (the
    10   * "License"); you may not use this file except in compliance
    11   * with the License.  You may obtain a copy of the License at
    12   *
    13   *   http://www.apache.org/licenses/LICENSE-2.0
    14   *
    15   * Unless required by applicable law or agreed to in writing,
    16   * software distributed under the License is distributed on an
    17   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    18   * KIND, either express or implied.  See the License for the
    19   * specific language governing permissions and limitations
    20   * under the License.
    21   */
    22
    23
    24  import static org.junit.Assert.assertEquals;
    25
    26  import java.io.StringWriter;
    27
    28  import org.junit.Ignore;
    29  import org.junit.Test;
    30
    31  /**
    32   * Tests TurbinePipeline.
    33   *
    34   * @author <a href="mailto:dlr@finemaltcoding.com">Daniel Rall</a>
    35   * @version $Id: PipelineTest.java 1724799 2016-01-15 13:21:01Z tv $
    36   */
    37  public class PipelineTest
    38  {
    39      private final static int THREADS = 100;
    40      private final static int LOOPS = 10000;
    41
    42      /**
    43       * Tests the Pipeline.
    44       */
    45      @Test public void testPipeline() throws Exception
    46      {
    47          // Make sure Valves are getting added properly to the
    48          // Pipeline.
    49          StringWriter writer = new StringWriter();
    50          Pipeline pipeline = new TurbinePipeline();
    51
    52          SimpleValve valve = new SimpleValve();
    53          valve.setWriter(writer);
    54          valve.setValue("foo");
    55          pipeline.addValve(valve);
    56          valve = new SimpleValve();
    57          valve.setWriter(writer);
    58          valve.setValue("bar");
    59          pipeline.addValve(valve);
    60
    61          pipeline.invoke(new DefaultPipelineData());
    62
    63          assertEquals("foobar", writer.toString());
    64      }
    65
    66      /**
    67       * Tests the Pipeline throughput.
    68       */
    69      @Ignore("For performance tests only") @Test public void testPipelinePerformance() throws Exception
    70      {
    71          StringWriter writer = new StringWriter();
    72          Pipeline pipeline = new TurbinePipeline();
    73
    74          SimpleValve valve = new SimpleValve();
    75          valve.setWriter(writer);
    76          valve.setValue("foo");
    77          pipeline.addValve(valve);
    78          valve = new SimpleValve();
    79          valve.setWriter(writer);
    80          valve.setValue("bar");
    81          pipeline.addValve(valve);
    82
    83          Worker[] worker = new Worker[THREADS];
    84          long startTime = System.currentTimeMillis();
    85
    86          for (int i = 0; i < THREADS; i++)
    87          {
    88              worker[i] = new Worker(pipeline);
    89              worker[i].start();
    90          }
    91
    92          for (int i = 0; i < THREADS; i++)
    93          {
    94              worker[i].join();
    95          }
    96
    97          System.out.println(System.currentTimeMillis() - startTime);
    98      }
    99
    100     /**
    101      * Worker thread
    102      */
    103     protected class Worker extends Thread
    104     {
    105         Pipeline pipeline;
    106
    107         /**
    108          * Constructor
    109          *
    110          * @param pipeline
    111          */
    112         public Worker(Pipeline pipeline)
    113         {
    114             super();
    115             this.pipeline = pipeline;
    116         }
    117
    118         @Override
    119         public void run()
    120         {
    121             PipelineData pd = new DefaultPipelineData();
    122
    123             for (int idx = 0; idx < LOOPS; idx++)
    124             {
    125                 try
    126                 {
    127                     pipeline.invoke(pd);
    128                 }
    129                 catch (Exception e)
    130                 {
    131                     e.printStackTrace();
    132                 }
    133             }
    134         }
    135     }
    136 }