KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgroups > tests > perf > PerformanceTestGenerator


1 package org.jgroups.tests.perf;
2
3 import java.io.FileInputStream JavaDoc;
4 import java.io.FileNotFoundException JavaDoc;
5 import java.io.FileWriter JavaDoc;
6 import java.io.IOException JavaDoc;
7 import java.util.ArrayList JavaDoc;
8 import java.util.Properties JavaDoc;
9
10
11 /**
12  * Utility tool that creates configuration files for automated performance tests.
13  * PerformanceTestGenerator, given an input file with -config flag, generates
14  * test files that are used in conjunction with bin/clusterperfromancetest.sh.
15  * The number of test files generated is numOfSenders*messageSizes.
16  *
17  * Default configuration file used for this utility is conf/performancetestconfig.txt
18  * See bin/clusterperformancetest.sh
19  *
20  * @author Vladimir Blagojevic
21  * @version $Id$
22  */

23 public class PerformanceTestGenerator {
24     Properties JavaDoc configProperties = null;
25     private long totalDataBytes;
26     private int[] numOfSenders;
27     private int[] messageSizes;
28     private int numNodes;
29     private int interval;
30     
31     
32     public PerformanceTestGenerator(Properties JavaDoc config) {
33         configProperties = config;
34     }
35
36     /**
37      * @param args
38      */

39     public static void main(String JavaDoc[] args) {
40         Properties JavaDoc config = null;
41         for(int i=0; i < args.length; i++) {
42             if("-config".equals(args[i])) {
43                 String JavaDoc config_file=args[++i];
44                 config = new Properties JavaDoc();
45                 try {
46                     config.load(new FileInputStream JavaDoc(config_file));
47                 } catch (FileNotFoundException JavaDoc e) {
48                     System.err.println("File " + config_file + " not found");
49                     return;
50                 } catch (IOException JavaDoc e) {
51                     System.err.println("Error reading configuration file " + config_file);
52                     return;
53                 }
54                 continue;
55             }
56             System.out.println("PerformanceTestGenerator -config <file>");
57             return;
58         }
59
60         try {
61             PerformanceTestGenerator t=new PerformanceTestGenerator(config);
62             t.parse();
63             t.generateTestConfigFiles();
64            
65         }
66         catch(Exception JavaDoc e) {
67             e.printStackTrace();
68         }
69     }
70
71     private void parse() throws Exception JavaDoc{
72         
73         numNodes = Integer.parseInt(configProperties.getProperty("nodes"));
74         totalDataBytes = Long.parseLong(configProperties.getProperty("total_data"));
75         numOfSenders = tokenizeAndConvert(configProperties.getProperty("number_of_senders"),",");
76         messageSizes = tokenizeAndConvert(configProperties.getProperty("message_sizes"),",");
77         interval = Integer.parseInt(configProperties.getProperty("interval"));
78     }
79     private void generateFile(int numOfSenders, int messageSize,int nodeCount) {
80         FileWriter JavaDoc fw = null;
81         long numOfMessages = (totalDataBytes/messageSize);
82         try {
83             fw = new FileWriter JavaDoc("config_"+numOfSenders + "_" +nodeCount +"_"+ messageSize + ".txt");
84             fw.write("transport=org.jgroups.tests.perf.transports.JGroupsTransport\n");
85             fw.write("msg_size="+messageSize+"\n");
86             fw.write("num_msgs="+(numOfMessages/numOfSenders) +"\n");
87             fw.write("num_senders="+numOfSenders+"\n");
88             fw.write("num_members="+nodeCount+"\n");
89             fw.write("log_interval="+interval+"\n");
90             fw.close();
91         } catch (IOException JavaDoc e) {
92             e.printStackTrace();
93         }
94         
95     }
96     public static String JavaDoc[] tokenize(String JavaDoc s, String JavaDoc delim) {
97         if (s == null || s.length() == 0) {
98             return new String JavaDoc[0];
99         }
100         if (delim == null || delim.length() == 0) {
101             return new String JavaDoc[] { s };
102         }
103
104         ArrayList JavaDoc tokens = new ArrayList JavaDoc();
105
106         int pos = 0;
107         int delPos = 0;
108         while ((delPos = s.indexOf(delim, pos)) != -1) {
109             tokens.add(s.substring(pos, delPos));
110             pos = delim.length() + delPos;
111         }
112         tokens.add(s.substring(pos));
113         return (String JavaDoc[]) tokens.toArray(new String JavaDoc[tokens.size()]);
114     }
115     
116     public static int[] tokenizeAndConvert(String JavaDoc s, String JavaDoc delim) {
117         String JavaDoc stokens [] = tokenize(s,delim);
118         int result [] = new int[stokens.length];
119         for (int i = 0; i < stokens.length; i++) {
120             result[i]=Integer.parseInt(stokens[i]);
121         }
122         return result;
123     }
124
125     private void generateTestConfigFiles() {
126         for (int i = 0; i < numOfSenders.length; i++) {
127             for (int j = 0; j < messageSizes.length; j++) {
128                 generateFile(numOfSenders[i],messageSizes[j],numNodes);
129             }
130         }
131     }
132
133 }
134
Popular Tags