0N/A/*
3969N/A * Copyright (c) 2007, 2011, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A/*
0N/A * @test
0N/A * @bug 5102289
0N/A * @summary Stress test for ResourceBundle.getBundle with ResourceBundle.Control.
3969N/A * @run main/othervm -esa StressTest 2 15
0N/A */
0N/A
0N/Aimport java.util.*;
0N/Aimport java.util.concurrent.atomic.*;
0N/A
3969N/A// Usage: java StressTest [threadsFactor [duration]]
0N/Apublic class StressTest {
0N/A static final Locale ROOT_LOCALE = new Locale("");
0N/A static final Random rand = new Random();
0N/A static final Locale[] locales = {
0N/A Locale.US,
0N/A Locale.CHINA,
0N/A ROOT_LOCALE,
0N/A Locale.JAPAN,
0N/A Locale.CANADA,
0N/A Locale.KOREA
0N/A };
0N/A static final String[] expected = {
0N/A "U.S.A.",
0N/A "China",
0N/A "U.S.A.",
0N/A "Japan",
0N/A "U.S.A.", // StressOut_en_CA.properties is empty.
0N/A "Korea"
0N/A };
0N/A final static long startTime = System.currentTimeMillis();
0N/A
0N/A // increment each element when one getBundle call is done.
0N/A static AtomicIntegerArray counters;
0N/A static int[] prevCounters;
0N/A static int intervalForCounterCheck;
0N/A static AtomicInteger clearCounter = new AtomicInteger();
0N/A
0N/A static volatile boolean runrun = true;
0N/A
0N/A public static void main(String[] args) {
3969N/A int threadsFactor = 2;
0N/A if (args.length > 0) {
3969N/A threadsFactor = Math.max(2, Integer.parseInt(args[0]));
0N/A }
3969N/A int duration = 180;
0N/A if (args.length > 1) {
3969N/A duration = Math.max(5, Integer.parseInt(args[1]));
0N/A }
0N/A Locale.setDefault(Locale.US);
3969N/A Thread[] tasks = new Thread[locales.length * threadsFactor];
0N/A counters = new AtomicIntegerArray(tasks.length);
0N/A
0N/A for (int i = 0; i < tasks.length; i++) {
0N/A tasks[i] = new Thread(new Worker(i));
0N/A }
0N/A for (int i = 0; i < tasks.length; i++) {
0N/A tasks[i].start();
0N/A }
0N/A
0N/A int nProcessors = Runtime.getRuntime().availableProcessors();
0N/A intervalForCounterCheck = Math.max(tasks.length / nProcessors, 1);
0N/A System.out.printf("%d processors, intervalForCounterCheck = %d [sec]%n",
0N/A nProcessors, intervalForCounterCheck);
0N/A try {
3969N/A for (int i = 0; runrun && i < duration; i++) {
3969N/A Thread.sleep(1000); // 1 second
0N/A if ((i % intervalForCounterCheck) == 0) {
0N/A checkCounters();
0N/A }
0N/A }
0N/A runrun = false;
0N/A for (int i = 0; i < tasks.length; i++) {
0N/A tasks[i].join();
0N/A }
0N/A } catch (InterruptedException e) {
0N/A }
0N/A
0N/A printCounters();
0N/A }
0N/A
0N/A static void checkCounters() {
0N/A int length = counters.length();
0N/A int[] snapshot = new int[length];
0N/A for (int i = 0; i < length; i++) {
0N/A snapshot[i] = counters.get(i);
0N/A }
0N/A
0N/A if (prevCounters == null) {
0N/A prevCounters = snapshot;
0N/A return;
0N/A }
0N/A
0N/A for (int i = 0; i < length; i++) {
0N/A if (snapshot[i] > prevCounters[i]) {
0N/A continue;
0N/A }
0N/A System.out.printf(
0N/A "Warning: Thread #%d hasn't updated its counter for the last %d second(s).%n",
0N/A i, intervalForCounterCheck);
0N/A }
0N/A prevCounters = snapshot;
0N/A }
0N/A
0N/A static void printCounters() {
0N/A long total = 0;
0N/A int min = Integer.MAX_VALUE;
0N/A int max = Integer.MIN_VALUE;
0N/A for (int i = 0; i < counters.length(); i++) {
0N/A int counter = counters.get(i);
0N/A total += counter;
0N/A min = Math.min(min, counter);
0N/A max = Math.max(max, counter);
0N/A }
0N/A System.out.printf("Total: %d calls, min=%d, max=%d, cache cleared %d times%n",
0N/A total, min, max, clearCounter.get());
0N/A }
0N/A
0N/A static class Worker implements Runnable {
0N/A final int id;
0N/A final int index;
0N/A final Locale locale;
0N/A final String str;
0N/A final int max;
0N/A final boolean cleaner;
0N/A ResourceBundle.Control control;
0N/A
0N/A Worker(int i) {
0N/A id = i;
0N/A index = i % locales.length;
0N/A locale = locales[index];
0N/A cleaner = locale.equals(ROOT_LOCALE);
0N/A str = expected[index];
0N/A max = rand.nextInt((index + 1) * 500) + 1000;
0N/A control = new TestControl(max);
0N/A System.out.println("Worker" + i + ": locale="+locale+", expected="+str+
0N/A ", max="+max);
0N/A }
0N/A
0N/A public void run() {
0N/A while (runrun) {
0N/A ResourceBundle rb = ResourceBundle.getBundle("StressOut", locale, control);
0N/A counters.incrementAndGet(id);
0N/A String s = rb.getString("data");
0N/A if (!s.equals(str)) {
0N/A runrun = false;
0N/A throw new RuntimeException(locale + ": rb.locale=" + rb.getLocale() +
0N/A ", got " + s + ", expected " + str);
0N/A }
0N/A try {
0N/A Thread.sleep(rand.nextInt(max/500));
0N/A } catch (InterruptedException e) {
0N/A }
0N/A if (cleaner && (rand.nextInt(10000) == 0)) {
0N/A //System.out.println("Clearing cache!");
0N/A ResourceBundle.clearCache();
0N/A clearCounter.incrementAndGet();
0N/A }
0N/A }
0N/A }
0N/A
0N/A static class TestControl extends ResourceBundle.Control {
0N/A int max;
0N/A
0N/A public List<Locale> getCandidateLocales(String baseName, Locale locale) {
0N/A List<Locale> list = super.getCandidateLocales(baseName, locale);
0N/A //System.out.println("Candidate locales=" + list);
0N/A return list;
0N/A }
0N/A public TestControl(int max) {
0N/A this.max = max;
0N/A }
0N/A public long getTimeToLive(String baseName, Locale locale) {
0N/A // This will set TTL to a random value for each bundle.
0N/A long ttl = rand.nextInt(max);
0N/A //System.out.println("TTL: " + baseName + "_" + locale + " for " + ttl);
0N/A return ttl;
0N/A }
0N/A public boolean needsReload(String baseName, Locale locale,
0N/A String format, ClassLoader loader,
0N/A ResourceBundle bundle, long loadTime) {
0N/A //System.out.println("Expired: " + baseName + "_" + locale + "." + format +
0N/A // " at " + (loadTime-startTime) + ", bundle=" + bundle);
0N/A return rand.nextBoolean();
0N/A }
0N/A }
0N/A }
0N/A}