1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
45
46
47
48
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
65 static ClientBootstrap bootstrap;
66
67 static LocalExecClientPipelineFactory localExecClientPipelineFactory;
68
69
70
71
72
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
88 bootstrap = new ClientBootstrap(
89 new NioClientSocketChannelFactory(threadPool, threadPool2));
90
91 localExecClientPipelineFactory =
92 new LocalExecClientPipelineFactory();
93 bootstrap.setPipelineFactory(localExecClientPipelineFactory);
94 try {
95
96 LocalExecClientTest client = new LocalExecClientTest();
97
98 long first = System.currentTimeMillis();
99 client.connect();
100 client.runOnce();
101 client.disconnect();
102 long second = System.currentTimeMillis();
103
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
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
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
122
123 ExecutorService executorService = Executors.newFixedThreadPool(nth);
124 first = System.currentTimeMillis();
125
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
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
143 first = System.currentTimeMillis();
144 client.connect();
145 client.runFinal();
146 client.disconnect();
147 second = System.currentTimeMillis();
148
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
155 bootstrap.releaseExternalResources();
156 localExecClientPipelineFactory.releaseResources();
157 }
158 }
159
160
161
162
163 public LocalExecClientTest() {
164 }
165
166 private Channel channel;
167
168
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
180
181 private void connect() {
182
183 ChannelFuture future = bootstrap.connect(address);
184
185
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
198
199 private void disconnect() {
200
201
202 try {
203 channel.close().await();
204 } catch (InterruptedException e) {
205 }
206 }
207
208
209
210
211 public void runOnce() {
212
213 LocalExecClientHandler clientHandler =
214 (LocalExecClientHandler) channel.getPipeline().getLast();
215 clientHandler.initExecClient();
216
217
218 ChannelFuture lastWriteFuture = null;
219 String line = command+"\n";
220 if (line != null) {
221
222 lastWriteFuture = channel.write(line);
223
224 if (lastWriteFuture != null) {
225 try {
226 lastWriteFuture.await();
227 } catch (InterruptedException e) {
228 }
229 }
230
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
245
246 private void runFinal() {
247
248 LocalExecClientHandler clientHandler =
249 (LocalExecClientHandler) channel.getPipeline().getLast();
250 clientHandler.initExecClient();
251
252
253 ChannelFuture lastWriteFuture = null;
254 String line = "-1000 stop\n";
255 if (line != null) {
256
257 lastWriteFuture = channel.write(line);
258
259 if (lastWriteFuture != null) {
260 try {
261 lastWriteFuture.await();
262 } catch (InterruptedException e) {
263 }
264 }
265
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 }