Bug 58100 - Performance enhancements : Replace Random by ThreadLocalRandom

Bugzilla Id: 58100

git-svn-id: https://svn.apache.org/repos/asf/jmeter/trunk@1689154 13f79535-47bb-0310-9956-ffa450edef68

Former-commit-id: ebb9c4e453
This commit is contained in:
Philippe Mouawad 2015-07-04 13:39:31 +00:00
parent 7fd0f14c3f
commit 9df1ebb1dd
6 changed files with 13 additions and 28 deletions

View File

@ -35,7 +35,7 @@ import java.io.Reader;
import java.io.Writer;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;
import org.apache.commons.collections.ArrayStack;
import org.apache.jmeter.gui.JMeterFileFilter;
@ -83,8 +83,6 @@ public class FileServer {
private static final FileServer server = new FileServer();
private final Random random = new Random();
// volatile needed to ensure safe publication
private volatile String scriptName;
@ -514,7 +512,7 @@ public class FileServer {
if (src.isDirectory() && src.list() != null) {
File[] lfiles = src.listFiles(new JMeterFileFilter(extensions));
int count = lfiles.length;
input = lfiles[random.nextInt(count)];
input = lfiles[ThreadLocalRandom.current().nextInt(count)];
}
}
return input;

View File

@ -38,9 +38,9 @@ import java.util.List;
import java.util.Locale;
import java.util.MissingResourceException;
import java.util.Properties;
import java.util.Random;
import java.util.ResourceBundle;
import java.util.Vector;
import java.util.concurrent.ThreadLocalRandom;
import javax.swing.ImageIcon;
import javax.swing.JButton;
@ -107,9 +107,6 @@ public class JMeterUtils implements UnitTestManager {
}
};
// Provide Random numbers to whomever wants one
private static final Random rand = new Random();
/**
* Gets Perl5Matcher for this thread.
* @return the {@link Perl5Matcher} for this thread
@ -337,7 +334,7 @@ public class JMeterUtils implements UnitTestManager {
* @return a random <code>int</code>
*/
public static int getRandomInt(int r) {
return rand.nextInt(r);
return ThreadLocalRandom.current().nextInt(r);
}
/**

View File

@ -23,7 +23,7 @@ import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;
import org.apache.jmeter.engine.util.CompoundVariable;
import org.apache.jmeter.samplers.SampleResult;
@ -57,10 +57,6 @@ public class RegexFunction extends AbstractFunction {
private Object[] values;// Parameters are stored here
// Using the same Random across threads might result in pool performance
// It might make sense to use ThreadLocalRandom or ThreadLocal<Random>
private static final Random rand = new Random();
private static final List<String> desc = new LinkedList<String>();
private static final String TEMPLATE_PATTERN = "\\$(\\d+)\\$"; //$NON-NLS-1$
@ -187,7 +183,7 @@ public class RegexFunction extends AbstractFunction {
}
return value.toString();
} else if (valueIndex.equals(RAND)) {
MatchResult result = collectAllMatches.get(rand.nextInt(collectAllMatches.size()));
MatchResult result = collectAllMatches.get(ThreadLocalRandom.current().nextInt(collectAllMatches.size()));
return generateResult(result, name, tmplt, vars);
} else {
try {

View File

@ -23,7 +23,7 @@ import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;
import org.apache.jmeter.config.Argument;
import org.apache.jmeter.config.Arguments;
@ -53,8 +53,6 @@ public class AnchorModifier extends AbstractTestElement implements PreProcessor,
private static final long serialVersionUID = 240L;
private static final Random rand = new Random();
public AnchorModifier() {
}
@ -91,7 +89,7 @@ public class AnchorModifier extends AbstractTestElement implements PreProcessor,
addFormUrls(html, result, sampler, potentialLinks);
addFramesetUrls(html, result, sampler, potentialLinks);
if (potentialLinks.size() > 0) {
HTTPSamplerBase url = potentialLinks.get(rand.nextInt(potentialLinks.size()));
HTTPSamplerBase url = potentialLinks.get(ThreadLocalRandom.current().nextInt(potentialLinks.size()));
if (log.isDebugEnabled()) {
log.debug("Selected: "+url.toString());
}
@ -134,7 +132,7 @@ public class AnchorModifier extends AbstractTestElement implements PreProcessor,
}
if (possibleReplacements.size() > 0) {
replacementArg = possibleReplacements.get(rand.nextInt(possibleReplacements.size()));
replacementArg = possibleReplacements.get(ThreadLocalRandom.current().nextInt(possibleReplacements.size()));
arg.setName(replacementArg.getName());
arg.setValue(replacementArg.getValue());
if (log.isDebugEnabled()) {

View File

@ -31,7 +31,7 @@ import java.net.URL;
import java.util.Hashtable;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;
import javax.xml.parsers.DocumentBuilder;
@ -108,11 +108,6 @@ public class WebServiceSampler extends HTTPSamplerBase {
public static final boolean MAINTAIN_SESSION_DEFAULT = true;
/*
* Random class for generating random numbers.
*/
private final Random RANDOM = new Random();
private String fileContents = null;
/**
@ -166,7 +161,7 @@ public class WebServiceSampler extends HTTPSamplerBase {
File src = new File(this.getXmlPathLoc());
if (src.isDirectory() && src.list() != null) {
File [] fileList = src.listFiles(new JMeterFileFilter(new String[] { ".xml" }, false));
File one = fileList[RANDOM.nextInt(fileList.length)];
File one = fileList[ThreadLocalRandom.current().nextInt(fileList.length)];
// return the absolutePath of the file
return one.getAbsolutePath();
}

View File

@ -110,7 +110,8 @@ Summary
<h3>General</h3>
<ul>
<li><bug>57913</bug>Automated backups of last saved JMX files. Contributed by Benoit Vatan (benoit.vatan at gmail.com)</li>
<li><bug>57988</bug> Shortcuts (Ctrl+1 .. Ctrl+9) to quick add elements into test plan. Implemented by Andrey Pokhilko (andrey at blazemeter.com) and contributed by BlazeMeter Ltd.</li>
<li><bug>57988</bug>Shortcuts (Ctrl+1 .. Ctrl+9) to quick add elements into test plan. Implemented by Andrey Pokhilko (andrey at blazemeter.com) and contributed by BlazeMeter Ltd.</li>
<li><bug>58100</bug>Performance enhancements : Replace Random by ThreadLocalRandom.</li>
</ul>
<ch_section>Non-functional changes</ch_section>
<ul>