Search in sources :

Example 86 with AtomicLong

use of java.util.concurrent.atomic.AtomicLong in project deeplearning4j by deeplearning4j.

the class TextPipelineTest method testZipFunction2.

@Test
public void testZipFunction2() throws Exception {
    JavaSparkContext sc = getContext();
    JavaRDD<String> corpusRDD = getCorpusRDD(sc);
    //  word2vec.setRemoveStop(false);
    Broadcast<Map<String, Object>> broadcastTokenizerVarMap = sc.broadcast(word2vecNoStop.getTokenizerVarMap());
    TextPipeline pipeline = new TextPipeline(corpusRDD, broadcastTokenizerVarMap);
    pipeline.buildVocabCache();
    pipeline.buildVocabWordListRDD();
    JavaRDD<AtomicLong> sentenceCountRDD = pipeline.getSentenceCountRDD();
    JavaRDD<List<VocabWord>> vocabWordListRDD = pipeline.getVocabWordListRDD();
    CountCumSum countCumSum = new CountCumSum(sentenceCountRDD);
    JavaRDD<Long> sentenceCountCumSumRDD = countCumSum.buildCumSum();
    JavaPairRDD<List<VocabWord>, Long> vocabWordListSentenceCumSumRDD = vocabWordListRDD.zip(sentenceCountCumSumRDD);
    List<Tuple2<List<VocabWord>, Long>> lst = vocabWordListSentenceCumSumRDD.collect();
    List<VocabWord> vocabWordsList1 = lst.get(0)._1();
    Long cumSumSize1 = lst.get(0)._2();
    assertEquals(6, vocabWordsList1.size());
    assertEquals(vocabWordsList1.get(0).getWord(), "this");
    assertEquals(vocabWordsList1.get(1).getWord(), "is");
    assertEquals(vocabWordsList1.get(2).getWord(), "a");
    assertEquals(vocabWordsList1.get(3).getWord(), "strange");
    assertEquals(vocabWordsList1.get(4).getWord(), "strange");
    assertEquals(vocabWordsList1.get(5).getWord(), "world");
    assertEquals(cumSumSize1, 6L, 0);
    List<VocabWord> vocabWordsList2 = lst.get(1)._1();
    Long cumSumSize2 = lst.get(1)._2();
    assertEquals(vocabWordsList2.size(), 3);
    assertEquals(vocabWordsList2.get(0).getWord(), "flowers");
    assertEquals(vocabWordsList2.get(1).getWord(), "are");
    assertEquals(vocabWordsList2.get(2).getWord(), "red");
    assertEquals(cumSumSize2, 9L, 0);
    sc.stop();
}
Also used : VocabWord(org.deeplearning4j.models.word2vec.VocabWord) TextPipeline(org.deeplearning4j.spark.text.functions.TextPipeline) AtomicLong(java.util.concurrent.atomic.AtomicLong) Tuple2(scala.Tuple2) AtomicLong(java.util.concurrent.atomic.AtomicLong) CountCumSum(org.deeplearning4j.spark.text.functions.CountCumSum) JavaSparkContext(org.apache.spark.api.java.JavaSparkContext) Test(org.junit.Test)

Example 87 with AtomicLong

use of java.util.concurrent.atomic.AtomicLong in project deeplearning4j by deeplearning4j.

the class TextPipelineTest method testCountCumSum.

@Test
public void testCountCumSum() throws Exception {
    JavaSparkContext sc = getContext();
    JavaRDD<String> corpusRDD = getCorpusRDD(sc);
    Broadcast<Map<String, Object>> broadcastTokenizerVarMap = sc.broadcast(word2vec.getTokenizerVarMap());
    TextPipeline pipeline = new TextPipeline(corpusRDD, broadcastTokenizerVarMap);
    pipeline.buildVocabCache();
    pipeline.buildVocabWordListRDD();
    JavaRDD<AtomicLong> sentenceCountRDD = pipeline.getSentenceCountRDD();
    CountCumSum countCumSum = new CountCumSum(sentenceCountRDD);
    JavaRDD<Long> sentenceCountCumSumRDD = countCumSum.buildCumSum();
    List<Long> sentenceCountCumSumList = sentenceCountCumSumRDD.collect();
    assertTrue(sentenceCountCumSumList.get(0) == 6L);
    assertTrue(sentenceCountCumSumList.get(1) == 9L);
    sc.stop();
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) AtomicLong(java.util.concurrent.atomic.AtomicLong) CountCumSum(org.deeplearning4j.spark.text.functions.CountCumSum) JavaSparkContext(org.apache.spark.api.java.JavaSparkContext) TextPipeline(org.deeplearning4j.spark.text.functions.TextPipeline) Test(org.junit.Test)

Example 88 with AtomicLong

use of java.util.concurrent.atomic.AtomicLong in project druid by druid-io.

the class CountingMapTest method testSnapshot.

@Test
public void testSnapshot() {
    long defaultValue = 10;
    String defaultKey = "defaultKey";
    mapObject.add(defaultKey, defaultValue);
    ImmutableMap snapShotMap = (ImmutableMap) mapObject.snapshot();
    Assert.assertEquals("Maps size does not match", mapObject.size(), snapShotMap.size());
    long expected = (long) snapShotMap.get(defaultKey);
    AtomicLong actual = (AtomicLong) mapObject.get(defaultKey);
    Assert.assertEquals("Values for key = " + defaultKey + " does not match", actual.longValue(), expected);
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) ImmutableMap(com.google.common.collect.ImmutableMap) Test(org.junit.Test)

