|
| 1 | +package com.komanov.redis.perf; |
| 2 | + |
| 3 | +import com.komanov.redis.StringUuidCodec$; |
| 4 | +import io.lettuce.core.RedisClient; |
| 5 | +import io.lettuce.core.api.StatefulRedisConnection; |
| 6 | +import io.lettuce.core.api.async.RedisAsyncCommands; |
| 7 | + |
| 8 | +import java.nio.file.Files; |
| 9 | +import java.nio.file.Paths; |
| 10 | +import java.util.ArrayList; |
| 11 | +import java.util.Arrays; |
| 12 | +import java.util.List; |
| 13 | +import java.util.UUID; |
| 14 | +import java.util.concurrent.CountDownLatch; |
| 15 | +import java.util.concurrent.LinkedBlockingDeque; |
| 16 | +import java.util.stream.Collectors; |
| 17 | + |
| 18 | +public class PerfTester { |
| 19 | + private static class InFlight { |
| 20 | + public final int index; |
| 21 | + public long startTime; |
| 22 | + |
| 23 | + public InFlight(int index) { |
| 24 | + this.index = index; |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + public static void main(String[] args) throws Throwable { |
| 29 | + assert args.length == 2; |
| 30 | + |
| 31 | + String setName = args[0]; |
| 32 | + List<UUID> uuids = Files.readAllLines(Paths.get(args[1])) |
| 33 | + .stream() |
| 34 | + .map(UUID::fromString) |
| 35 | + .limit(1_000_000) |
| 36 | + .collect(Collectors.toList()); |
| 37 | + |
| 38 | + RedisClient client = RedisClient.create("redis://localhost:6379"); |
| 39 | + StatefulRedisConnection<String, UUID> connection = client.connect(StringUuidCodec$.MODULE$); |
| 40 | + RedisAsyncCommands<String, UUID> async = connection.async(); |
| 41 | + |
| 42 | + System.out.println("About to run warmup"); |
| 43 | + int warmupCount = 10_000; |
| 44 | + CountDownLatch latch = new CountDownLatch(warmupCount); |
| 45 | + uuids.stream().limit(warmupCount).forEach(v -> { |
| 46 | + async.sismember(setName, v).handle((r, e) -> { |
| 47 | + latch.countDown(); |
| 48 | + return r; |
| 49 | + }); |
| 50 | + }); |
| 51 | + System.out.println("Awaiting for warmup to complete"); |
| 52 | + latch.await(); |
| 53 | + System.out.println("Warmup completed!"); |
| 54 | + |
| 55 | + ArrayList<ArrayList<String>> results = new ArrayList<>(); |
| 56 | + for (int parallelism = 0; parallelism <= 90; parallelism += 10) { |
| 57 | + results.add(runBenchmark(async, uuids, setName, parallelism == 0 ? 1 : parallelism)); |
| 58 | + } |
| 59 | + for (int parallelism = 100; parallelism <= 1000; parallelism += 100) { |
| 60 | + results.add(runBenchmark(async, uuids, setName, parallelism)); |
| 61 | + } |
| 62 | + results.add(runBenchmark(async, uuids, setName, 2000)); |
| 63 | + |
| 64 | + for (int num = 0; num < results.get(0).size(); ++num) { |
| 65 | + for (int i = 0; i < results.size(); ++i) { |
| 66 | + ArrayList<String> single = results.get(i); |
| 67 | + System.out.print(single.get(num) + "\t"); |
| 68 | + } |
| 69 | + System.out.println(); |
| 70 | + } |
| 71 | + |
| 72 | + connection.close(); |
| 73 | + client.shutdown(); |
| 74 | + } |
| 75 | + |
| 76 | + private static ArrayList<String> runBenchmark(RedisAsyncCommands<String, UUID> async, List<UUID> uuids, String setName, int parallelism) |
| 77 | + throws Throwable { |
| 78 | + long[] durations = new long[uuids.size()]; |
| 79 | + LinkedBlockingDeque<InFlight> queue = new LinkedBlockingDeque<>(parallelism); |
| 80 | + long overallStartTime = System.nanoTime(); |
| 81 | + for (int i = 0; i < uuids.size(); i++) { |
| 82 | + final InFlight item = new InFlight(i); |
| 83 | + queue.put(item); |
| 84 | + item.startTime = System.nanoTime(); |
| 85 | + async |
| 86 | + .sismember(setName, uuids.get(i)) |
| 87 | + .handle((isMember, e) -> { |
| 88 | + if (e == null) { |
| 89 | + if (!isMember) { |
| 90 | + System.out.println("NOT MEMBER!"); |
| 91 | + System.exit(1); |
| 92 | + } |
| 93 | + durations[item.index] = System.nanoTime() - item.startTime; |
| 94 | + if (!queue.remove(item)) { |
| 95 | + System.out.println("NOT IN QUEUE!"); |
| 96 | + System.exit(2); |
| 97 | + } |
| 98 | + } else { |
| 99 | + e.printStackTrace(); |
| 100 | + System.exit(1); |
| 101 | + } |
| 102 | + return Boolean.TRUE; |
| 103 | + }) |
| 104 | + ; |
| 105 | + } |
| 106 | + |
| 107 | + while (!queue.isEmpty()) { |
| 108 | + Thread.sleep(0); |
| 109 | + } |
| 110 | + |
| 111 | + long overallDuration = System.nanoTime() - overallStartTime; |
| 112 | + |
| 113 | + ArrayList<String> results = new ArrayList<>(); |
| 114 | + results.add("" + uuids.size()); |
| 115 | + results.add("" + parallelism); |
| 116 | + results.add("" + overallDuration); |
| 117 | + results.add("" + overallDuration / uuids.size()); |
| 118 | + |
| 119 | + Arrays.sort(durations); |
| 120 | + Arrays.stream(durations).limit(5).forEach(d -> { |
| 121 | + results.add("" + d); |
| 122 | + }); |
| 123 | + Arrays.stream(durations).skip(durations.length / 2).limit(5).forEach(d -> { |
| 124 | + results.add("" + d); |
| 125 | + }); |
| 126 | + Arrays.stream(durations).skip(durations.length - 5).forEach(d -> { |
| 127 | + results.add("" + d); |
| 128 | + }); |
| 129 | + return results; |
| 130 | + } |
| 131 | +} |
0 commit comments