Skip to content

Instantly share code, notes, and snippets.

@pochemuto
Created July 19, 2017 14:38
Show Gist options
  • Save pochemuto/214ecf69ba140717c412eb961ec7b012 to your computer and use it in GitHub Desktop.
Save pochemuto/214ecf69ba140717c412eb961ec7b012 to your computer and use it in GitHub Desktop.
SkuMemTest
import java.util.Map;
import java.util.Objects;
import java.util.Random;
import java.util.Scanner;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/**
* @author Alexander Kramarev (pochemuto@yandex-team.ru)
* @date 19.07.2017
*/
public class SkuMemTest {
private static final int SIZE = 1_000_000;
private static final int PARAMS_COUNT = 6;
private static final Random RANDOM = new Random();
private static final int SKU_LENGTH = 20;
private ConcurrentMap<Key, String> map;
public static void main(String... args) {
SkuMemTest t = new SkuMemTest();
t.perform();
}
private void perform() {
map = new ConcurrentHashMap<>(SIZE);
System.out.println("generating...");
Stream.generate(this::randomKey)
.limit(SIZE)
.forEach(k -> map.put(k, randomSku()));
System.out.println(map.size() + " done.");
System.out.println("Press enter to exit...");
new Scanner(System.in).next();
logSize(map);
}
private void logSize(Map<Key, String> map) {
System.out.println("size: " + map.size());
}
@SuppressWarnings("CheckStyle")
private String randomSku() {
return RANDOM.ints(SKU_LENGTH).map(i -> i % 10)
.mapToObj(String::valueOf)
.collect(Collectors.joining());
}
private Key randomKey() {
Set<ParamValue> values = Stream.generate(this::randomValue)
.limit(PARAMS_COUNT)
.collect(Collectors.toSet());
return new Key(RANDOM.nextLong(), values);
}
private ParamValue randomValue() {
return new ParamValue(
RANDOM.nextLong(),
RANDOM.nextLong(),
RANDOM.nextBoolean(),
RANDOM.nextDouble()
);
}
}
class Key {
private final long modelId;
private final Set<ParamValue> values;
Key(long modelId, Set<ParamValue> values) {
this.modelId = modelId;
this.values = values;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Key key = (Key) o;
return modelId == key.modelId &&
Objects.equals(values, key.values);
}
@Override
public int hashCode() {
return Objects.hash(modelId, values);
}
}
class ParamValue {
private final long paramId;
private final long valueId;
private final boolean booleanValue;
private final double numberValue;
ParamValue(long paramId, long valueId, boolean booleanValue, double numberValue) {
this.paramId = paramId;
this.valueId = valueId;
this.booleanValue = booleanValue;
this.numberValue = numberValue;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ParamValue that = (ParamValue) o;
return paramId == that.paramId &&
valueId == that.valueId &&
booleanValue == that.booleanValue &&
Double.compare(that.numberValue, numberValue) == 0;
}
@Override
public int hashCode() {
return Objects.hash(paramId, valueId, booleanValue, numberValue);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment