UUID { public String nextID() { return instanceId + "-" + StringUtil.longToString(System.currentTimeMillis(), noCase) + "-" + StringUtil.longToString(counter.getAndIncrement(), noCase); } UUID();  UUID(boolean noCase); String nextID();  }
@Test public void generate() { newUUID(false); String sid = uuid.nextID(); assertTrue(sid.length() > instanceId.length()); assertTrue(sid.startsWith(instanceId)); assertTrue(sid.matches("[A-Za-z0-9-]+")); }
@Test public void generate_noCase() { newUUID(true); String sid = uuid.nextID(); assertTrue(sid.length() > instanceId.length()); assertTrue(sid.startsWith(instanceId)); assertTrue(sid.matches("[A-Z0-9-]+")); }
@Test public synchronized void performance() throws InterruptedException { final String[][] results = new String[concurrency][]; Thread[] threads = new Thread[concurrency]; for (int i = 0; i < concurrency; i++) { final String[] result = new String[loop]; results[i] = result; threads[i] = new Thread(new Runnable() { public void run() { for (int i = 0; i < loop; i++) { result[i] = uuid.nextID(); } } }, "t-" + (i + 1)); } long start = System.currentTimeMillis(); for (Thread thread : threads) { thread.start(); } for (Thread thread : threads) { thread.join(); } long duration = System.currentTimeMillis() - start; System.out.printf("%s: requests=%d, concurrency=%d%n", uuid.getClass().getSimpleName(), concurrency * loop, concurrency); System.out.printf(" Total time: %,d ms.%n", duration); System.out.printf("Average time: %,2.2f \u03BCs.%n", (double) duration / concurrency / loop * 1000); Set<String> allIDs = CollectionUtil.createHashSet(); for (String[] result : results) { for (String id : result) { assertNotNull(id); assertTrue(id, !id.contains("+") && !id.contains("/") && !id.contains("=")); allIDs.add(id); } } assertEquals(concurrency * loop, allIDs.size()); }