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 openr66.protocol.test;
22  
23  import goldengate.common.command.exception.CommandAbstractException;
24  import goldengate.common.logging.GgInternalLoggerFactory;
25  import goldengate.common.logging.GgSlf4JLoggerFactory;
26  
27  import java.util.concurrent.ExecutorService;
28  import java.util.concurrent.Executors;
29  
30  import openr66.client.DirectTransfer;
31  import openr66.context.ErrorCode;
32  import openr66.context.R66Result;
33  import openr66.database.DbConstant;
34  import openr66.protocol.configuration.Configuration;
35  import openr66.protocol.networkhandler.NetworkTransaction;
36  import openr66.protocol.utils.R66Future;
37  
38  import org.jboss.netty.logging.InternalLoggerFactory;
39  
40  /**
41   * Test class for multiple DirectTransfer
42   *
43   * @author Frederic Bregier
44   *
45   */
46  public class TestTransferNoDb extends DirectTransfer {
47      static int nb = 100;
48      /**
49       * @param args
50       * @param rank
51       * @return True if OK
52       */
53      protected static boolean getSpecialParams(String []args, int rank) {
54          for (int i = rank; i<args.length; i++) {
55              if (args[i].equalsIgnoreCase("-nb")) {
56                  i++;
57                  nb = Integer.parseInt(args[i]);
58              } else if (args[i].equalsIgnoreCase("-md5")) {
59              } else if (args[i].charAt(0) == '-') {
60                  i++;// jump one
61              }
62          }
63          return true;
64      }
65  
66      public TestTransferNoDb(R66Future future, String remoteHost,
67              String filename, String rulename, String fileinfo, boolean isMD5, int blocksize, long id,
68              NetworkTransaction networkTransaction) {
69          super(future, remoteHost, filename, rulename, fileinfo, isMD5, blocksize, id, networkTransaction);
70      }
71  
72  
73      public static void main(String[] args) {
74          InternalLoggerFactory.setDefaultFactory(new GgSlf4JLoggerFactory(
75                  null));
76          if (logger == null) {
77              logger = GgInternalLoggerFactory.getLogger(DirectTransfer.class);
78          }
79          if (! getParams(args, false)) {
80              logger.error("Wrong initialization");
81              if (DbConstant.admin != null && DbConstant.admin.isConnected) {
82                  DbConstant.admin.close();
83              }
84              System.exit(1);
85          }
86          getSpecialParams(args, 1);
87          Configuration.configuration.CLIENT_THREAD = nb;
88          Configuration.configuration.pipelineInit();
89          NetworkTransaction networkTransaction = new NetworkTransaction();
90          try {
91              ExecutorService executorService = Executors.newCachedThreadPool();
92  
93              R66Future[] arrayFuture = new R66Future[nb];
94              logger.info("Start of Test Transfer");
95              long time1 = System.currentTimeMillis();
96              for (int i = 0; i < nb; i ++) {
97                  arrayFuture[i] = new R66Future(true);
98                  TestTransferNoDb transaction = new TestTransferNoDb(arrayFuture[i],
99                          rhost, localFilename, rule, fileInfo, ismd5, block,
100                         DbConstant.ILLEGALVALUE,
101                         networkTransaction);
102                 executorService.execute(transaction);
103                 try {
104                     Thread.sleep(10);
105                 } catch (InterruptedException e) {
106                 }
107             }
108             int success = 0;
109             int error = 0;
110             int warn = 0;
111             for (int i = 0; i < nb; i ++) {
112                 arrayFuture[i].awaitUninterruptibly();
113                 R66Result result = arrayFuture[i].getResult();
114                 if (arrayFuture[i].isSuccess()) {
115                     if (result.runner.getErrorInfo() == ErrorCode.Warning) {
116                         warn ++;
117                     } else {
118                         success ++;
119                     }
120                 } else {
121                     if (result.runner != null &&
122                             result.runner.getErrorInfo() == ErrorCode.Warning) {
123                         warn ++;
124                     } else {
125                         error ++;
126                     }
127                 }
128             }
129             long time2 = System.currentTimeMillis();
130             long length = 0;
131             // Get the first result as testing only
132             R66Result result = arrayFuture[0].getResult();
133             logger.warn("Final file: " +
134                     (result.file != null? result.file.toString() : "no file"));
135             try {
136                 length = result.file != null? result.file.length() : 0L;
137             } catch (CommandAbstractException e) {
138             }
139             long delay = time2 - time1;
140             float nbs = success * 1000;
141             nbs /= delay;
142             float mbs = nbs * length / 1024;
143             logger.warn("Success: " + success + " Warning: " + warn + " Error: " +
144                     error + " delay: " + delay + " NB/s: " + nbs + " KB/s: " + mbs);
145             executorService.shutdown();
146         } finally {
147             networkTransaction.closeAll();
148         }
149     }
150 
151 }