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