Search in sources :

Example 1 with CountUpLatch

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());
}
Also used : Path(java.nio.file.Path) AtomicDouble(com.google.common.util.concurrent.AtomicDouble) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ByteBuffer(java.nio.ByteBuffer) CountUpLatch(com.cinchapi.common.concurrent.CountUpLatch) Value(com.cinchapi.concourse.server.model.Value) Benchmark(com.cinchapi.common.profile.Benchmark) ConcourseBaseTest(com.cinchapi.concourse.test.ConcourseBaseTest) Test(org.junit.Test)

Example 2 with CountUpLatch

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();
    }
}
Also used : CountUpLatch(com.cinchapi.common.concurrent.CountUpLatch)

Aggregations

CountUpLatch (com.cinchapi.common.concurrent.CountUpLatch)2 Benchmark (com.cinchapi.common.profile.Benchmark)1 Value (com.cinchapi.concourse.server.model.Value)1 ConcourseBaseTest (com.cinchapi.concourse.test.ConcourseBaseTest)1 AtomicDouble (com.google.common.util.concurrent.AtomicDouble)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 ByteBuffer (java.nio.ByteBuffer)1 Path (java.nio.file.Path)1 Test (org.junit.Test)1