remove RandomStringGenerator, using random strings from randomized testing

This commit is contained in:
Shay Banon 2013-07-29 01:06:09 +02:00
parent d2842a936e
commit 94c8834dd3
7 changed files with 29 additions and 330 deletions

View File

@ -1,305 +0,0 @@
/*
* Licensed to ElasticSearch and Shay Banon under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. ElasticSearch licenses this
* file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.elasticsearch.common;
import jsr166y.ThreadLocalRandom;
import java.util.Random;
public class RandomStringGenerator {
/**
* <p><code>RandomStringUtils</code> instances should NOT be constructed in
* standard programming. Instead, the class should be used as
* <code>RandomStringUtils.random(5);</code>.</p>
* <p/>
* <p>This constructor is public to permit tools that require a JavaBean instance
* to operate.</p>
*/
public RandomStringGenerator() {
super();
}
// Random
//-----------------------------------------------------------------------
/**
* <p>Creates a random string whose length is the number of characters
* specified.</p>
* <p/>
* <p>Characters will be chosen from the set of all characters.</p>
*
* @param count the length of random string to create
* @return the random string
*/
public static String random(int count) {
return random(count, false, false);
}
/**
* <p>Creates a random string whose length is the number of characters
* specified.</p>
* <p/>
* <p>Characters will be chosen from the set of characters whose
* ASCII value is between <code>32</code> and <code>126</code> (inclusive).</p>
*
* @param count the length of random string to create
* @return the random string
*/
public static String randomAscii(int count) {
return random(count, 32, 127, false, false);
}
/**
* <p>Creates a random string whose length is the number of characters
* specified.</p>
* <p/>
* <p>Characters will be chosen from the set of alphabetic
* characters.</p>
*
* @param count the length of random string to create
* @return the random string
*/
public static String randomAlphabetic(int count) {
return random(count, true, false);
}
/**
* <p>Creates a random string whose length is the number of characters
* specified.</p>
* <p/>
* <p>Characters will be chosen from the set of alpha-numeric
* characters.</p>
*
* @param count the length of random string to create
* @return the random string
*/
public static String randomAlphanumeric(int count) {
return random(count, true, true);
}
/**
* <p>Creates a random string whose length is the number of characters
* specified.</p>
* <p/>
* <p>Characters will be chosen from the set of numeric
* characters.</p>
*
* @param count the length of random string to create
* @return the random string
*/
public static String randomNumeric(int count) {
return random(count, false, true);
}
/**
* <p>Creates a random string whose length is the number of characters
* specified.</p>
* <p/>
* <p>Characters will be chosen from the set of alpha-numeric
* characters as indicated by the arguments.</p>
*
* @param count the length of random string to create
* @param letters if <code>true</code>, generated string will include
* alphabetic characters
* @param numbers if <code>true</code>, generated string will include
* numeric characters
* @return the random string
*/
public static String random(int count, boolean letters, boolean numbers) {
return random(count, 0, 0, letters, numbers);
}
/**
* <p>Creates a random string whose length is the number of characters
* specified.</p>
* <p/>
* <p>Characters will be chosen from the set of alpha-numeric
* characters as indicated by the arguments.</p>
*
* @param count the length of random string to create
* @param start the position in set of chars to start at
* @param end the position in set of chars to end before
* @param letters if <code>true</code>, generated string will include
* alphabetic characters
* @param numbers if <code>true</code>, generated string will include
* numeric characters
* @return the random string
*/
public static String random(int count, int start, int end, boolean letters, boolean numbers) {
return random(count, start, end, letters, numbers, null, ThreadLocalRandom.current());
}
/**
* <p>Creates a random string based on a variety of options, using
* default source of randomness.</p>
* <p/>
* <p>This method has exactly the same semantics as
* {@link #random(int, int, int, boolean, boolean, char[], Random)}, but
* instead of using an externally supplied source of randomness, it uses
* the internal static {@link Random} instance.</p>
*
* @param count the length of random string to create
* @param start the position in set of chars to start at
* @param end the position in set of chars to end before
* @param letters only allow letters?
* @param numbers only allow numbers?
* @param chars the set of chars to choose randoms from.
* If <code>null</code>, then it will use the set of all chars.
* @return the random string
* @throws ArrayIndexOutOfBoundsException if there are not
* <code>(end - start) + 1</code> characters in the set array.
*/
public static String random(int count, int start, int end, boolean letters, boolean numbers, char[] chars) {
return random(count, start, end, letters, numbers, chars, ThreadLocalRandom.current());
}
/**
* <p>Creates a random string based on a variety of options, using
* supplied source of randomness.</p>
* <p/>
* <p>If start and end are both <code>0</code>, start and end are set
* to <code>' '</code> and <code>'z'</code>, the ASCII printable
* characters, will be used, unless letters and numbers are both
* <code>false</code>, in which case, start and end are set to
* <code>0</code> and <code>Integer.MAX_VALUE</code>.
* <p/>
* <p>If set is not <code>null</code>, characters between start and
* end are chosen.</p>
* <p/>
* <p>This method accepts a user-supplied {@link Random}
* instance to use as a source of randomness. By seeding a single
* {@link Random} instance with a fixed seed and using it for each call,
* the same random sequence of strings can be generated repeatedly
* and predictably.</p>
*
* @param count the length of random string to create
* @param start the position in set of chars to start at
* @param end the position in set of chars to end before
* @param letters only allow letters?
* @param numbers only allow numbers?
* @param chars the set of chars to choose randoms from.
* If <code>null</code>, then it will use the set of all chars.
* @param random a source of randomness.
* @return the random string
* @throws ArrayIndexOutOfBoundsException if there are not
* <code>(end - start) + 1</code> characters in the set array.
* @throws IllegalArgumentException if <code>count</code> &lt; 0.
* @since 2.0
*/
public static String random(int count, int start, int end, boolean letters, boolean numbers,
char[] chars, Random random) {
if (count == 0) {
return "";
} else if (count < 0) {
throw new IllegalArgumentException("Requested random string length " + count + " is less than 0.");
}
if ((start == 0) && (end == 0)) {
end = 'z' + 1;
start = ' ';
if (!letters && !numbers) {
start = 0;
end = Integer.MAX_VALUE;
}
}
char[] buffer = new char[count];
int gap = end - start;
while (count-- != 0) {
char ch;
if (chars == null) {
ch = (char) (random.nextInt(gap) + start);
} else {
ch = chars[random.nextInt(gap) + start];
}
if ((letters && Character.isLetter(ch))
|| (numbers && Character.isDigit(ch))
|| (!letters && !numbers)) {
if (ch >= 56320 && ch <= 57343) {
if (count == 0) {
count++;
} else {
// low surrogate, insert high surrogate after putting it in
buffer[count] = ch;
count--;
buffer[count] = (char) (55296 + random.nextInt(128));
}
} else if (ch >= 55296 && ch <= 56191) {
if (count == 0) {
count++;
} else {
// high surrogate, insert low surrogate before putting it in
buffer[count] = (char) (56320 + random.nextInt(128));
count--;
buffer[count] = ch;
}
} else if (ch >= 56192 && ch <= 56319) {
// private high surrogate, no effing clue, so skip it
count++;
} else {
buffer[count] = ch;
}
} else {
count++;
}
}
return new String(buffer);
}
/**
* <p>Creates a random string whose length is the number of characters
* specified.</p>
* <p/>
* <p>Characters will be chosen from the set of characters
* specified.</p>
*
* @param count the length of random string to create
* @param chars the String containing the set of characters to use,
* may be null
* @return the random string
* @throws IllegalArgumentException if <code>count</code> &lt; 0.
*/
public static String random(int count, String chars) {
if (chars == null) {
return random(count, 0, 0, false, false, null, ThreadLocalRandom.current());
}
return random(count, chars.toCharArray());
}
/**
* <p>Creates a random string whose length is the number of characters
* specified.</p>
* <p/>
* <p>Characters will be chosen from the set of characters specified.</p>
*
* @param count the length of random string to create
* @param chars the character array containing the set of characters to use,
* may be null
* @return the random string
* @throws IllegalArgumentException if <code>count</code> &lt; 0.
*/
public static String random(int count, char[] chars) {
if (chars == null) {
return random(count, 0, 0, false, false, null, ThreadLocalRandom.current());
}
return random(count, 0, chars.length, false, false, chars, ThreadLocalRandom.current());
}
}

