Skip to content

Commit

Permalink
feat: add shardingsphere to Mod Uniformity test (#498)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ahoo-Wang authored Nov 30, 2023
1 parent cff9f78 commit 65b456c
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 0 deletions.
1 change: 1 addition & 0 deletions cosid-mod-test/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ dependencies {
implementation(project(":cosid-core"))
testImplementation(project(":cosid-test"))
testImplementation("com.netease.nim:camellia-id-gen-core:1.2.19")
testImplementation("org.apache.shardingsphere:shardingsphere-sharding-core:5.4.1")
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,14 @@

import com.netease.nim.camellia.id.gen.snowflake.CamelliaSnowflakeConfig;
import com.netease.nim.camellia.id.gen.snowflake.CamelliaSnowflakeIdGen;
import org.apache.shardingsphere.sharding.algorithm.keygen.SnowflakeKeyGenerateAlgorithm;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.State;

import java.util.Properties;

/**
* SnowflakeId Benchmark.
*
Expand All @@ -35,6 +38,8 @@ public class SnowflakeIdBenchmark {
public static final int TEST_MACHINE_ID = 1;
SnowflakeFriendlyId cosidSnowflakeId;
CamelliaSnowflakeIdGen camelliaSnowflakeId;
SnowflakeKeyGenerateAlgorithm shardingsphereSnowflakeId;
SnowflakeKeyGenerateAlgorithm shardingsphereSnowflakeIdVibration127;

/**
* Initialize IdGenerator.
Expand All @@ -46,6 +51,16 @@ public void setup() {
CamelliaSnowflakeConfig camelliaSnowflakeCfg = new CamelliaSnowflakeConfig();
camelliaSnowflakeCfg.setWorkerIdGen(maxWorkerId -> TEST_MACHINE_ID);
camelliaSnowflakeId = new CamelliaSnowflakeIdGen(camelliaSnowflakeCfg);

Properties defaultProperties = new Properties();
defaultProperties.setProperty("max-tolerate-time-difference-milliseconds", String.valueOf(200));
shardingsphereSnowflakeId = new SnowflakeKeyGenerateAlgorithm();
shardingsphereSnowflakeId.init(defaultProperties);

Properties vibration127Properties = new Properties(defaultProperties);
shardingsphereSnowflakeIdVibration127 = new SnowflakeKeyGenerateAlgorithm();
vibration127Properties.setProperty("max-vibration-offset", String.valueOf(127));
shardingsphereSnowflakeIdVibration127.init(vibration127Properties);
}

@Benchmark
Expand All @@ -57,4 +72,14 @@ public long cosid() {
public long netease() {
return camelliaSnowflakeId.genId();
}

@Benchmark
public long shardingsphere() {
return shardingsphereSnowflakeId.generateKey();
}

@Benchmark
public long shardingsphereVibration127() {
return shardingsphereSnowflakeIdVibration127.generateKey();
}
}
29 changes: 29 additions & 0 deletions cosid-mod-test/src/test/java/me/ahoo/cosid/test/mod/ModTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@

import com.netease.nim.camellia.id.gen.snowflake.CamelliaSnowflakeConfig;
import com.netease.nim.camellia.id.gen.snowflake.CamelliaSnowflakeIdGen;
import org.apache.shardingsphere.sharding.algorithm.keygen.SnowflakeKeyGenerateAlgorithm;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.time.Duration;
import java.util.Properties;
import java.util.concurrent.locks.LockSupport;

public class ModTest {
Expand All @@ -33,6 +35,7 @@ public class ModTest {
private static final double ALLOWABLE_POP_STD = 10;
SnowflakeFriendlyId cosidSnowflakeId;
CamelliaSnowflakeIdGen camelliaSnowflakeId;
SnowflakeKeyGenerateAlgorithm shardingsphereSnowflakeId = new SnowflakeKeyGenerateAlgorithm();

@BeforeEach
void setup() {
Expand All @@ -49,6 +52,22 @@ public void cosidMod4() {
spec.run();
}

@Test
public void shardingsphereMod4Default() {
ModSpec spec = new ModSpec(ITERATIONS, 4, ALLOWABLE_POP_STD, shardingsphereSnowflakeId::generateKey, () -> LockSupport.parkNanos(Duration.ofMillis(1).toNanos()));
spec.run();
}

@Test
public void shardingsphereMod4Vibration3() {
SnowflakeKeyGenerateAlgorithm idGen = new SnowflakeKeyGenerateAlgorithm();
Properties properties = new Properties();
properties.setProperty("max-vibration-offset", String.valueOf(3));
idGen.init(properties);
ModSpec spec = new ModSpec(ITERATIONS, 4, ALLOWABLE_POP_STD, idGen::generateKey, () -> LockSupport.parkNanos(Duration.ofMillis(1).toNanos()));
spec.run();
}

@Test
public void neteaseMod4() {
ModSpec spec = new ModSpec(ITERATIONS, 4, ALLOWABLE_POP_STD, camelliaSnowflakeId::genId, () -> LockSupport.parkNanos(Duration.ofMillis(1).toNanos()));
Expand All @@ -66,4 +85,14 @@ public void neteaseMod128() {
ModSpec spec = new ModSpec(ITERATIONS, 128, ALLOWABLE_POP_STD, camelliaSnowflakeId::genId, () -> LockSupport.parkNanos(Duration.ofMillis(1).toNanos()));
spec.run();
}

@Test
public void shardingsphereMod128Vibration127() {
SnowflakeKeyGenerateAlgorithm idGen = new SnowflakeKeyGenerateAlgorithm();
Properties properties = new Properties();
properties.setProperty("max-vibration-offset", String.valueOf(127));
idGen.init(properties);
ModSpec spec = new ModSpec(ITERATIONS, 128, ALLOWABLE_POP_STD, idGen::generateKey, () -> LockSupport.parkNanos(Duration.ofMillis(1).toNanos()));
spec.run();
}
}

0 comments on commit 65b456c

Please sign in to comment.