use of net.openhft.chronicle.bytes.Bytes in project Chronicle-Queue by OpenHFT.
the class SingleCQFormat2Test method appendMessage.
public void appendMessage(@NotNull ChronicleQueue queue, long expectedIndex, String msg) {
@NotNull ExcerptAppender appender = queue.acquireAppender();
switch(appendMode) {
case 1:
appender.writeDocument(w -> w.write("msg").text(msg));
break;
case 2:
Bytes bytes = Bytes.elasticByteBuffer();
new BinaryWire(bytes).write("msg").text(msg);
appender.writeBytes(bytes);
bytes.releaseLast();
break;
default:
try (DocumentContext dc = appender.writingDocument()) {
Wire wire = dc.wire();
wire.write("msg").text(msg);
}
break;
}
long index = appender.lastIndexAppended();
assertHexEquals(expectedIndex, index);
}
use of net.openhft.chronicle.bytes.Bytes in project Chronicle-Queue by OpenHFT.
the class ThroughputPerfMain method main.
public static void main(String[] args) {
String base = path + "/delete-" + Time.uniqueId() + ".me";
long start = System.nanoTime();
long count = 0;
nbs = BytesStore.nativeStoreWithFixedCapacity(size);
long blockSize = OS.is64Bit() ? 4L << 30 : 256L << 20;
try (ChronicleQueue q = ChronicleQueue.singleBuilder(base).rollCycle(RollCycles.LARGE_HOURLY_XSPARSE).blockSize(blockSize).build()) {
ExcerptAppender appender = q.acquireAppender();
long lastIndex = -1;
do {
int defaultIndexSpacing = q.rollCycle().defaultIndexSpacing();
Wire wire = appender.wire();
int writeCount = (int) (defaultIndexSpacing - (lastIndex & (defaultIndexSpacing - 1)) - 1);
if (wire != null && writeCount > 0) {
MappedBytes bytes = (MappedBytes) wire.bytes();
long address = bytes.addressForWrite(bytes.writePosition());
long bstart = bytes.start();
long bcap = bytes.realCapacity();
long canWrite = bcap - (bytes.writePosition() - bstart);
long lengthCount = writeMessages(address, canWrite, writeCount);
bytes.writeSkip((int) lengthCount);
lastIndex += lengthCount >> 32;
count += lengthCount >> 32;
} else {
try (DocumentContext dc = appender.writingDocument()) {
dc.wire().bytes().write(nbs);
}
lastIndex = appender.lastIndexAppended();
count++;
}
} while (start + time * 1e9 > System.nanoTime());
}
nbs.releaseLast();
long mid = System.nanoTime();
long time1 = mid - start;
Bytes bytes = Bytes.allocateElasticDirect(64);
try (ChronicleQueue q = ChronicleQueue.singleBuilder(base).rollCycle(RollCycles.LARGE_HOURLY_XSPARSE).blockSize(blockSize).build()) {
ExcerptTailer tailer = q.createTailer();
for (long i = 0; i < count; i++) {
try (DocumentContext dc = tailer.readingDocument()) {
bytes.clear();
bytes.write(dc.wire().bytes());
}
}
}
bytes.releaseLast();
long end = System.nanoTime();
long time2 = end - mid;
System.out.printf("Writing %,d messages took %.3f seconds, at a rate of %,d per second%n", count, time1 / 1e9, (long) (1e9 * count / time1));
System.out.printf("Reading %,d messages took %.3f seconds, at a rate of %,d per second%n", count, time2 / 1e9, (long) (1e9 * count / time2));
// make sure its cleaned up for windows to delete.
System.gc();
IOTools.deleteDirWithFiles(base, 2);
}
use of net.openhft.chronicle.bytes.Bytes in project Chronicle-Queue by OpenHFT.
the class RawAccessJavaTest method Tailer.
@Test
public void Tailer() {
if (!assert_from_cpp())
return;
String tmp = "/dev/shm/RawAccessCtoJ";
// so C++ knows this ran rather than skipped
System.out.println(tmp);
try (ChronicleQueue cq = SingleChronicleQueueBuilder.binary(tmp).build()) {
ExcerptTailer tailer = cq.createTailer();
for (int i = 0; i < COUNT; ++i) {
try (DocumentContext dc = tailer.readingDocument()) {
Bytes bytes = dc.wire().bytes();
bytes.readSkip(-QUEUE_HEADER_SIZE);
int header = bytes.readInt();
// document length, inc 4-byte length
int length = Wires.lengthOf(header);
// actual length of data
int data_length = bytes.readInt();
assertEquals(bytes.readByte(), (byte) 0xab);
assertEquals(bytes.readShort(), (short) 12);
assertEquals(bytes.readInt(), 123);
assertEquals(bytes.readLong(), 123456789L);
assertEquals(bytes.readFloat(), 1.234f, 1.0e-7);
assertEquals(bytes.readDouble(), 123.456, 1.0e-7);
assertEquals(bytes.readChar(), 'a');
StringBuilder sb = new StringBuilder();
bytes.read8bit(sb);
assertEquals(sb.toString(), "Hello World");
}
}
}
}
use of net.openhft.chronicle.bytes.Bytes in project Chronicle-Queue by OpenHFT.
the class RawAccessJavaTest method Appender.
@Test
public void Appender() {
if (!assert_from_cpp())
return;
String tmp = "/dev/shm/RawAccessJtoC";
// so C++ knows this ran rather than skipped
System.out.println(tmp);
try (ChronicleQueue cq = SingleChronicleQueueBuilder.binary(tmp).build()) {
ExcerptAppender appender = cq.acquireAppender();
for (int i = 0; i < COUNT; ++i) {
try (DocumentContext dc = appender.writingDocument()) {
Bytes bytes = dc.wire().bytes();
// will contain the size of the blob
long start = bytes.writePosition();
bytes.writeSkip(RAW_SIZE_PREFIX);
{
bytes.writeByte((byte) 0xab);
bytes.writeShort((short) 12);
bytes.writeInt(123);
bytes.writeLong(123456789L);
bytes.writeFloat(1.234f);
bytes.writeDouble(123.456);
bytes.writeChar('a');
bytes.write8bit("Hello World");
}
long end = bytes.writePosition();
bytes.writeInt(start, (int) (end - start - RAW_SIZE_PREFIX));
}
}
}
}
use of net.openhft.chronicle.bytes.Bytes in project Chronicle-Queue by OpenHFT.
the class ThreadedQueueTest method testTailerReadingEmptyQueue.
@Test
public void testTailerReadingEmptyQueue() {
final File path = getTmpDir();
try (final ChronicleQueue rqueue = SingleChronicleQueueBuilder.fieldlessBinary(path).testBlockSize().rollCycle(TEST_DAILY).build()) {
final ExcerptTailer tailer = rqueue.createTailer();
try (final ChronicleQueue wqueue = SingleChronicleQueueBuilder.fieldlessBinary(path).testBlockSize().rollCycle(TEST_DAILY).build()) {
Bytes bytes = Bytes.elasticByteBuffer();
assertFalse(tailer.readBytes(bytes));
final ExcerptAppender appender = wqueue.acquireAppender();
appender.writeBytes(Bytes.wrapForRead("Hello World".getBytes(ISO_8859_1)));
bytes.clear();
boolean condition = tailer.readBytes(bytes);
// TODO FIX, Something in the cache for directory isn't being updated.
if (!condition) {
Jvm.pause(1);
condition = tailer.readBytes(bytes);
}
assertTrue(condition);
assertEquals("Hello World", bytes.toString());
bytes.releaseLast();
}
}
}
Aggregations