View Javadoc

1   /**
2      This file is part of GoldenGate Project (named also GoldenGate or GG).
3   
4      Copyright 2009, Frederic Bregier, and individual contributors by the @author
5      tags. See the COPYRIGHT.txt in the distribution for a full listing of
6      individual contributors.
7   
8      All GoldenGate Project is free software: you can redistribute it and/or 
9      modify it under the terms of the GNU General Public License as published 
10     by the Free Software Foundation, either version 3 of the License, or
11     (at your option) any later version.
12  
13     GoldenGate is distributed in the hope that it will be useful,
14     but WITHOUT ANY WARRANTY; without even the implied warranty of
15     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16     GNU General Public License for more details.
17  
18     You should have received a copy of the GNU General Public License
19     along with GoldenGate .  If not, see <http://www.gnu.org/licenses/>.
20   */
21  package goldengate.commandexec.client.test;
22  
23  import goldengate.commandexec.client.LocalExecClientHandler;
24  import goldengate.commandexec.client.LocalExecClientPipelineFactory;
25  import goldengate.commandexec.utils.LocalExecResult;
26  import goldengate.common.logging.GgSlf4JLoggerFactory;
27  
28  import java.net.InetAddress;
29  import java.net.InetSocketAddress;
30  import java.net.UnknownHostException;
31  import java.util.concurrent.ExecutorService;
32  import java.util.concurrent.Executors;
33  import java.util.concurrent.TimeUnit;
34  
35  import org.jboss.netty.bootstrap.ClientBootstrap;
36  import org.jboss.netty.channel.Channel;
37  import org.jboss.netty.channel.ChannelFuture;
38  import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory;
39  import org.jboss.netty.logging.InternalLoggerFactory;
40  
41  import ch.qos.logback.classic.Level;
42  
43  /**
44   * LocalExec client.
45   *
46   * This class is an example of client.
47   *
48   * On a bi-core Centrino2 vPro: 18/s in 50 sequential, 30/s in 10 threads with 50 sequential
49   */
50  public class LocalExecClientTest extends Thread {
51  
52      static int nit = 1;
53      static int nth = 1;
54      static String command = "d:\\GG\\testexec.bat";
55      static int port = 9999;
56      static InetSocketAddress address;
57  
58      static LocalExecResult result;
59      static int ok = 0;
60      static int ko = 0;
61  
62      static ExecutorService threadPool;
63      static ExecutorService threadPool2;
64      // Configure the client.
65      static ClientBootstrap bootstrap;
66      // Configure the pipeline factory.
67      static LocalExecClientPipelineFactory localExecClientPipelineFactory;
68  
69      /**
70       * Test & example main
71       * @param args ignored
72       * @throws Exception
73       */
74      public static void main(String[] args) throws Exception {
75          InternalLoggerFactory.setDefaultFactory(new GgSlf4JLoggerFactory(
76                  Level.WARN));
77          InetAddress addr;
78          byte []loop = {127,0,0,1};
79          try {
80              addr = InetAddress.getByAddress(loop);
81          } catch (UnknownHostException e) {
82              return;
83          }
84          address = new InetSocketAddress(addr, port);
85          threadPool = Executors.newCachedThreadPool();
86          threadPool2 = Executors.newCachedThreadPool();
87          // Configure the client.
88          bootstrap = new ClientBootstrap(
89                  new NioClientSocketChannelFactory(threadPool, threadPool2));
90          // Configure the pipeline factory.
91          localExecClientPipelineFactory =
92                  new LocalExecClientPipelineFactory();
93          bootstrap.setPipelineFactory(localExecClientPipelineFactory);
94          try {
95              // Parse options.
96              LocalExecClientTest client = new LocalExecClientTest();
97              // run once
98              long first = System.currentTimeMillis();
99              client.connect();
100             client.runOnce();
101             client.disconnect();
102             long second = System.currentTimeMillis();
103             // print time for one exec
104             System.err.println("1=Total time in ms: "+(second-first)+" or "+(1*1000/(second-first))+" exec/s");
105             System.err.println("Result: " + ok+":"+ko);
106             ok = 0;
107             ko = 0;
108             // Now run multiple within one thread
109             first = System.currentTimeMillis();
110             for (int i = 0; i < nit; i ++) {
111                 client.connect();
112                 client.runOnce();
113                 client.disconnect();
114             }
115             second = System.currentTimeMillis();
116             // print time for one exec
117             System.err.println(nit+"=Total time in ms: "+(second-first)+" or "+(nit*1000/(second-first))+" exec/s");
118             System.err.println("Result: " + ok+":"+ko);
119             ok = 0;
120             ko = 0;
121             // Now run multiple within multiple threads
122             // Create multiple threads
123             ExecutorService executorService = Executors.newFixedThreadPool(nth);
124             first = System.currentTimeMillis();
125             // Starts all thread with a default number of execution
126             for (int i = 0; i < nth; i ++) {
127                 executorService.submit(new LocalExecClientTest());
128             }
129             Thread.sleep(500);
130             executorService.shutdown();
131             while (! executorService.awaitTermination(200, TimeUnit.MILLISECONDS)) {
132                 Thread.sleep(50);
133             }
134             second = System.currentTimeMillis();
135 
136             // print time for one exec
137             System.err.println((nit*nth)+"=Total time in ms: "+(second-first)+" or "+(nit*nth*1000/(second-first))+" exec/s");
138             System.err.println("Result: " + ok+":"+ko);
139             ok = 0;
140             ko = 0;
141 
142             // run once
143             first = System.currentTimeMillis();
144             client.connect();
145             client.runFinal();
146             client.disconnect();
147             second = System.currentTimeMillis();
148             // print time for one exec
149             System.err.println("1=Total time in ms: "+(second-first)+" or "+(1*1000/(second-first))+" exec/s");
150             System.err.println("Result: " + ok+":"+ko);
151             ok = 0;
152             ko = 0;
153         } finally {
154             // Shut down all thread pools to exit.
155             bootstrap.releaseExternalResources();
156             localExecClientPipelineFactory.releaseResources();
157         }
158     }
159 
160     /**
161      * Simple constructor
162      */
163     public LocalExecClientTest() {
164     }
165 
166     private Channel channel;
167     /**
168      * Run method for thread
169      */
170     public void run() {
171         connect();
172         for (int i = 0; i < nit; i ++) {
173             this.runOnce();
174         }
175         disconnect();
176     }
177 
178     /**
179      * Connect to the Server
180      */
181     private void connect() {
182         // Start the connection attempt.
183         ChannelFuture future = bootstrap.connect(address);
184 
185         // Wait until the connection attempt succeeds or fails.
186         try {
187             channel = future.await().getChannel();
188         } catch (InterruptedException e) {
189         }
190         if (!future.isSuccess()) {
191             System.err.println("Client Not Connected");
192             future.getCause().printStackTrace();
193             return;
194         }
195     }
196     /**
197      * Disconnect from the server
198      */
199     private void disconnect() {
200      // Close the connection. Make sure the close operation ends because
201         // all I/O operations are asynchronous in Netty.
202         try {
203             channel.close().await();
204         } catch (InterruptedException e) {
205         }
206     }
207 
208     /**
209      * Run method both for not threaded execution and threaded execution
210      */
211     public void runOnce() {
212      // Initialize the command context
213         LocalExecClientHandler clientHandler =
214             (LocalExecClientHandler) channel.getPipeline().getLast();
215         clientHandler.initExecClient();
216         // Command to execute
217 
218         ChannelFuture lastWriteFuture = null;
219         String line = command+"\n";
220         if (line != null) {
221             // Sends the received line to the server.
222             lastWriteFuture = channel.write(line);
223             // Wait until all messages are flushed before closing the channel.
224             if (lastWriteFuture != null) {
225                 try {
226                     lastWriteFuture.await();
227                 } catch (InterruptedException e) {
228                 }
229             }
230             // Wait for the end of the exec command
231             LocalExecResult localExecResult = clientHandler.waitFor(10000);
232             int status = localExecResult.status;
233             if (status < 0) {
234                 System.err.println("Status: " + status + "\nResult: " +
235                         localExecResult.result);
236                 ko++;
237             } else {
238                 ok++;
239                 result = localExecResult;
240             }
241         }
242     }
243     /**
244      * Run method for closing Server
245      */
246     private void runFinal() {
247         // Initialize the command context
248         LocalExecClientHandler clientHandler =
249             (LocalExecClientHandler) channel.getPipeline().getLast();
250         clientHandler.initExecClient();
251         // Command to execute
252 
253         ChannelFuture lastWriteFuture = null;
254         String line = "-1000 stop\n";
255         if (line != null) {
256             // Sends the received line to the server.
257             lastWriteFuture = channel.write(line);
258             // Wait until all messages are flushed before closing the channel.
259             if (lastWriteFuture != null) {
260                 try {
261                     lastWriteFuture.await();
262                 } catch (InterruptedException e) {
263                 }
264             }
265             // Wait for the end of the exec command
266             LocalExecResult localExecResult = clientHandler.waitFor(10000);
267             int status = localExecResult.status;
268             if (status < 0) {
269                 System.err.println("Status: " + status + "\nResult: " +
270                         localExecResult.result);
271                 ko++;
272             } else {
273                 ok++;
274                 result = localExecResult;
275             }
276         }
277     }
278 }