1 /*
2  * Copyright (c) 2007, 2018, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.
8  *
9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  */
23 package nsk.share.test;
24 
25 import java.io.PrintStream;
26 import vm.share.options.Option;
27 
28 /**
29  * Options for stress tests.
30  *
31  * The following options may be configured:
32  *
33  *   -stressTime [time] execution time in seconds
34  *   -stressIterationsFactor [factor] iterations factor.
35  *   The actual number of iterations is obtained by multiplying standard
36  *   number of iterations (which is defined by the test itself) and this factor.
37  *   -stressThreadsFactor [factor] number of threads factor
38  *   The actual number of threads is determined by multiplying standard
39  *   number of threads (which is determined by test itself and may also depend
40  *   on machine configuration) and this factor.
41  */
42 public class StressOptions {
43     /**
44      * This enum contains names of stress options
45      */
46     public static enum StressOptionsParam {
47         stressTime,
48         stressIterationsFactor,
49         stressThreadsFactor,
50         stressRunsFactor,
51         stressDebug,
52         stressDebugDetailed,
53     }
54 
55     /* Execution time in seconds */
56     @Option(name = "stressTime", default_value = "60", description = "Stress execution time in seconds")
57     private long time;
58 
59     /* Iterations factor */
60     @Option(name = "stressIterationsFactor", default_value = "1", description = "Stress iterations factor")
61     private int iterationsFactor;
62 
63     /* Number of threads factor */
64     @Option(name = "stressThreadsFactor", default_value = "1", description = "Stress threads factor")
65     private int threadsFactor;
66 
67     @Option(name = "stressRunsFactor", default_value = "1", description = "Times to re-run the test (if supported by the test)")
68     private int runsFactor;
69 
70     /* Debug stress execution */
71     @Option(name = "stressDebugEnabled", default_value = "false", description = "Stress debug execution enabled")
72     private boolean debugEnabled = false;
73 
74     /* Detailed stressExecution */
75     @Option(name = "stressDebugDetailed", default_value = "false", description = "Stress debug detailed enabled")
76     private boolean debugDetailed = false;
77 
78     /**
79      * Create StressOptions with default settings.
80      */
StressOptions()81     public StressOptions() {
82         time = 60;
83         iterationsFactor = 1;
84         threadsFactor = 1;
85         runsFactor = 1;
86     }
87 
88     /**
89      * Create StressOptions configured from command line arguments.
90      *
91      * @param arg arguments
92      */
StressOptions(String[] args)93     public StressOptions(String[] args) {
94         this();
95         parseCommandLine(args);
96     }
97 
98     /**
99      * Create stresser with same parameters as another.
100      *
101      * @param other another instance of StressOptions
102      */
StressOptions(StressOptions other)103     public StressOptions(StressOptions other) {
104         this.time = other.time;
105         this.iterationsFactor = other.iterationsFactor;
106         this.threadsFactor = other.threadsFactor;
107         this.runsFactor = other.runsFactor;
108     }
109 
isValidStressOption(String option)110     public static boolean isValidStressOption(String option) {
111         for (int i = 0; i < StressOptions.StressOptionsParam.values().length; i++) {
112             if (option.equals(StressOptions.StressOptionsParam.values()[i].name()))
113                 return true;
114         }
115 
116         return false;
117     }
118 
119     /**
120      * Parse command line options related to stress.
121      *
122      * Other options are ignored.
123      *
124      * @param args arguments
125      */
parseCommandLine(String[] args)126     public void parseCommandLine(String[] args) {
127         int i = 0;
128         while (i < args.length) {
129             String arg = args[i];
130             String value = null;
131 
132             int eqPos = arg.indexOf('=');
133             if (eqPos != -1) {
134                 value = arg.substring(eqPos + 1);
135                 arg = arg.substring(0, eqPos);
136             }
137 
138             if (arg.equals("-stressTime")) {
139                 try {
140                     if (value == null) {
141                         if (++i >= args.length)
142                             error("Missing value of -stressTime parameter");
143                         value = args[i];
144                     }
145                     time = Long.parseLong(value);
146                     if (time < 0) {
147                         error("Invalid value of -stressTime parameter: " + time);
148                     }
149                 } catch (NumberFormatException e) {
150                     error("Invalid value of -stressTime parameter: " + value);
151                 }
152             } else if (arg.equals("-stressIterationsFactor")) {
153                 try {
154                     if ( value == null ) {
155                         if (++i >= args.length) {
156                             error("Missing value of -stressIterationsFactor parameter");
157                         }
158                         value = args[i];
159                     }
160                     iterationsFactor = Integer.parseInt(value);
161                     if (iterationsFactor <= 0) {
162                         error("Invalid value of -stressIterationsFactor parameter: " + threadsFactor);
163                     }
164                 } catch (NumberFormatException e) {
165                     error("Invalid value of -stressIterationsFactor parameter: " + value);
166                 }
167             } else if (arg.equals("-stressThreadsFactor")) {
168                 try {
169                     if ( value == null ) {
170                         if (++i >= args.length) {
171                             error("Missing value of -stressThreadsFactor parameter");
172                         }
173                         value = args[i];
174                     }
175                     threadsFactor = Integer.parseInt(value);
176                     if (threadsFactor <= 0) {
177                         error("Invalid value of -stressThreadsFactor parameter: " + threadsFactor);
178                     }
179                 } catch (NumberFormatException e) {
180                     error("Invalid value of -stressThreadsFactor parameter: " + value);
181                 }
182             } else if (arg.equals("-stressRunsFactor")) {
183                 try {
184                     if (value == null) {
185                         if (++i >= args.length) {
186                             error("Missing value of -stressRunsFactor parameter");
187                         }
188                         value = args[i];
189                     }
190                     runsFactor = Integer.parseInt(value);
191                     if (runsFactor <= 0) {
192                         error("Invalid value of -stressRunsFactor parameter: " + threadsFactor);
193                     }
194                 } catch (NumberFormatException e) {
195                     error("Invalid value of -stressRunsFactor parameter: " + value);
196                 }
197             } else if (arg.equals("-stressDebug")) {
198                 debugEnabled = true;
199             } else if (arg.equals("-stressDebugDetailed")) {
200                 debugDetailed = true;
201             }
202 
203             ++i;
204         }
205     }
206 
207     /**
208      * Display information about stress options.
209      *
210      * @param out output stream
211      */
printInfo(PrintStream out)212     public void printInfo(PrintStream out) {
213         out.println("Stress time: " + time + " seconds");
214         out.println("Stress iterations factor: " + iterationsFactor);
215         out.println("Stress threads factor: " + threadsFactor);
216         out.println("Stress runs factor: " + runsFactor);
217     }
218 
error(String msg)219     private void error(String msg) {
220         throw new IllegalArgumentException(msg);
221     }
222 
223     /**
224      * Obtain execution time in seconds.
225      *
226      * @return time
227      */
getTime()228     public long getTime() {
229         return time;
230     }
231 
232     /**
233      * Obtain iterations factor.
234      *
235      * @return iterations factor
236      */
getIterationsFactor()237     public int getIterationsFactor() {
238         return iterationsFactor;
239     }
240 
241     /**
242      * Obtain threads factor.
243      *
244      * @return threads factor
245      */
getThreadsFactor()246     public int getThreadsFactor() {
247         return threadsFactor;
248     }
249 
250     /**
251      * Obtain runs factor.
252      *
253      * @return runs factor
254      */
getRunsFactor()255     public int getRunsFactor() {
256         return runsFactor;
257     }
258 
259     /**
260      * Determine if debugging of stress execution is set.
261      *
262      * @return true if debugging stress execution
263      */
isDebugEnabled()264     public boolean isDebugEnabled() {
265         return debugEnabled;
266     }
267 
268     /**
269      * Determine if detailed debugging of stress execution is set.
270      *
271      * @return true if detailed debugging is enabled
272      */
isDebugDetailed()273     public boolean isDebugDetailed() {
274         return debugDetailed;
275     }
276 }
277