use of net.openhft.chronicle.bytes.Bytes in project Chronicle-Bytes by OpenHFT.
the class OptimisedBytesStoreHashTest method testRandomness.
// @Test
// @Ignore("Long running, har mean score = 5436")
public void testRandomness() {
@NotNull SecureRandom rand = new SecureRandom();
long time = 0, timeCount = 0;
double scoreSum = 0;
int runs = 500;
for (int t = 0; t < runs; t++) {
@NotNull long[] hashs = new long[8192];
@NotNull byte[] init = new byte[hashs.length / 8];
Bytes b = Bytes.allocateDirect(init.length);
rand.nextBytes(init);
for (int i = 0; i < hashs.length; i++) {
b.clear();
b.write(init);
long prev = b.readLong(i >> 6 << 3);
b.writeLong(i >> 6 << 3, prev ^ (1L << i));
b.readLimit(init.length);
long start = System.nanoTime();
hashs[i] = VanillaBytesStoreHash.INSTANCE.applyAsLong(b);
time += System.nanoTime() - start;
timeCount++;
}
long score = 0;
for (int i = 0; i < hashs.length - 1; i++) for (int j = i + 1; j < hashs.length; j++) {
long diff = hashs[j] ^ hashs[i];
int diffBC = Long.bitCount(diff);
if (diffBC <= 17) {
long d = 1L << (17 - diffBC);
score += d;
}
}
scoreSum += 1.0 / score;
if (t % 50 == 0)
System.out.println(t + " - Score: " + score);
}
System.out.println("Average score: " + (long) (runs / scoreSum));
System.out.printf("Average time %.3f us%n", time / timeCount / 1e3);
}
use of net.openhft.chronicle.bytes.Bytes in project Chronicle-Bytes by OpenHFT.
the class TextLongArrayReferenceTest method getSetValues.
@Test
public void getSetValues() {
int length = 5 * 22 + 90;
Bytes bytes = Bytes.allocateDirect(length);
TextLongArrayReference.write(bytes, 5);
@NotNull TextLongArrayReference array = new TextLongArrayReference();
array.bytesStore(bytes, 0, length);
assertEquals(5, array.getCapacity());
for (int i = 0; i < 5; i++) array.setValueAt(i, i + 1);
for (int i = 0; i < 5; i++) assertEquals(i + 1, array.getValueAt(i));
@NotNull final String expected = "{ locked: false, capacity: 5 , used: 00000000000000000000, " + "values: [ 00000000000000000001, 00000000000000000002, 00000000000000000003, 00000000000000000004, 00000000000000000005 ] }\n";
System.out.println(expected.length());
assertEquals(expected, bytes.toString());
bytes.release();
}
use of net.openhft.chronicle.bytes.Bytes in project Chronicle-Bytes by OpenHFT.
the class LZWTest method testCompressionRatio.
@Test
public void testCompressionRatio() throws IORuntimeException {
@NotNull byte[] bytes = new byte[1 << 20];
Arrays.fill(bytes, (byte) 'X');
@NotNull Random rand = new Random();
for (int i = 0; i < bytes.length; i += 40) bytes[rand.nextInt(bytes.length)] = '1';
byte[] compress = LZW.compress(bytes);
System.out.println(compress.length);
Bytes bytes2 = Bytes.wrapForRead(bytes);
@NotNull Bytes bytes3 = Bytes.allocateElasticDirect();
LZW.compress(bytes2, bytes3);
@NotNull byte[] bytes4 = bytes3.toByteArray();
byte[] bytes5 = LZW.uncompress(bytes4);
// assertEquals(Arrays.toString(bytes).replace(", ", "\n"),
// Arrays.toString(bytes5).replace(", ", "\n"));
// assertEquals(Arrays.toString(compress).replace(", ", "\n"),
// Arrays.toString(bytes4).replace(", ", "\n"));
assertEquals(compress.length, bytes4.length);
assertTrue(Arrays.equals(compress, bytes4));
@NotNull Bytes bytes6 = Bytes.allocateElasticDirect();
LZW.uncompress(bytes3, bytes6);
assertTrue(Arrays.equals(bytes, bytes6.toByteArray()));
// assertEquals(Arrays.toString(bytes).replace(", ", "\n"),
// Arrays.toString(bytes6.toByteArray()).replace(", ", "\n"));
}
Aggregations