Yuta HIGUCHI | d47eac3 | 2014-04-07 13:44:47 -0700 | [diff] [blame] | 1 | package net.onrc.onos.core.datastore; |
| 2 | |
Yuta HIGUCHI | 44a0b35 | 2014-05-14 21:32:48 -0700 | [diff] [blame] | 3 | import static org.junit.Assert.assertEquals; |
| 4 | import static org.junit.Assert.assertNull; |
Yuta HIGUCHI | d47eac3 | 2014-04-07 13:44:47 -0700 | [diff] [blame] | 5 | |
| 6 | import java.util.ArrayList; |
| 7 | import java.util.List; |
Yuta HIGUCHI | 3e7994c | 2014-05-12 21:01:33 -0700 | [diff] [blame] | 8 | import java.util.UUID; |
Yuta HIGUCHI | d47eac3 | 2014-04-07 13:44:47 -0700 | [diff] [blame] | 9 | import java.util.concurrent.Callable; |
| 10 | import java.util.concurrent.ConcurrentLinkedQueue; |
| 11 | import java.util.concurrent.ConcurrentMap; |
| 12 | import java.util.concurrent.ConcurrentNavigableMap; |
| 13 | import java.util.concurrent.ConcurrentSkipListMap; |
| 14 | import java.util.concurrent.ExecutionException; |
| 15 | import java.util.concurrent.ExecutorService; |
| 16 | import java.util.concurrent.Executors; |
| 17 | import java.util.concurrent.Future; |
| 18 | |
Yuta HIGUCHI | d47eac3 | 2014-04-07 13:44:47 -0700 | [diff] [blame] | 19 | import org.junit.After; |
| 20 | import org.junit.Before; |
| 21 | import org.junit.Test; |
| 22 | |
| 23 | public class AtomicCounterTest { |
| 24 | |
Yuta HIGUCHI | ceb21b6 | 2014-04-17 15:46:05 -0700 | [diff] [blame] | 25 | static { |
| 26 | // configuration to quickly fall back to instance mode for faster test run |
| 27 | System.setProperty("net.onrc.onos.core.datastore.hazelcast.client.attemptLimit", "0"); |
| 28 | } |
| 29 | |
Yuta HIGUCHI | 3e7994c | 2014-05-12 21:01:33 -0700 | [diff] [blame] | 30 | private static final String TEST_COUNTER = "TestCounter" + UUID.randomUUID(); |
Yuta HIGUCHI | d47eac3 | 2014-04-07 13:44:47 -0700 | [diff] [blame] | 31 | private static final byte[] LONG_ZERO = {0, 0, 0, 0, 0, 0, 0, 0}; // 0L |
Yuta HIGUCHI | ceb21b6 | 2014-04-17 15:46:05 -0700 | [diff] [blame] | 32 | private IKVTableID counterID; |
Yuta HIGUCHI | d47eac3 | 2014-04-07 13:44:47 -0700 | [diff] [blame] | 33 | |
Yuta HIGUCHI | d47eac3 | 2014-04-07 13:44:47 -0700 | [diff] [blame] | 34 | @Before |
Yuta HIGUCHI | ceb21b6 | 2014-04-17 15:46:05 -0700 | [diff] [blame] | 35 | @After |
Yuta HIGUCHI | d47eac3 | 2014-04-07 13:44:47 -0700 | [diff] [blame] | 36 | public void resetCounter() { |
| 37 | IKVClient client = DataStoreClient.getClient(); |
Yuta HIGUCHI | ceb21b6 | 2014-04-17 15:46:05 -0700 | [diff] [blame] | 38 | counterID = client.getTable(TEST_COUNTER).getTableId(); |
Yuta HIGUCHI | d47eac3 | 2014-04-07 13:44:47 -0700 | [diff] [blame] | 39 | client.setCounter(counterID, LONG_ZERO, 0L); |
| 40 | client.destroyCounter(counterID, LONG_ZERO); |
Yuta HIGUCHI | c955262 | 2014-04-24 11:01:48 -0700 | [diff] [blame] | 41 | counterID = client.getTable(TEST_COUNTER).getTableId(); |
Yuta HIGUCHI | d47eac3 | 2014-04-07 13:44:47 -0700 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | @Test |
| 45 | public void testSetCounter() throws ObjectExistsException, ObjectDoesntExistException { |
| 46 | IKVClient client = DataStoreClient.getClient(); |
| 47 | |
| 48 | final long five = 5; |
| 49 | client.createCounter(counterID, LONG_ZERO, five); |
| 50 | |
| 51 | final long three = 3; |
| 52 | client.setCounter(counterID, LONG_ZERO, three); |
| 53 | |
| 54 | final long four = client.incrementCounter(counterID, LONG_ZERO, 1); |
| 55 | assertEquals(4, four); |
| 56 | } |
| 57 | |
| 58 | @Test |
| 59 | public void testIncrementCounter() throws ObjectExistsException, ObjectDoesntExistException { |
| 60 | |
| 61 | IKVClient client = DataStoreClient.getClient(); |
| 62 | |
| 63 | final long five = 5; |
| 64 | client.createCounter(counterID, LONG_ZERO, five); |
| 65 | |
| 66 | final long six = client.incrementCounter(counterID, LONG_ZERO, 1); |
| 67 | assertEquals(6, six); |
| 68 | |
| 69 | |
| 70 | final long nine = client.incrementCounter(counterID, LONG_ZERO, 3); |
| 71 | assertEquals(9, nine); |
| 72 | } |
| 73 | |
| 74 | |
Yuta HIGUCHI | 6920035 | 2014-04-11 12:32:44 -0700 | [diff] [blame] | 75 | private static final int NUM_INCREMENTS = Math.max(1, Integer |
| 76 | .valueOf(System.getProperty("AtomicCounterTest.NUM_INCREMENTS", |
| 77 | "500"))); |
| 78 | private static final int NUM_THREADS = Math.max(1, Integer.valueOf(System |
| 79 | .getProperty("AtomicCounterTest.NUM_THREADS", "3"))); |
Yuta HIGUCHI | d47eac3 | 2014-04-07 13:44:47 -0700 | [diff] [blame] | 80 | |
| 81 | class Incrementor implements Callable<Long> { |
Yuta HIGUCHI | 44a0b35 | 2014-05-14 21:32:48 -0700 | [diff] [blame] | 82 | private final ConcurrentMap<Long, Long> uniquenessTestSet; |
Yuta HIGUCHI | d47eac3 | 2014-04-07 13:44:47 -0700 | [diff] [blame] | 83 | private final ConcurrentLinkedQueue<Long> incrementTimes; |
| 84 | |
| 85 | public Incrementor(ConcurrentMap<Long, Long> uniquenessTestSet, ConcurrentLinkedQueue<Long> incrementTimes) { |
| 86 | super(); |
| 87 | this.uniquenessTestSet = uniquenessTestSet; |
| 88 | this.incrementTimes = incrementTimes; |
| 89 | } |
| 90 | |
| 91 | @Override |
| 92 | public Long call() throws ObjectDoesntExistException { |
| 93 | IKVClient client = DataStoreClient.getClient(); |
Yuta HIGUCHI | 44a0b35 | 2014-05-14 21:32:48 -0700 | [diff] [blame] | 94 | for (int i = 0; i < NUM_INCREMENTS; ++i) { |
Yuta HIGUCHI | d47eac3 | 2014-04-07 13:44:47 -0700 | [diff] [blame] | 95 | final long start = System.nanoTime(); |
| 96 | final long incremented = client.incrementCounter(counterID, LONG_ZERO, 1); |
Yuta HIGUCHI | 44a0b35 | 2014-05-14 21:32:48 -0700 | [diff] [blame] | 97 | incrementTimes.add(System.nanoTime() - start); |
Yuta HIGUCHI | d47eac3 | 2014-04-07 13:44:47 -0700 | [diff] [blame] | 98 | final Long expectNull = uniquenessTestSet.putIfAbsent(incremented, incremented); |
| 99 | assertNull(expectNull); |
| 100 | } |
| 101 | return null; |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | @Test |
Yuta HIGUCHI | 6920035 | 2014-04-11 12:32:44 -0700 | [diff] [blame] | 106 | public void testParallelIncrementCounter() throws ObjectExistsException, |
| 107 | InterruptedException, ExecutionException { |
| 108 | |
Yuta HIGUCHI | d47eac3 | 2014-04-07 13:44:47 -0700 | [diff] [blame] | 109 | IKVClient client = DataStoreClient.getClient(); |
| 110 | |
| 111 | client.createCounter(counterID, LONG_ZERO, 0L); |
| 112 | |
Yuta HIGUCHI | 6920035 | 2014-04-11 12:32:44 -0700 | [diff] [blame] | 113 | ExecutorService executor = Executors.newFixedThreadPool(NUM_THREADS); |
| 114 | final int initThreads = Math.max(1, Integer.valueOf(System |
| 115 | .getProperty("AtomicCounterTest.initThreads", |
| 116 | String.valueOf(NUM_THREADS)))); |
Yuta HIGUCHI | 44a0b35 | 2014-05-14 21:32:48 -0700 | [diff] [blame] | 117 | for (int numThreads = initThreads; numThreads <= NUM_THREADS; ++numThreads) { |
Yuta HIGUCHI | 6920035 | 2014-04-11 12:32:44 -0700 | [diff] [blame] | 118 | client.setCounter(counterID, LONG_ZERO, 0L); |
Yuta HIGUCHI | 44a0b35 | 2014-05-14 21:32:48 -0700 | [diff] [blame] | 119 | parallelIncrementCounter(executor, numThreads); |
Yuta HIGUCHI | 6920035 | 2014-04-11 12:32:44 -0700 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | executor.shutdown(); |
| 123 | } |
| 124 | |
| 125 | private void parallelIncrementCounter(final ExecutorService executor, |
Yuta HIGUCHI | 44a0b35 | 2014-05-14 21:32:48 -0700 | [diff] [blame] | 126 | final int numThreads) throws InterruptedException, ExecutionException { |
Yuta HIGUCHI | 6920035 | 2014-04-11 12:32:44 -0700 | [diff] [blame] | 127 | |
Yuta HIGUCHI | 44a0b35 | 2014-05-14 21:32:48 -0700 | [diff] [blame] | 128 | ConcurrentNavigableMap<Long, Long> uniquenessTestSet = new ConcurrentSkipListMap<>(); |
Yuta HIGUCHI | d47eac3 | 2014-04-07 13:44:47 -0700 | [diff] [blame] | 129 | ConcurrentLinkedQueue<Long> incrementTimes = new ConcurrentLinkedQueue<Long>(); |
| 130 | |
Yuta HIGUCHI | 44a0b35 | 2014-05-14 21:32:48 -0700 | [diff] [blame] | 131 | List<Callable<Long>> tasks = new ArrayList<>(numThreads); |
| 132 | for (int i = 0; i < numThreads; ++i) { |
Yuta HIGUCHI | d47eac3 | 2014-04-07 13:44:47 -0700 | [diff] [blame] | 133 | tasks.add(new Incrementor(uniquenessTestSet, incrementTimes)); |
| 134 | } |
| 135 | List<Future<Long>> futures = executor.invokeAll(tasks); |
| 136 | |
| 137 | // wait for all tasks to end |
| 138 | for (Future<Long> future : futures) { |
| 139 | future.get(); |
| 140 | } |
| 141 | |
Yuta HIGUCHI | 44a0b35 | 2014-05-14 21:32:48 -0700 | [diff] [blame] | 142 | assertEquals(numThreads * NUM_INCREMENTS , uniquenessTestSet.size()); |
Yuta HIGUCHI | d47eac3 | 2014-04-07 13:44:47 -0700 | [diff] [blame] | 143 | long prevValue = 0; |
Yuta HIGUCHI | 44a0b35 | 2014-05-14 21:32:48 -0700 | [diff] [blame] | 144 | for (Long value : uniquenessTestSet.keySet()) { |
| 145 | assertEquals((prevValue + 1), value.longValue()); |
Yuta HIGUCHI | d47eac3 | 2014-04-07 13:44:47 -0700 | [diff] [blame] | 146 | prevValue = value; |
| 147 | } |
| 148 | |
| 149 | long max = 0L; |
| 150 | long min = Long.MAX_VALUE; |
| 151 | long sum = 0L; |
| 152 | for (Long time : incrementTimes) { |
| 153 | sum += time; |
| 154 | max = Math.max(max, time); |
| 155 | min = Math.min(min, time); |
| 156 | } |
Yuta HIGUCHI | c7b72c3 | 2014-04-17 19:49:18 -0700 | [diff] [blame] | 157 | System.err.printf("incrementCounter: th, incs, N, avg(ns), min(ns), max(ns)\n"); |
| 158 | System.err.printf("incrementCounter: %d, %d, %d, %f, %d, %d\n", |
Yuta HIGUCHI | 44a0b35 | 2014-05-14 21:32:48 -0700 | [diff] [blame] | 159 | numThreads, NUM_INCREMENTS, incrementTimes.size(), |
| 160 | sum / (double) incrementTimes.size(), min, max); |
Yuta HIGUCHI | d47eac3 | 2014-04-07 13:44:47 -0700 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | } |