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