001package org.apache.turbine.pipeline;
002
003
004/*
005 * Licensed to the Apache Software Foundation (ASF) under one
006 * or more contributor license agreements.  See the NOTICE file
007 * distributed with this work for additional information
008 * regarding copyright ownership.  The ASF licenses this file
009 * to you under the Apache License, Version 2.0 (the
010 * "License"); you may not use this file except in compliance
011 * with the License.  You may obtain a copy of the License at
012 *
013 *   http://www.apache.org/licenses/LICENSE-2.0
014 *
015 * Unless required by applicable law or agreed to in writing,
016 * software distributed under the License is distributed on an
017 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
018 * KIND, either express or implied.  See the License for the
019 * specific language governing permissions and limitations
020 * under the License.
021 */
022
023
024import static org.junit.jupiter.api.Assertions.*;
025
026import java.io.StringWriter;
027
028import org.junit.jupiter.api.Tag;
029import org.junit.jupiter.api.Test;
030
031/**
032 * Tests TurbinePipeline.
033 *
034 * @author <a href="mailto:dlr@finemaltcoding.com">Daniel Rall</a>
035 * @version $Id$
036 */
037public class PipelineTest
038{
039    private final static int THREADS = 100;
040    private final static int LOOPS = 10000;
041
042    /**
043     * Tests the Pipeline.
044     */
045    @Test public void testPipeline() throws Exception
046    {
047        // Make sure Valves are getting added properly to the
048        // Pipeline.
049        StringWriter writer = new StringWriter();
050        Pipeline pipeline = new TurbinePipeline();
051
052        SimpleValve valve = new SimpleValve();
053        valve.setWriter(writer);
054        valve.setValue("foo");
055        pipeline.addValve(valve);
056        valve = new SimpleValve();
057        valve.setWriter(writer);
058        valve.setValue("bar");
059        pipeline.addValve(valve);
060
061        pipeline.invoke(new DefaultPipelineData());
062
063        assertEquals("foobar", writer.toString());
064    }
065
066    /**
067     * Tests the Pipeline throughput.
068     */
069    //@Disabled("For performance tests only")
070    @Tag("performance")
071    @Test public void testPipelinePerformance() throws Exception
072    {
073        StringWriter writer = new StringWriter();
074        Pipeline pipeline = new TurbinePipeline();
075
076        SimpleValve valve = new SimpleValve();
077        valve.setWriter(writer);
078        valve.setValue("foo");
079        pipeline.addValve(valve);
080        valve = new SimpleValve();
081        valve.setWriter(writer);
082        valve.setValue("bar");
083        pipeline.addValve(valve);
084
085        Worker[] worker = new Worker[THREADS];
086        long startTime = System.currentTimeMillis();
087
088        for (int i = 0; i < THREADS; i++)
089        {
090            worker[i] = new Worker(pipeline);
091            worker[i].start();
092        }
093
094        for (int i = 0; i < THREADS; i++)
095        {
096            worker[i].join();
097        }
098
099        System.out.println(System.currentTimeMillis() - startTime);
100    }
101
102    /**
103     * Worker thread
104     */
105    protected class Worker extends Thread
106    {
107        Pipeline pipeline;
108
109        /**
110         * Constructor
111         *
112         * @param pipeline
113         */
114        public Worker(Pipeline pipeline)
115        {
116            super();
117            this.pipeline = pipeline;
118        }
119
120        @Override
121        public void run()
122        {
123            PipelineData pd = new DefaultPipelineData();
124
125            for (int idx = 0; idx < LOOPS; idx++)
126            {
127                try
128                {
129                    pipeline.invoke(pd);
130                }
131                catch (Exception e)
132                {
133                    e.printStackTrace();
134                }
135            }
136        }
137    }
138}