mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
@@ -26,17 +26,21 @@ import org.dromara.hutool.core.data.id.Snowflake;
|
||||
import org.dromara.hutool.core.thread.ThreadUtil;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Disabled;
|
||||
import org.junit.jupiter.api.RepeatedTest;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
/**
|
||||
* {@link IdUtil} 单元测试
|
||||
*
|
||||
* @author looly
|
||||
*
|
||||
*/
|
||||
public class IdUtilTest {
|
||||
|
||||
@@ -105,9 +109,9 @@ public class IdUtilTest {
|
||||
//每个线程生成的ID数
|
||||
final int idCountPerThread = 10000;
|
||||
final CountDownLatch latch = new CountDownLatch(threadCount);
|
||||
for(int i =0; i < threadCount; i++) {
|
||||
for (int i = 0; i < threadCount; i++) {
|
||||
ThreadUtil.execute(() -> {
|
||||
for(int i1 = 0; i1 < idCountPerThread; i1++) {
|
||||
for (int i1 = 0; i1 < idCountPerThread; i1++) {
|
||||
final long id = snowflake.next();
|
||||
set.add(id);
|
||||
// Console.log("Add new id: {}", id);
|
||||
@@ -135,9 +139,9 @@ public class IdUtilTest {
|
||||
//每个线程生成的ID数
|
||||
final int idCountPerThread = 10000;
|
||||
final CountDownLatch latch = new CountDownLatch(threadCount);
|
||||
for(int i =0; i < threadCount; i++) {
|
||||
for (int i = 0; i < threadCount; i++) {
|
||||
ThreadUtil.execute(() -> {
|
||||
for(int i1 = 0; i1 < idCountPerThread; i1++) {
|
||||
for (int i1 = 0; i1 < idCountPerThread; i1++) {
|
||||
final long id = IdUtil.getSnowflake(1, 1).next();
|
||||
set.add(id);
|
||||
// Console.log("Add new id: {}", id);
|
||||
@@ -156,9 +160,80 @@ public class IdUtilTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getDataCenterIdTest(){
|
||||
public void getDataCenterIdTest() {
|
||||
//按照mac地址算法拼接的算法,maxDatacenterId应该是0xffffffffL>>6-1此处暂时按照0x7fffffffffffffffL-1,防止最后取模溢出
|
||||
final long dataCenterId = IdUtil.getDataCenterId(Long.MAX_VALUE);
|
||||
Assertions.assertTrue(dataCenterId >= 0);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testUUIDv7Format() {
|
||||
org.dromara.hutool.core.data.id.UUID uuid = org.dromara.hutool.core.data.id.UUID.randomUUID7();
|
||||
String uuidStr = uuid.toString();
|
||||
|
||||
// 验证UUID字符串格式是否符合标准
|
||||
assertTrue(uuidStr.matches("^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUUIDv7Properties() {
|
||||
org.dromara.hutool.core.data.id.UUID uuid = org.dromara.hutool.core.data.id.UUID.randomUUID7();
|
||||
|
||||
// 验证版本号是否为7
|
||||
assertEquals(7, uuid.version());
|
||||
|
||||
// 验证变体是否为IETF variant
|
||||
assertEquals(2, uuid.variant());
|
||||
|
||||
}
|
||||
|
||||
@RepeatedTest(10)
|
||||
public void testUUIDv7Uniqueness() {
|
||||
Set<org.dromara.hutool.core.data.id.UUID> uuids = new HashSet<>();
|
||||
|
||||
// 生成100万个UUIDv7,验证是否有重复
|
||||
for (int i = 0; i < 1000000; i++) {
|
||||
org.dromara.hutool.core.data.id.UUID uuid = org.dromara.hutool.core.data.id.UUID.randomUUID7();
|
||||
assertFalse(uuids.contains(uuid));
|
||||
uuids.add(uuid);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testUUIDv7Monotonicity() {
|
||||
org.dromara.hutool.core.data.id.UUID prev = org.dromara.hutool.core.data.id.UUID.randomUUID7();
|
||||
|
||||
// 验证连续生成的1000个UUIDv7是否呈单调递增趋势
|
||||
for (int i = 0; i < 1000; i++) {
|
||||
org.dromara.hutool.core.data.id.UUID next = org.dromara.hutool.core.data.id.UUID.randomUUID7();
|
||||
assertTrue(next.compareTo(prev) > 0);
|
||||
prev = next;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* UUIDv7的性能测试
|
||||
*/
|
||||
@Test
|
||||
public void testUUIDv7Benchmark() {
|
||||
final StopWatch timer = DateUtil.createStopWatch();
|
||||
|
||||
// UUID v7 generation benchmark
|
||||
timer.start("UUID v7 generation");
|
||||
for (int i = 0; i < 1000000; i++) {
|
||||
IdUtil.randomUUID7();
|
||||
}
|
||||
timer.stop();
|
||||
Console.log("UUIDv7 generation time: {} ms", timer.getLastTaskTimeMillis());
|
||||
|
||||
|
||||
timer.start("UUID v7 generation and formatting");
|
||||
for (int i = 0; i < 1000000; i++) {
|
||||
IdUtil.randomUUID7().replace("-", "");
|
||||
}
|
||||
timer.stop();
|
||||
Console.log("UUIDv7 generation and formatting time: {} ms", timer.getLastTaskTimeMillis());
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user