1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
| package org.dragon.singleton;
import lombok.extern.slf4j.Slf4j;
import java.util.*; import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; import java.util.function.Supplier;
@Slf4j public class SingletonPerformanceTest { static long timeout = 20; // 超时时间,单位为秒 static int testIterations = 10_000_000; // 测试次数 static int threadCount = 1000; // 并发线程数 static Map<String, HashMap<String, Long>> result = new HashMap<>();
public static void main(String[] args) { /* * 多次调用,结果是最后一次调用存入。为什么多次调用,因为单次test不准确,总是靠前的OrderManager跑的快,可能是因为Java某些机制导致的 * 所以多次调用,逐渐平稳。 * */ firstCreationTest(); mulAccessTest(); mulAccessTest(); mulAccessTest(); ConcurrentAccessTest(); ConcurrentAccessTest(); printRes(); ConcurrentAccessTest(); printRes(); ConcurrentAccessTest(); printRes(); }
/** * 打印结果 */ private static void printRes(){ ArrayList<String> names = new ArrayList<>(); names.add(OrderManager1.class.getSimpleName()); names.add(OrderManager2.class.getSimpleName()); names.add(OrderManager3.class.getSimpleName()); names.add(OrderManager4.class.getSimpleName()); names.add(OrderManager5.class.getSimpleName()); // 表头 System.out.printf("%-20s%-20s%-25s%-25s%-20s%n", "Singleton Type", "First Creation (ms)", "Multiple Access (ms)", "Concurrent Access (ms)", "Memory Used (MB)"); System.out.println("---------------------------------------------------------------------------------------------------------------");
for (String name : names) { // 打印结果,转换时间为毫秒 System.out.printf("%-20s%-20.3f%-25.3f%-25.3f%-20.3f%n", name, result.get(name).get("firstCreation") / 1_000_000.0, result.get(name).get("mulAccess") / 1_000_000.0, result.get(name).get("ConcurrentAccess") / 1_000_000.0, 0 / (1024.0 * 1024.0)); } }
/** * 首次创建测试 */ private static void firstCreationTest(){ List<Runnable> tests = new ArrayList<>(); tests.add(()->firstCreation(OrderManager1::getInstance)); tests.add(()->firstCreation(OrderManager2::getInstance)); tests.add(()->firstCreation(OrderManager3::getInstance)); tests.add(()->firstCreation(OrderManager4::getInstance)); tests.add(()->firstCreation(() -> OrderManager5.INSTANCE)); // 随机化测试顺序 Collections.shuffle(tests); //run for (Runnable test : tests) { test.run(); log.info("Complete one test"); try { Thread.sleep(200); } catch (InterruptedException e) { throw new RuntimeException(e); } } }
/** * 多次访问测试 */ private static void mulAccessTest(){ List<Runnable> tests = new ArrayList<>(); tests.add(()->mulAccess(OrderManager1::getInstance, testIterations)); tests.add(()->mulAccess(OrderManager2::getInstance, testIterations)); tests.add(()->mulAccess(OrderManager3::getInstance, testIterations)); tests.add(()->mulAccess(OrderManager4::getInstance, testIterations)); tests.add(()->mulAccess(() -> OrderManager5.INSTANCE, testIterations)); // 随机化测试顺序 Collections.shuffle(tests); //run for (Runnable test : tests) { test.run(); log.info("Complete one test"); try { Thread.sleep(200); } catch (InterruptedException e) { throw new RuntimeException(e); } } }
/** * 多线程访问测试 */ private static void ConcurrentAccessTest(){ List<Runnable> tests = new ArrayList<>(); tests.add(()->ConcurrentAccess(OrderManager1::getInstance, testIterations, threadCount)); tests.add(()->ConcurrentAccess(OrderManager2::getInstance, testIterations, threadCount)); tests.add(()->ConcurrentAccess(OrderManager3::getInstance, testIterations, threadCount)); tests.add(()->ConcurrentAccess(OrderManager4::getInstance, testIterations, threadCount)); tests.add(()->ConcurrentAccess(() -> OrderManager5.INSTANCE, testIterations, threadCount)); // 随机化测试顺序 Collections.shuffle(tests); //run for (Runnable test : tests) { test.run(); log.info("Complete one test"); try { Thread.sleep(200); } catch (InterruptedException e) { throw new RuntimeException(e); } } }
/** * 首次创建 * * @param singletonSupplier 单一供应商 * @return long ns */ private static <T> long firstCreation(Supplier<T> singletonSupplier){ // 测试首次创建时间 long startTime = System.nanoTime(); T instance = singletonSupplier.get(); long endTime = System.nanoTime(); long resTime = endTime - startTime; //save res String simpleName = instance.getClass().getSimpleName(); HashMap<String, Long> resMap = result.computeIfAbsent(simpleName, k->new HashMap<>()); resMap.put("firstCreation", resTime); return resTime; }
/** * 多次访问 * * @param singletonSupplier 单一供应商 * @param iterations 迭代 * @return long ns */ private static <T> long mulAccess(Supplier<T> singletonSupplier, int iterations){ //预热 for (int i = 0; i < 100_000; i++) { T instance = singletonSupplier.get(); } //计算 long startTime = System.nanoTime(); for (int i = 0; i < iterations; i++) { T instance = singletonSupplier.get(); } long endTime = System.nanoTime(); long resTime = endTime - startTime; //save res String simpleName = singletonSupplier.get().getClass().getSimpleName(); HashMap<String, Long> resMap = result.computeIfAbsent(simpleName, k->new HashMap<>()); resMap.put("mulAccess", resTime); return resTime; }
/** * 并发访问 * * @param singletonSupplier 单一供应商 * @param iterations 迭代 * @param threadCount 线程数 * @return long ns */ private static <T> long ConcurrentAccess(Supplier<T> singletonSupplier, int iterations, int threadCount){ ExecutorService executorService = Executors.newFixedThreadPool(100); //预热 CountDownLatch latch1 = new CountDownLatch(100); for (int i = 0; i < threadCount; i++) { executorService.submit(() -> { for (int j = 0; j < 100_000; j++) { T instance = singletonSupplier.get(); } latch1.countDown(); }); } try { boolean completed = latch1.await(timeout, TimeUnit.SECONDS); if (!completed) { System.out.println("Concurrent access test for 预热" + singletonSupplier.get().getClass().getSimpleName() + " timed out!"); } } catch (InterruptedException e) { e.printStackTrace(); } //计算 CountDownLatch latch2 = new CountDownLatch(threadCount); long startTime = System.nanoTime(); for (int i = 0; i < threadCount; i++) { executorService.submit(() -> { for (int j = 0; j < iterations; j++) { T instance = singletonSupplier.get(); } latch2.countDown(); }); } try { boolean completed = latch2.await(timeout, TimeUnit.SECONDS); if (!completed) { System.out.println("Concurrent access test for " + singletonSupplier.getClass().getSimpleName() + " timed out!"); } } catch (InterruptedException e) { e.printStackTrace(); } long endTime = System.nanoTime(); long concurrentAccessTime = endTime - startTime;
executorService.shutdown(); //save res String simpleName = singletonSupplier.get().getClass().getSimpleName(); HashMap<String, Long> resMap = result.computeIfAbsent(simpleName, k->new HashMap<>()); resMap.put("ConcurrentAccess", concurrentAccessTime); return concurrentAccessTime; } }
|