Search in sources :

Example 51 with Bytes

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);
}
Also used : Bytes(net.openhft.chronicle.bytes.Bytes) NativeBytes(net.openhft.chronicle.bytes.NativeBytes) SecureRandom(java.security.SecureRandom) NotNull(org.jetbrains.annotations.NotNull)

Example 52 with Bytes

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();
}
Also used : Bytes(net.openhft.chronicle.bytes.Bytes) NotNull(org.jetbrains.annotations.NotNull) Test(org.junit.Test)

Example 53 with Bytes

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"));
}
Also used : Bytes(net.openhft.chronicle.bytes.Bytes) Random(java.util.Random) NotNull(org.jetbrains.annotations.NotNull) Test(org.junit.Test)

Aggregations

Bytes (net.openhft.chronicle.bytes.Bytes)53 Test (org.junit.Test)23 NotNull (org.jetbrains.annotations.NotNull)16 File (java.io.File)13 DocumentContext (net.openhft.chronicle.wire.DocumentContext)10 BytesRingBuffer (net.openhft.chronicle.queue.impl.ringbuffer.BytesRingBuffer)9 MappedBytes (net.openhft.chronicle.bytes.MappedBytes)8 Wire (net.openhft.chronicle.wire.Wire)7 ByteBuffer (java.nio.ByteBuffer)6 AtomicLong (java.util.concurrent.atomic.AtomicLong)6 ChronicleQueue (net.openhft.chronicle.queue.ChronicleQueue)6 ExcerptTailer (net.openhft.chronicle.queue.ExcerptTailer)6 InternalAppender (net.openhft.chronicle.queue.impl.single.InternalAppender)6 SingleChronicleQueue (net.openhft.chronicle.queue.impl.single.SingleChronicleQueue)6 ExcerptAppender (net.openhft.chronicle.queue.ExcerptAppender)5 NativeBytes (net.openhft.chronicle.bytes.NativeBytes)4 SingleChronicleQueueBuilder (net.openhft.chronicle.queue.impl.single.SingleChronicleQueueBuilder)4 ParseException (java.text.ParseException)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 AffinityLock (net.openhft.affinity.AffinityLock)3