use of com.cinchapi.common.concurrent.CountUpLatch in project concourse by cinchapi.
the class ByteableCollectionsTest method testNewVsDeprecatedPerformance.
@Test
public void testNewVsDeprecatedPerformance() throws IOException, InterruptedException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
List<Value> values = Lists.newArrayList();
while (baos.size() < Math.pow(2, 24)) {
Value value = TestData.getValue();
baos.write(ByteBuffers.getByteArray(value.getBytes()));
values.add(value);
}
ByteBuffer bytes = ByteableCollections.toByteBuffer(values);
Path file = Paths.get(TestData.getTemporaryTestFile());
String $file = file.toString();
FileSystem.writeBytes(bytes, $file);
int bufferSize = 8192;
Benchmark benchmark1 = new Benchmark(TimeUnit.MILLISECONDS) {
@Override
public void action() {
CloseableIterator<ByteBuffer> it = ByteableCollections.stream(file, bufferSize);
while (it.hasNext()) {
Value.fromByteBuffer(it.next());
}
it.closeQuietly();
}
};
Benchmark benchmark2 = new Benchmark(TimeUnit.MILLISECONDS) {
@SuppressWarnings("deprecation")
@Override
public void action() {
Iterator<ByteBuffer> it = ByteableCollections.streamingIterator($file, bufferSize);
while (it.hasNext()) {
Value.fromByteBuffer(it.next());
}
}
};
AtomicDouble avg1 = new AtomicDouble();
AtomicDouble avg2 = new AtomicDouble();
CountUpLatch latch = new CountUpLatch();
Thread t1 = new Thread(() -> {
double avg = benchmark1.average(10);
System.out.println("New: " + avg);
avg1.set(avg);
latch.countUp();
});
Thread t2 = new Thread(() -> {
double avg = benchmark2.average(10);
System.out.println("Deprecated: " + avg);
avg2.set(avg);
latch.countUp();
});
List<Thread> threads = Lists.newArrayList(t1, t2);
Collections.shuffle(threads);
for (Thread thread : threads) {
thread.start();
}
latch.await(2);
Assert.assertTrue(avg1.get() < avg2.get());
}
use of com.cinchapi.common.concurrent.CountUpLatch in project concourse by cinchapi.
the class CorpusChunk method insert.
/**
* Insert a revision for {@code key} as {@code value} in {@code record} at
* {@code version}
*
* @param key
* @param value
* @param record
* @param version
* @param type
*/
public final Collection<CorpusArtifact> insert(Text key, Value value, Identifier record, long version, Action type) {
Preconditions.checkState(isMutable(), "Cannot modify a chunk that is immutable");
if (value.getType() == Type.STRING) {
write.lock();
try {
// CON-10
String string = value.getObject().toString().toLowerCase();
String[] toks = string.split(TStrings.REGEX_GROUP_OF_ONE_OR_MORE_WHITESPACE_CHARS);
Collection<CorpusArtifact> artifacts = TRACK_ARTIFACTS ? new ConcurrentLinkedQueue<>() : new NoOpList<>();
CountUpLatch tracker = new CountUpLatch();
int pos = 0;
int numPrepared = 0;
for (String tok : toks) {
numPrepared += prepare(tracker, key, tok, record, pos, version, type, artifacts);
++pos;
}
try {
tracker.await(numPrepared);
} catch (InterruptedException e) {
throw CheckedExceptions.wrapAsRuntimeException(e);
}
return artifacts;
} finally {
write.unlock();
}
} else {
return ImmutableList.of();
}
}
Aggregations