apply new code formatter and save action
This commit is contained in:
@@ -14,133 +14,133 @@ import org.lucares.pdb.api.UniqueStringIntegerPairs;
|
||||
|
||||
public class MemoryScale {
|
||||
|
||||
public static final String A = "A";
|
||||
public static final String A = "A";
|
||||
|
||||
public static void main(final String[] args) {
|
||||
Tags.STRING_COMPRESSOR = new StringCompressor(new UniqueStringIntegerPairs());
|
||||
public static void main(final String[] args) {
|
||||
Tags.STRING_COMPRESSOR = new StringCompressor(new UniqueStringIntegerPairs());
|
||||
|
||||
scale("singleTag");
|
||||
scale("tags0");
|
||||
scale("tags1");
|
||||
scale("tags2");
|
||||
scale("tags6");
|
||||
}
|
||||
scale("singleTag");
|
||||
scale("tags0");
|
||||
scale("tags1");
|
||||
scale("tags2");
|
||||
scale("tags6");
|
||||
}
|
||||
|
||||
private static void scale(final String what) {
|
||||
System.out.println("start: " + what);
|
||||
// warmup of classes
|
||||
getUsedMemory();
|
||||
Object handle = createObject(what);
|
||||
private static void scale(final String what) {
|
||||
System.out.println("start: " + what);
|
||||
// warmup of classes
|
||||
getUsedMemory();
|
||||
Object handle = createObject(what);
|
||||
|
||||
handle = null;
|
||||
handle = null;
|
||||
|
||||
runGc();
|
||||
final long memoryBefore = getUsedMemory();
|
||||
runGc();
|
||||
final long memoryBefore = getUsedMemory();
|
||||
|
||||
handle = createObject(what);
|
||||
handle = createObject(what);
|
||||
|
||||
runGc();
|
||||
final long memoryAfter = getUsedMemory();
|
||||
System.out.println(what + ": used memory: " + (memoryAfter - memoryBefore));
|
||||
handle.hashCode(); // use the variable, so causes no warnings and is not removed by JIT compiler
|
||||
}
|
||||
runGc();
|
||||
final long memoryAfter = getUsedMemory();
|
||||
System.out.println(what + ": used memory: " + (memoryAfter - memoryBefore));
|
||||
handle.hashCode(); // use the variable, so causes no warnings and is not removed by JIT compiler
|
||||
}
|
||||
|
||||
private static Object createObject(final String what) {
|
||||
private static Object createObject(final String what) {
|
||||
|
||||
switch (what) {
|
||||
case "singleTag":
|
||||
return createTag();
|
||||
case "tags0":
|
||||
return createTags0();
|
||||
case "tags1":
|
||||
return createTags1();
|
||||
case "tags2":
|
||||
return createTags2();
|
||||
case "tags6":
|
||||
return createTags6();
|
||||
case "string":
|
||||
return createString();
|
||||
case "linkedHashMap":
|
||||
return createLinkedHashMap();
|
||||
case "path":
|
||||
return createPath("C:\\pdb\\dataNew\\storage\\0\\4\\3n-5k_0-5l_2-1L_4-4n_3w-5h_6-7$.pdb");
|
||||
case "pathAsString":
|
||||
return createPathAsString("C:\\pdb\\dataNew\\storage\\0\\4\\3n-5k_0-5l_2-1L_4-4n_3w-5h_6-7$.pdb");
|
||||
case "pathAsUtf8":
|
||||
return createPathAsUtf8("C:\\pdb\\dataNew\\storage\\0\\4\\3n-5k_0-5l_2-1L_4-4n_3w-5h_6-7$.pdb");
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
switch (what) {
|
||||
case "singleTag":
|
||||
return createTag();
|
||||
case "tags0":
|
||||
return createTags0();
|
||||
case "tags1":
|
||||
return createTags1();
|
||||
case "tags2":
|
||||
return createTags2();
|
||||
case "tags6":
|
||||
return createTags6();
|
||||
case "string":
|
||||
return createString();
|
||||
case "linkedHashMap":
|
||||
return createLinkedHashMap();
|
||||
case "path":
|
||||
return createPath("C:\\pdb\\dataNew\\storage\\0\\4\\3n-5k_0-5l_2-1L_4-4n_3w-5h_6-7$.pdb");
|
||||
case "pathAsString":
|
||||
return createPathAsString("C:\\pdb\\dataNew\\storage\\0\\4\\3n-5k_0-5l_2-1L_4-4n_3w-5h_6-7$.pdb");
|
||||
case "pathAsUtf8":
|
||||
return createPathAsUtf8("C:\\pdb\\dataNew\\storage\\0\\4\\3n-5k_0-5l_2-1L_4-4n_3w-5h_6-7$.pdb");
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private static Object createTag() {
|
||||
return new Tag("", "");
|
||||
}
|
||||
private static Object createTag() {
|
||||
return new Tag("", "");
|
||||
}
|
||||
|
||||
private static Object createTags0() {
|
||||
return new Tags();
|
||||
}
|
||||
private static Object createTags0() {
|
||||
return new Tags();
|
||||
}
|
||||
|
||||
private static Object createTags1() {
|
||||
return Tags.createAndAddToDictionary("k1", "v1");
|
||||
}
|
||||
private static Object createTags1() {
|
||||
return Tags.createAndAddToDictionary("k1", "v1");
|
||||
}
|
||||
|
||||
private static Object createTags2() {
|
||||
return Tags.createAndAddToDictionary("k1", "v1", "k2", "v2");
|
||||
}
|
||||
private static Object createTags2() {
|
||||
return Tags.createAndAddToDictionary("k1", "v1", "k2", "v2");
|
||||
}
|
||||
|
||||
private static Object createTags6() {
|
||||
TagsBuilder result = TagsBuilder.create();
|
||||
result = result.add("k1", "v1");
|
||||
result = result.add("k2", "v2");
|
||||
result = result.add("k3", "v3");
|
||||
result = result.add("k4", "v4");
|
||||
result = result.add("k5", "v5");
|
||||
result = result.add("k6", "v6");
|
||||
return result.build();
|
||||
}
|
||||
private static Object createTags6() {
|
||||
TagsBuilder result = TagsBuilder.create();
|
||||
result = result.add("k1", "v1");
|
||||
result = result.add("k2", "v2");
|
||||
result = result.add("k3", "v3");
|
||||
result = result.add("k4", "v4");
|
||||
result = result.add("k5", "v5");
|
||||
result = result.add("k6", "v6");
|
||||
return result.build();
|
||||
}
|
||||
|
||||
private static Object createPathAsUtf8(final String string) {
|
||||
return string.getBytes(StandardCharsets.UTF_8);
|
||||
}
|
||||
private static Object createPathAsUtf8(final String string) {
|
||||
return string.getBytes(StandardCharsets.UTF_8);
|
||||
}
|
||||
|
||||
private static String createPathAsString(final String string) {
|
||||
return string.replace("C", "c");
|
||||
}
|
||||
private static String createPathAsString(final String string) {
|
||||
return string.replace("C", "c");
|
||||
}
|
||||
|
||||
private static Path createPath(final String string) {
|
||||
return Paths.get(string);
|
||||
}
|
||||
private static Path createPath(final String string) {
|
||||
return Paths.get(string);
|
||||
}
|
||||
|
||||
private static String createString() {
|
||||
private static String createString() {
|
||||
|
||||
final int i = 0;
|
||||
return "" + i;
|
||||
}
|
||||
final int i = 0;
|
||||
return "" + i;
|
||||
}
|
||||
|
||||
private static Object createLinkedHashMap() {
|
||||
final Map<String, String> map = new LinkedHashMap<>();
|
||||
private static Object createLinkedHashMap() {
|
||||
final Map<String, String> map = new LinkedHashMap<>();
|
||||
|
||||
map.put("A", "A");
|
||||
for (int i = 0; i < 0; i++) {
|
||||
map.put("" + i, "" + i);
|
||||
}
|
||||
map.put("A", "A");
|
||||
for (int i = 0; i < 0; i++) {
|
||||
map.put("" + i, "" + i);
|
||||
}
|
||||
|
||||
return map;
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
private static void runGc() {
|
||||
for (int i = 0; i < 10; i++) {
|
||||
System.gc();
|
||||
try {
|
||||
Thread.sleep(100);
|
||||
} catch (final InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
}
|
||||
}
|
||||
private static void runGc() {
|
||||
for (int i = 0; i < 10; i++) {
|
||||
System.gc();
|
||||
try {
|
||||
Thread.sleep(100);
|
||||
} catch (final InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static long getUsedMemory() {
|
||||
return Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
|
||||
}
|
||||
private static long getUsedMemory() {
|
||||
return Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,27 +11,27 @@ import org.testng.annotations.DataProvider;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
public class DateTimeRangeTest {
|
||||
@DataProvider
|
||||
Object[][] providerIntersect() {
|
||||
final List<Object[]> result = new ArrayList<>();
|
||||
@DataProvider
|
||||
Object[][] providerIntersect() {
|
||||
final List<Object[]> result = new ArrayList<>();
|
||||
|
||||
final OffsetDateTime a = Instant.ofEpochMilli(1000).atOffset(ZoneOffset.UTC);
|
||||
final OffsetDateTime b = Instant.ofEpochMilli(2000).atOffset(ZoneOffset.UTC);
|
||||
final OffsetDateTime c = Instant.ofEpochMilli(3000).atOffset(ZoneOffset.UTC);
|
||||
final OffsetDateTime d = Instant.ofEpochMilli(4000).atOffset(ZoneOffset.UTC);
|
||||
final OffsetDateTime a = Instant.ofEpochMilli(1000).atOffset(ZoneOffset.UTC);
|
||||
final OffsetDateTime b = Instant.ofEpochMilli(2000).atOffset(ZoneOffset.UTC);
|
||||
final OffsetDateTime c = Instant.ofEpochMilli(3000).atOffset(ZoneOffset.UTC);
|
||||
final OffsetDateTime d = Instant.ofEpochMilli(4000).atOffset(ZoneOffset.UTC);
|
||||
|
||||
result.add(new Object[] { new DateTimeRange(a, b), new DateTimeRange(c, d), false });
|
||||
result.add(new Object[] { new DateTimeRange(a, c), new DateTimeRange(b, d), true });
|
||||
result.add(new Object[] { new DateTimeRange(a, d), new DateTimeRange(b, d), true });
|
||||
result.add(new Object[] { new DateTimeRange(a, d), new DateTimeRange(b, d), true });
|
||||
result.add(new Object[] { new DateTimeRange(a, b), new DateTimeRange(b, d), true });
|
||||
result.add(new Object[] { new DateTimeRange(a, b), new DateTimeRange(c, d), false });
|
||||
result.add(new Object[] { new DateTimeRange(a, c), new DateTimeRange(b, d), true });
|
||||
result.add(new Object[] { new DateTimeRange(a, d), new DateTimeRange(b, d), true });
|
||||
result.add(new Object[] { new DateTimeRange(a, d), new DateTimeRange(b, d), true });
|
||||
result.add(new Object[] { new DateTimeRange(a, b), new DateTimeRange(b, d), true });
|
||||
|
||||
return result.toArray(new Object[result.size()][]);
|
||||
}
|
||||
return result.toArray(new Object[result.size()][]);
|
||||
}
|
||||
|
||||
@Test(dataProvider = "providerIntersect")
|
||||
public void testIntersect(final DateTimeRange a, final DateTimeRange b, final boolean expected) throws Exception {
|
||||
Assert.assertEquals(a.intersect(b), expected, a + " intersects " + b);
|
||||
Assert.assertEquals(b.intersect(a), expected, a + " intersects " + b);
|
||||
}
|
||||
@Test(dataProvider = "providerIntersect")
|
||||
public void testIntersect(final DateTimeRange a, final DateTimeRange b, final boolean expected) throws Exception {
|
||||
Assert.assertEquals(a.intersect(b), expected, a + " intersects " + b);
|
||||
Assert.assertEquals(b.intersect(a), expected, a + " intersects " + b);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,63 +18,63 @@ import org.testng.annotations.Test;
|
||||
|
||||
@Test
|
||||
public class StringCompressorTest {
|
||||
private Path dataDirectory;
|
||||
private Path dataDirectory;
|
||||
|
||||
@BeforeMethod
|
||||
public void beforeMethod() throws IOException {
|
||||
dataDirectory = Files.createTempDirectory("pdb");
|
||||
}
|
||||
@BeforeMethod
|
||||
public void beforeMethod() throws IOException {
|
||||
dataDirectory = Files.createTempDirectory("pdb");
|
||||
}
|
||||
|
||||
@AfterMethod
|
||||
public void afterMethod() throws IOException {
|
||||
FileUtils.delete(dataDirectory);
|
||||
}
|
||||
@AfterMethod
|
||||
public void afterMethod() throws IOException {
|
||||
FileUtils.delete(dataDirectory);
|
||||
}
|
||||
|
||||
public void testKeyCompressorRoundtrip() throws Exception {
|
||||
final StringCompressor keyValueCompressor = StringCompressor.create(dataDirectory.resolve("key.csv"));
|
||||
public void testKeyCompressorRoundtrip() throws Exception {
|
||||
final StringCompressor keyValueCompressor = StringCompressor.create(dataDirectory.resolve("key.csv"));
|
||||
|
||||
final String value = "foo";
|
||||
final Integer intFoo = keyValueCompressor.put(value);
|
||||
final String actual = keyValueCompressor.get(intFoo);
|
||||
final String value = "foo";
|
||||
final Integer intFoo = keyValueCompressor.put(value);
|
||||
final String actual = keyValueCompressor.get(intFoo);
|
||||
|
||||
Assert.assertEquals(actual, value);
|
||||
}
|
||||
Assert.assertEquals(actual, value);
|
||||
}
|
||||
|
||||
public void testKeyCompressorInitialization() throws Exception {
|
||||
final Path database = dataDirectory.resolve("key.csv");
|
||||
final String value = "foo";
|
||||
{
|
||||
final StringCompressor keyValueCompressor = StringCompressor.create(database);
|
||||
public void testKeyCompressorInitialization() throws Exception {
|
||||
final Path database = dataDirectory.resolve("key.csv");
|
||||
final String value = "foo";
|
||||
{
|
||||
final StringCompressor keyValueCompressor = StringCompressor.create(database);
|
||||
|
||||
keyValueCompressor.put(value);
|
||||
}
|
||||
{
|
||||
final StringCompressor keyValueCompressor = StringCompressor.create(database);
|
||||
keyValueCompressor.put(value);
|
||||
}
|
||||
{
|
||||
final StringCompressor keyValueCompressor = StringCompressor.create(database);
|
||||
|
||||
keyValueCompressor.get(0);
|
||||
}
|
||||
keyValueCompressor.get(0);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@Test(invocationCount = 1)
|
||||
public void testPutConcurrently() throws InterruptedException, ExecutionException {
|
||||
final UniqueStringIntegerPairs usip = new UniqueStringIntegerPairs();
|
||||
final StringCompressor stringCompressor = new StringCompressor(usip);
|
||||
@Test(invocationCount = 1)
|
||||
public void testPutConcurrently() throws InterruptedException, ExecutionException {
|
||||
final UniqueStringIntegerPairs usip = new UniqueStringIntegerPairs();
|
||||
final StringCompressor stringCompressor = new StringCompressor(usip);
|
||||
|
||||
final ExecutorService pool = Executors.newCachedThreadPool();
|
||||
final ExecutorService pool = Executors.newCachedThreadPool();
|
||||
|
||||
final int numEntries = 1000;
|
||||
final Future<List<String>> future1 = pool.submit(new StringInserter(stringCompressor, numEntries));
|
||||
final Future<List<String>> future2 = pool.submit(new StringInserter(stringCompressor, numEntries));
|
||||
final Future<List<String>> future3 = pool.submit(new StringInserter(stringCompressor, numEntries));
|
||||
final int numEntries = 1000;
|
||||
final Future<List<String>> future1 = pool.submit(new StringInserter(stringCompressor, numEntries));
|
||||
final Future<List<String>> future2 = pool.submit(new StringInserter(stringCompressor, numEntries));
|
||||
final Future<List<String>> future3 = pool.submit(new StringInserter(stringCompressor, numEntries));
|
||||
|
||||
future1.get();
|
||||
future2.get();
|
||||
future3.get();
|
||||
future1.get();
|
||||
future2.get();
|
||||
future3.get();
|
||||
|
||||
pool.shutdown();
|
||||
pool.awaitTermination(1, TimeUnit.MILLISECONDS);
|
||||
pool.shutdown();
|
||||
pool.awaitTermination(1, TimeUnit.MILLISECONDS);
|
||||
|
||||
Assert.assertEquals((int) usip.getHighestInteger(), 3 * numEntries - 1);
|
||||
}
|
||||
Assert.assertEquals((int) usip.getHighestInteger(), 3 * numEntries - 1);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,23 +7,23 @@ import java.util.concurrent.Callable;
|
||||
|
||||
final class StringInserter implements Callable<List<String>> {
|
||||
|
||||
private final StringCompressor stringCompressor;
|
||||
private final int numEntries;
|
||||
private final StringCompressor stringCompressor;
|
||||
private final int numEntries;
|
||||
|
||||
public StringInserter(final StringCompressor stringCompressor, final int numEntries) {
|
||||
this.stringCompressor = stringCompressor;
|
||||
this.numEntries = numEntries;
|
||||
}
|
||||
public StringInserter(final StringCompressor stringCompressor, final int numEntries) {
|
||||
this.stringCompressor = stringCompressor;
|
||||
this.numEntries = numEntries;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> call() throws Exception {
|
||||
@Override
|
||||
public List<String> call() throws Exception {
|
||||
|
||||
final List<String> result = new ArrayList<>();
|
||||
for (int i = 0; i < numEntries; i++) {
|
||||
final String s = UUID.randomUUID().toString();
|
||||
stringCompressor.put(s);
|
||||
result.add(s);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
final List<String> result = new ArrayList<>();
|
||||
for (int i = 0; i < numEntries; i++) {
|
||||
final String s = UUID.randomUUID().toString();
|
||||
stringCompressor.put(s);
|
||||
result.add(s);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@@ -13,62 +13,62 @@ import org.testng.annotations.Test;
|
||||
@Test
|
||||
public class UniqueStringIntegerPairsTest {
|
||||
|
||||
private Path dataDirectory;
|
||||
private Path dataDirectory;
|
||||
|
||||
@BeforeMethod
|
||||
public void beforeMethod() throws IOException {
|
||||
dataDirectory = Files.createTempDirectory("pdb");
|
||||
}
|
||||
@BeforeMethod
|
||||
public void beforeMethod() throws IOException {
|
||||
dataDirectory = Files.createTempDirectory("pdb");
|
||||
}
|
||||
|
||||
@AfterMethod
|
||||
public void afterMethod() throws IOException {
|
||||
FileUtils.delete(dataDirectory);
|
||||
}
|
||||
@AfterMethod
|
||||
public void afterMethod() throws IOException {
|
||||
FileUtils.delete(dataDirectory);
|
||||
}
|
||||
|
||||
public void testPutGet() throws Exception {
|
||||
final Path database = dataDirectory.resolve("key.csv");
|
||||
final String first = "key1";
|
||||
final Integer second = 1;
|
||||
public void testPutGet() throws Exception {
|
||||
final Path database = dataDirectory.resolve("key.csv");
|
||||
final String first = "key1";
|
||||
final Integer second = 1;
|
||||
|
||||
{
|
||||
final UniqueStringIntegerPairs usip = new UniqueStringIntegerPairs(database);
|
||||
{
|
||||
final UniqueStringIntegerPairs usip = new UniqueStringIntegerPairs(database);
|
||||
|
||||
usip.put(first, second);
|
||||
Assert.assertEquals(usip.get(first), second);
|
||||
Assert.assertEquals(usip.getKey(second), first);
|
||||
}
|
||||
usip.put(first, second);
|
||||
Assert.assertEquals(usip.get(first), second);
|
||||
Assert.assertEquals(usip.getKey(second), first);
|
||||
}
|
||||
|
||||
{
|
||||
final UniqueStringIntegerPairs usip = new UniqueStringIntegerPairs(database);
|
||||
{
|
||||
final UniqueStringIntegerPairs usip = new UniqueStringIntegerPairs(database);
|
||||
|
||||
Assert.assertEquals(usip.get(first), second);
|
||||
Assert.assertEquals(usip.getKey(second), first);
|
||||
}
|
||||
}
|
||||
Assert.assertEquals(usip.get(first), second);
|
||||
Assert.assertEquals(usip.getKey(second), first);
|
||||
}
|
||||
}
|
||||
|
||||
public void testUniqueKeyContstraint() throws Exception {
|
||||
final Path database = dataDirectory.resolve("key.csv");
|
||||
final String first = "key1";
|
||||
final Integer second = 1;
|
||||
public void testUniqueKeyContstraint() throws Exception {
|
||||
final Path database = dataDirectory.resolve("key.csv");
|
||||
final String first = "key1";
|
||||
final Integer second = 1;
|
||||
|
||||
final UniqueStringIntegerPairs usip = new UniqueStringIntegerPairs(database);
|
||||
usip.put(first, second);
|
||||
try {
|
||||
// cannot add another pair with the first key
|
||||
final int another = second + 1;
|
||||
usip.put(first, another);
|
||||
Assert.fail("expected an IllegalArgumentException");
|
||||
} catch (final IllegalArgumentException e) {
|
||||
// expected
|
||||
}
|
||||
final UniqueStringIntegerPairs usip = new UniqueStringIntegerPairs(database);
|
||||
usip.put(first, second);
|
||||
try {
|
||||
// cannot add another pair with the first key
|
||||
final int another = second + 1;
|
||||
usip.put(first, another);
|
||||
Assert.fail("expected an IllegalArgumentException");
|
||||
} catch (final IllegalArgumentException e) {
|
||||
// expected
|
||||
}
|
||||
|
||||
try {
|
||||
// cannot add another pair with the same second value
|
||||
final String another = first + 1;
|
||||
usip.put(another, second);
|
||||
Assert.fail("expected an IllegalArgumentException");
|
||||
} catch (final IllegalArgumentException e) {
|
||||
// expected
|
||||
}
|
||||
}
|
||||
try {
|
||||
// cannot add another pair with the same second value
|
||||
final String another = first + 1;
|
||||
usip.put(another, second);
|
||||
Assert.fail("expected an IllegalArgumentException");
|
||||
} catch (final IllegalArgumentException e) {
|
||||
// expected
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user