|
| 1 | +package org.dataloader; |
| 2 | + |
| 3 | +import org.openjdk.jcstress.annotations.Actor; |
| 4 | +import org.openjdk.jcstress.annotations.Arbiter; |
| 5 | +import org.openjdk.jcstress.annotations.JCStressTest; |
| 6 | +import org.openjdk.jcstress.annotations.Outcome; |
| 7 | +import org.openjdk.jcstress.annotations.State; |
| 8 | +import org.openjdk.jcstress.infra.results.II_Result; |
| 9 | + |
| 10 | +import java.util.List; |
| 11 | +import java.util.concurrent.CompletableFuture; |
| 12 | +import java.util.concurrent.atomic.AtomicInteger; |
| 13 | + |
| 14 | +import static org.openjdk.jcstress.annotations.Expect.ACCEPTABLE; |
| 15 | + |
| 16 | +@JCStressTest |
| 17 | +@State |
| 18 | +@Outcome(id = "2000, 2000", expect = ACCEPTABLE, desc = "accepted") |
| 19 | +public class DataLoaderDispatchJCStress { |
| 20 | + |
| 21 | + |
| 22 | + AtomicInteger counter = new AtomicInteger(); |
| 23 | + AtomicInteger batchLoaderCount = new AtomicInteger(); |
| 24 | + volatile boolean finished1; |
| 25 | + volatile boolean finished2; |
| 26 | + |
| 27 | + |
| 28 | + BatchLoader<String, String> batchLoader = keys -> { |
| 29 | + return CompletableFuture.supplyAsync(() -> { |
| 30 | + batchLoaderCount.getAndAdd(keys.size()); |
| 31 | + return keys; |
| 32 | + }); |
| 33 | + }; |
| 34 | + DataLoader<String, String> dataLoader = DataLoaderFactory.newDataLoader(batchLoader); |
| 35 | + |
| 36 | + public DataLoaderDispatchJCStress() { |
| 37 | + |
| 38 | + } |
| 39 | + |
| 40 | + @Actor |
| 41 | + public void load1() { |
| 42 | + for (int i = 0; i < 1000; i++) { |
| 43 | + dataLoader.load("load-1-" + i); |
| 44 | + } |
| 45 | + finished1 = true; |
| 46 | + } |
| 47 | + |
| 48 | + @Actor |
| 49 | + public void load2() { |
| 50 | + for (int i = 0; i < 1000; i++) { |
| 51 | + dataLoader.load("load-2-" + i); |
| 52 | + } |
| 53 | + finished2 = true; |
| 54 | + } |
| 55 | + |
| 56 | + |
| 57 | + @Actor |
| 58 | + public void dispatch1() { |
| 59 | + while (!finished1 || !finished2) { |
| 60 | + try { |
| 61 | + List<String> dispatchedResult = dataLoader.dispatch().get(); |
| 62 | + counter.getAndAdd(dispatchedResult.size()); |
| 63 | + } catch (Exception e) { |
| 64 | + throw new RuntimeException(e); |
| 65 | + } |
| 66 | + } |
| 67 | + try { |
| 68 | + List<String> dispatchedResult = dataLoader.dispatch().get(); |
| 69 | + counter.getAndAdd(dispatchedResult.size()); |
| 70 | + } catch (Exception e) { |
| 71 | + throw new RuntimeException(e); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + @Actor |
| 76 | + public void dispatch2() { |
| 77 | + while (!finished1 || !finished2) { |
| 78 | + try { |
| 79 | + List<String> dispatchedResult = dataLoader.dispatch().get(); |
| 80 | + counter.getAndAdd(dispatchedResult.size()); |
| 81 | + } catch (Exception e) { |
| 82 | + throw new RuntimeException(e); |
| 83 | + } |
| 84 | + } |
| 85 | + try { |
| 86 | + List<String> dispatchedResult = dataLoader.dispatch().get(); |
| 87 | + counter.getAndAdd(dispatchedResult.size()); |
| 88 | + } catch (Exception e) { |
| 89 | + throw new RuntimeException(e); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + @Arbiter |
| 94 | + public void arbiter(II_Result r) { |
| 95 | + r.r1 = counter.get(); |
| 96 | + r.r2 = batchLoaderCount.get(); |
| 97 | + } |
| 98 | + |
| 99 | + |
| 100 | +} |
0 commit comments