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.Assert.assertEquals; 025 026import java.io.StringWriter; 027 028import org.junit.Ignore; 029import org.junit.Test; 030 031/** 032 * Tests TurbinePipeline. 033 * 034 * @author <a href="mailto:dlr@finemaltcoding.com">Daniel Rall</a> 035 * @version $Id: PipelineTest.java 1724799 2016-01-15 13:21:01Z tv $ 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 @Ignore("For performance tests only") @Test public void testPipelinePerformance() throws Exception 070 { 071 StringWriter writer = new StringWriter(); 072 Pipeline pipeline = new TurbinePipeline(); 073 074 SimpleValve valve = new SimpleValve(); 075 valve.setWriter(writer); 076 valve.setValue("foo"); 077 pipeline.addValve(valve); 078 valve = new SimpleValve(); 079 valve.setWriter(writer); 080 valve.setValue("bar"); 081 pipeline.addValve(valve); 082 083 Worker[] worker = new Worker[THREADS]; 084 long startTime = System.currentTimeMillis(); 085 086 for (int i = 0; i < THREADS; i++) 087 { 088 worker[i] = new Worker(pipeline); 089 worker[i].start(); 090 } 091 092 for (int i = 0; i < THREADS; i++) 093 { 094 worker[i].join(); 095 } 096 097 System.out.println(System.currentTimeMillis() - startTime); 098 } 099 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}