1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
|
/*
* Copyright (c) 2007, 2011, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
* @bug 5102289
* @summary Stress test for ResourceBundle.getBundle with ResourceBundle.Control.
* @run main/othervm -esa StressTest 2 15
* @key randomness
*/
import java.util.*;
import java.util.concurrent.atomic.*;
// Usage: java StressTest [threadsFactor [duration]]
public class StressTest {
static final Locale ROOT_LOCALE = new Locale("");
static final Random rand = new Random();
static final Locale[] locales = {
Locale.US,
Locale.CHINA,
ROOT_LOCALE,
Locale.JAPAN,
Locale.CANADA,
Locale.KOREA
};
static final String[] expected = {
"U.S.A.",
"China",
"U.S.A.",
"Japan",
"U.S.A.", // StressOut_en_CA.properties is empty.
"Korea"
};
static final long startTime = System.currentTimeMillis();
// increment each element when one getBundle call is done.
static AtomicIntegerArray counters;
static int[] prevCounters;
static int intervalForCounterCheck;
static AtomicInteger clearCounter = new AtomicInteger();
static volatile boolean runrun = true;
public static void main(String[] args) {
int threadsFactor = 2;
if (args.length > 0) {
threadsFactor = Math.max(2, Integer.parseInt(args[0]));
}
int duration = 180;
if (args.length > 1) {
duration = Math.max(5, Integer.parseInt(args[1]));
}
Locale reservedLocale = Locale.getDefault();
try {
Locale.setDefault(Locale.US);
Thread[] tasks = new Thread[locales.length * threadsFactor];
counters = new AtomicIntegerArray(tasks.length);
for (int i = 0; i < tasks.length; i++) {
tasks[i] = new Thread(new Worker(i));
}
for (int i = 0; i < tasks.length; i++) {
tasks[i].start();
}
int nProcessors = Runtime.getRuntime().availableProcessors();
intervalForCounterCheck = Math.max(tasks.length / nProcessors, 1);
System.out.printf(
"%d processors, intervalForCounterCheck = %d [sec]%n",
nProcessors, intervalForCounterCheck);
try {
for (int i = 0; runrun && i < duration; i++) {
Thread.sleep(1000); // 1 second
if ((i % intervalForCounterCheck) == 0) {
checkCounters();
}
}
runrun = false;
for (int i = 0; i < tasks.length; i++) {
tasks[i].join();
}
} catch (InterruptedException e) {
}
printCounters();
} finally {
// restore the reserved locale
Locale.setDefault(reservedLocale);
}
}
static void checkCounters() {
int length = counters.length();
int[] snapshot = new int[length];
for (int i = 0; i < length; i++) {
snapshot[i] = counters.get(i);
}
if (prevCounters == null) {
prevCounters = snapshot;
return;
}
for (int i = 0; i < length; i++) {
if (snapshot[i] > prevCounters[i]) {
continue;
}
System.out.printf(
"Warning: Thread #%d hasn't updated its counter for the last %d second(s).%n",
i, intervalForCounterCheck);
}
prevCounters = snapshot;
}
static void printCounters() {
long total = 0;
int min = Integer.MAX_VALUE;
int max = Integer.MIN_VALUE;
for (int i = 0; i < counters.length(); i++) {
int counter = counters.get(i);
total += counter;
min = Math.min(min, counter);
max = Math.max(max, counter);
}
System.out.printf("Total: %d calls, min=%d, max=%d, cache cleared %d times%n",
total, min, max, clearCounter.get());
}
static class Worker implements Runnable {
final int id;
final int index;
final Locale locale;
final String str;
final int max;
final boolean cleaner;
ResourceBundle.Control control;
Worker(int i) {
id = i;
index = i % locales.length;
locale = locales[index];
cleaner = locale.equals(ROOT_LOCALE);
str = expected[index];
max = rand.nextInt((index + 1) * 500) + 1000;
control = new TestControl(max);
System.out.println("Worker" + i + ": locale="+locale+", expected="+str+
", max="+max);
}
public void run() {
while (runrun) {
ResourceBundle rb = ResourceBundle.getBundle("StressOut", locale, control);
counters.incrementAndGet(id);
String s = rb.getString("data");
if (!s.equals(str)) {
runrun = false;
throw new RuntimeException(locale + ": rb.locale=" + rb.getLocale() +
", got " + s + ", expected " + str);
}
try {
Thread.sleep(rand.nextInt(max/500));
} catch (InterruptedException e) {
}
if (cleaner && (rand.nextInt(10000) == 0)) {
//System.out.println("Clearing cache!");
ResourceBundle.clearCache();
clearCounter.incrementAndGet();
}
}
}
static class TestControl extends ResourceBundle.Control {
int max;
public List<Locale> getCandidateLocales(String baseName, Locale locale) {
List<Locale> list = super.getCandidateLocales(baseName, locale);
//System.out.println("Candidate locales=" + list);
return list;
}
public TestControl(int max) {
this.max = max;
}
public long getTimeToLive(String baseName, Locale locale) {
// This will set TTL to a random value for each bundle.
long ttl = rand.nextInt(max);
//System.out.println("TTL: " + baseName + "_" + locale + " for " + ttl);
return ttl;
}
public boolean needsReload(String baseName, Locale locale,
String format, ClassLoader loader,
ResourceBundle bundle, long loadTime) {
//System.out.println("Expired: " + baseName + "_" + locale + "." + format +
// " at " + (loadTime-startTime) + ", bundle=" + bundle);
return rand.nextBoolean();
}
}
}
}
|