Example 89 with AtomicLong

use of java.util.concurrent.atomic.AtomicLong in project hadoop by apache.

the class TestInstrumentedReadWriteLock method testReadLockLongHoldingReport.

/**
   * Tests the warning when the read lock is held longer than threshold.
   * @throws Exception
   */
@Test(timeout = 10000)
public void testReadLockLongHoldingReport() throws Exception {
    String testname = name.getMethodName();
    final AtomicLong time = new AtomicLong(0);
    Timer mclock = new Timer() {

        @Override
        public long monotonicNow() {
            return time.get();
        }
    };
    final AtomicLong wlogged = new AtomicLong(0);
    final AtomicLong wsuppresed = new AtomicLong(0);
    ReentrantReadWriteLock readWriteLock = new ReentrantReadWriteLock(true);
    InstrumentedReadLock readLock = new InstrumentedReadLock(testname, LOG, readWriteLock, 2000, 300, mclock) {

        @Override
        protected void logWarning(long lockHeldTime, long suppressed) {
            wlogged.incrementAndGet();
            wsuppresed.set(suppressed);
        }
    };
    // t = 0
    readLock.lock();
    time.set(100);
    // t = 100
    readLock.unlock();
    assertEquals(0, wlogged.get());
    assertEquals(0, wsuppresed.get());
    // t = 100
    readLock.lock();
    time.set(500);
    // t = 500
    readLock.unlock();
    assertEquals(1, wlogged.get());
    assertEquals(0, wsuppresed.get());
    // the suppress counting is only changed when
    // log is needed in the test
    // t = 500
    readLock.lock();
    time.set(900);
    // t = 900
    readLock.unlock();
    assertEquals(1, wlogged.get());
    assertEquals(0, wsuppresed.get());
    // t = 900
    readLock.lock();
    time.set(3000);
    // t = 3000
    readLock.unlock();
    assertEquals(2, wlogged.get());
    assertEquals(1, wsuppresed.get());
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) Test(org.junit.Test)

Example 90 with AtomicLong

use of java.util.concurrent.atomic.AtomicLong in project hadoop by apache.

the class TestInstrumentedLock method testLockLongHoldingReport.

/**
   * Test the lock logs warning when lock held time is greater than threshold
   * and not log warning otherwise.
   * @throws Exception
   */
@Test(timeout = 10000)
public void testLockLongHoldingReport() throws Exception {
    String testname = name.getMethodName();
    final AtomicLong time = new AtomicLong(0);
    Timer mclock = new Timer() {

        @Override
        public long monotonicNow() {
            return time.get();
        }
    };
    Lock mlock = mock(Lock.class);
    final AtomicLong wlogged = new AtomicLong(0);
    final AtomicLong wsuppresed = new AtomicLong(0);
    InstrumentedLock lock = new InstrumentedLock(testname, LOG, mlock, 2000, 300, mclock) {

        @Override
        void logWarning(long lockHeldTime, long suppressed) {
            wlogged.incrementAndGet();
            wsuppresed.set(suppressed);
        }
    };
    // do not log warning when the lock held time is short
    // t = 0
    lock.lock();
    time.set(200);
    // t = 200
    lock.unlock();
    assertEquals(0, wlogged.get());
    assertEquals(0, wsuppresed.get());
    // t = 200
    lock.lock();
    time.set(700);
    // t = 700
    lock.unlock();
    assertEquals(1, wlogged.get());
    assertEquals(0, wsuppresed.get());
    // despite the lock held time is greater than threshold
    // suppress the log warning due to the logging gap
    // (not recorded in wsuppressed until next log message)
    // t = 700
    lock.lock();
    time.set(1100);
    // t = 1100
    lock.unlock();
    assertEquals(1, wlogged.get());
    assertEquals(0, wsuppresed.get());
    // log a warning message when the lock held time is greater the threshold
    // and the logging time gap is satisfied. Also should display suppressed
    // previous warnings.
    time.set(2400);
    // t = 2400
    lock.lock();
    time.set(2800);
    // t = 2800
    lock.unlock();
    assertEquals(2, wlogged.get());
    assertEquals(1, wsuppresed.get());
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) Lock(java.util.concurrent.locks.Lock) Test(org.junit.Test)

Aggregations

AtomicLong (java.util.concurrent.atomic.AtomicLong)2292 Test (org.junit.Test)986 ArrayList (java.util.ArrayList)300 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)273 IOException (java.io.IOException)254 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)250 List (java.util.List)222 HashMap (java.util.HashMap)212 Map (java.util.Map)209 CountDownLatch (java.util.concurrent.CountDownLatch)185 AtomicReference (java.util.concurrent.atomic.AtomicReference)174 HashSet (java.util.HashSet)106 Arrays (java.util.Arrays)101 File (java.io.File)99 Test (org.junit.jupiter.api.Test)98 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)95 Set (java.util.Set)94 TimeUnit (java.util.concurrent.TimeUnit)91 Collections (java.util.Collections)88 Random (java.util.Random)85