View File

@ -19,6 +19,7 @@
package org.elasticsearch.benchmark.search.facet;
import com.carrotsearch.randomizedtesting.generators.RandomStrings;
import com.google.common.collect.Lists;
import jsr166y.ThreadLocalRandom;
import org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse;
@ -28,7 +29,6 @@ import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.Requests;
import org.elasticsearch.common.RandomStringGenerator;
import org.elasticsearch.common.StopWatch;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.unit.SizeValue;
@ -37,6 +37,7 @@ import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.node.Node;
import java.util.List;
import java.util.Random;
import static org.elasticsearch.client.Requests.createIndexRequest;
import static org.elasticsearch.cluster.metadata.IndexMetaData.SETTING_NUMBER_OF_REPLICAS;
@ -64,6 +65,8 @@ public class TermsFacetSearchBenchmark {
static Client client;
public static void main(String[] args) throws Exception {
Random random = new Random();
Settings settings = settingsBuilder()
.put("index.refresh_interval", "-1")
.put("gateway.type", "local")
@ -86,7 +89,7 @@ public class TermsFacetSearchBenchmark {
}
String[] sValues = new String[NUMBER_OF_TERMS];
for (int i = 0; i < NUMBER_OF_TERMS; i++) {
sValues[i] = RandomStringGenerator.randomAlphabetic(STRING_TERM_SIZE);
sValues[i] = RandomStrings.randomAsciiOfLength(random, STRING_TERM_SIZE);
}
Thread.sleep(10000);

View File

@ -19,6 +19,7 @@
package org.elasticsearch.benchmark.trove;
import com.carrotsearch.randomizedtesting.generators.RandomStrings;
import gnu.trove.map.custom_hash.TObjectIntCustomHashMap;
import gnu.trove.map.hash.THashMap;
import gnu.trove.map.hash.TIntIntHashMap;
@ -26,7 +27,6 @@ import gnu.trove.map.hash.TIntObjectHashMap;
import gnu.trove.map.hash.TObjectIntHashMap;
import gnu.trove.strategy.IdentityHashingStrategy;
import jsr166y.ThreadLocalRandom;
import org.elasticsearch.common.RandomStringGenerator;
import org.elasticsearch.common.StopWatch;
import org.elasticsearch.common.trove.StringIdentityHashingStrategy;
import org.elasticsearch.common.unit.SizeValue;
@ -47,7 +47,7 @@ public class StringMapAdjustOrPutBenchmark {
String[] values = new String[NUMBER_OF_KEYS];
for (int i = 0; i < values.length; i++) {
values[i] = RandomStringGenerator.randomAlphabetic(STRING_SIZE);
values[i] = RandomStrings.randomAsciiOfLength(ThreadLocalRandom.current(), STRING_SIZE);
}
StopWatch stopWatch;

View File

@ -1,7 +1,6 @@
package org.elasticsearch.test.integration.search.facet;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.common.RandomStringGenerator;
import org.elasticsearch.common.collect.Tuple;
import org.elasticsearch.common.regex.Regex;
import org.elasticsearch.common.settings.Settings;
@ -23,7 +22,7 @@ import static org.hamcrest.Matchers.equalTo;
/**
*/
public class ExtendedFacetsTests extends AbstractSharedClusterTest {
@Override
public Settings getSettings() {
return randomSettingsBuilder()
@ -87,13 +86,13 @@ public class ExtendedFacetsTests extends AbstractSharedClusterTest {
int numOfQueryValues = 50;
String[] queryValues = new String[numOfQueryValues];
for (int i = 0; i < numOfQueryValues; i++) {
queryValues[i] = RandomStringGenerator.random(5, 0, 0, true, true, null, random);
queryValues[i] = randomAsciiOfLength(5);
}
Set<String> uniqueValuesSet = new HashSet<String>();
int numOfVals = 400;
for (int i = 0; i < numOfVals; i++) {
uniqueValuesSet.add(RandomStringGenerator.random(10, 0, 0, true, true, null, random));
uniqueValuesSet.add(randomAsciiOfLength(10));
}
String[] allUniqueFieldValues = uniqueValuesSet.toArray(new String[uniqueValuesSet.size()]);

View File

@ -23,7 +23,6 @@ import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.common.Priority;
import org.elasticsearch.common.RandomStringGenerator;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.search.suggest.Suggest;
@ -35,7 +34,6 @@ import java.util.Locale;
import static org.elasticsearch.common.settings.ImmutableSettings.settingsBuilder;
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.is;
@ -62,21 +60,21 @@ public class CustomSuggesterSearchTests extends AbstractNodesTests {
@Test
public void testThatCustomSuggestersCanBeRegisteredAndWork() throws Exception {
String randomText = RandomStringGenerator.randomAlphanumeric(10);
String randomField = RandomStringGenerator.randomAlphanumeric(10);
String randomSuffix = RandomStringGenerator.randomAlphanumeric(10);
String randomText = randomAsciiOfLength(10);
String randomField = randomAsciiOfLength(10);
String randomSuffix = randomAsciiOfLength(10);
SearchRequestBuilder searchRequestBuilder = client.prepareSearch("test").setTypes("test").setFrom(0).setSize(1);
XContentBuilder query = jsonBuilder().startObject()
.startObject("suggest")
.startObject("someName")
.field("text", randomText)
.startObject("custom")
.field("field", randomField)
.field("suffix", randomSuffix)
.endObject()
.endObject()
.startObject("someName")
.field("text", randomText)
.startObject("custom")
.field("field", randomField)
.field("suffix", randomSuffix)
.endObject()
.endObject();
.endObject()
.endObject()
.endObject();
searchRequestBuilder.setExtraSource(query.bytes());
SearchResponse searchResponse = searchRequestBuilder.execute().actionGet();

View File

@ -19,11 +19,11 @@
package org.elasticsearch.test.stress.rollingrestart;
import com.carrotsearch.randomizedtesting.generators.RandomStrings;
import jsr166y.ThreadLocalRandom;
import org.elasticsearch.ElasticSearchException;
import org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse;
import org.elasticsearch.cluster.ClusterState;
import org.elasticsearch.common.RandomStringGenerator;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.unit.SizeValue;
@ -31,6 +31,7 @@ import org.elasticsearch.node.Node;
import org.elasticsearch.node.NodeBuilder;
import java.util.Date;
import java.util.Random;
/**
*/
@ -39,6 +40,8 @@ public class QuickRollingRestartStressTest {
public static void main(String[] args) throws Exception {
System.setProperty("es.logger.prefix", "");
Random random = new Random();
Settings settings = ImmutableSettings.settingsBuilder().build();
Node[] nodes = new Node[5];
@ -61,7 +64,7 @@ public class QuickRollingRestartStressTest {
System.out.println("--> indexing data...");
for (long i = 0; i < COUNT; i++) {
client.client().prepareIndex("test", "type", Long.toString(i))
.setSource("date", new Date(), "data", RandomStringGenerator.randomAlphabetic(10000))
.setSource("date", new Date(), "data", RandomStrings.randomAsciiOfLength(random, 10000))
.execute().actionGet();
}
System.out.println("--> done indexing data [" + COUNT + "]");

View File

@ -1,10 +1,11 @@
package org.elasticsearch.test.stress.search1;
import com.carrotsearch.randomizedtesting.generators.RandomStrings;
import jsr166y.ThreadLocalRandom;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.common.RandomStringGenerator;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.index.query.QueryBuilders;
@ -34,7 +35,7 @@ public class ConcurrentSearchSerializationTests {
final Client client = node1.client();
System.out.println("Indexing...");
final String data = RandomStringGenerator.random(100);
final String data = RandomStrings.randomAsciiOfLength(ThreadLocalRandom.current(), 100);
final CountDownLatch latch1 = new CountDownLatch(100);
for (int i = 0; i < 100; i++) {
client.prepareIndex("test", "type", Integer.toString(i))