From bdca240465ab765f7fdc181d7bc41dc621b30e0d Mon Sep 17 00:00:00 2001 From: bonn Date: Sat, 9 Oct 2021 10:06:54 +0800 Subject: [PATCH 1/5] =?UTF-8?q?=E4=BF=AE=E6=AD=A3demo=E7=A8=8B=E5=BA=8F?= =?UTF-8?q?=E4=B8=8A=E7=9A=84=E4=B8=80=E4=B8=AA=E9=94=99=E8=AF=AF=20(#14)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 修改demo程序的一个错误 * 修正demo上一个错误 --- TypeScript/README.md | 13 +++++++------ TypeScript/test/test1.ts | 9 +++++++++ TypeScript/test/{test.ts => test2.ts} | 0 3 files changed, 16 insertions(+), 6 deletions(-) create mode 100644 TypeScript/test/test1.ts rename TypeScript/test/{test.ts => test2.ts} (100%) diff --git a/TypeScript/README.md b/TypeScript/README.md index 1509999..00e643e 100644 --- a/TypeScript/README.md +++ b/TypeScript/README.md @@ -10,9 +10,7 @@ 执行测试代码 ```bash -ts-node test/test.ts - -NODE_ENV=development REDIS_HOST=127.0.0.1 +ts-node test/test1.ts ``` @@ -20,11 +18,14 @@ NODE_ENV=development REDIS_HOST=127.0.0.1 ## 使用 ```js -import { Genid } from '../index' +import { snowflakeIdv1 } from '../snowflakeIdv1' + +const WorkerId = process.env.WorkerId == undefined ? 1 : process.env.WorkerId +const Method = process.env.Method == undefined ? 1 : process.env.Method -let gen = new Genid({ WorkerId: 1 }) -let id1 = gen.NextId() +let gen1 = new snowflakeIdv1({ WorkerId: WorkerId, Method: Method }) +let id1 = gen1.NextId() console.log(id1, id1.toString().length) ``` diff --git a/TypeScript/test/test1.ts b/TypeScript/test/test1.ts new file mode 100644 index 0000000..1957567 --- /dev/null +++ b/TypeScript/test/test1.ts @@ -0,0 +1,9 @@ +import { snowflakeIdv1 } from '../snowflakeIdv1' + +const WorkerId = process.env.WorkerId == undefined ? 1 : process.env.WorkerId + +const Method = process.env.Method == undefined ? 1 : process.env.Method + +let gen1 = new snowflakeIdv1({ WorkerId: WorkerId, Method: Method }) +let id1 = gen1.NextId() +console.log(id1, id1.toString().length) \ No newline at end of file diff --git a/TypeScript/test/test.ts b/TypeScript/test/test2.ts similarity index 100% rename from TypeScript/test/test.ts rename to TypeScript/test/test2.ts From 458bfae171f727d981e6fbf3cf0b23d31a35012f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8B=8F=E6=8C=AF=E5=AE=87?= <858514558@qq.com> Date: Sat, 9 Oct 2021 02:14:34 +0000 Subject: [PATCH 2/5] =?UTF-8?q?!15=20=E5=A2=9E=E5=8A=A0java=E6=80=A7?= =?UTF-8?q?=E8=83=BD=E6=B5=8B=E8=AF=95=E6=A1=86=E6=9E=B6=20*=20=E8=B0=83?= =?UTF-8?q?=E6=95=B4=E5=8F=82=E6=95=B0=20*=20=E5=A2=9E=E5=8A=A0JMH?= =?UTF-8?q?=E6=80=A7=E8=83=BD=E6=B5=8B=E8=AF=95=E6=A1=86=E6=9E=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Java/source/pom.xml | 15 +++++- .../java/com/github/yitter/test/GenTest.java | 2 - .../src/test/java/com/github/yitter/test/README.md | 7 +++ .../java/com/github/yitter/test/StartUp.java | 2 - .../java/com/github/yitter/test/StartUpJmh.java | 62 +++++++++++++++++++++ .../java/com/github/yitter/test/StartUpJmh2.java | 63 ++++++++++++++++++++++ 6 files changed, 146 insertions(+), 5 deletions(-) rename Java/source/src/{main => test}/java/com/github/yitter/test/GenTest.java (90%) create mode 100644 Java/source/src/test/java/com/github/yitter/test/README.md rename Java/source/src/{main => test}/java/com/github/yitter/test/StartUp.java (90%) create mode 100644 Java/source/src/test/java/com/github/yitter/test/StartUpJmh.java create mode 100644 Java/source/src/test/java/com/github/yitter/test/StartUpJmh2.java diff --git a/Java/source/pom.xml b/Java/source/pom.xml index bb41e2a..0d8f601 100644 --- a/Java/source/pom.xml +++ b/Java/source/pom.xml @@ -45,7 +45,20 @@ 1.8 1.8 - + + + org.openjdk.jmh + jmh-core + 1.21 + provided + + + org.openjdk.jmh + jmh-generator-annprocess + 1.21 + provided + + diff --git a/Java/source/src/main/java/com/github/yitter/test/GenTest.java b/Java/source/src/test/java/com/github/yitter/test/GenTest.java similarity index 90% rename from Java/source/src/main/java/com/github/yitter/test/GenTest.java rename to Java/source/src/test/java/com/github/yitter/test/GenTest.java index 09430a4..036ec86 100644 --- a/Java/source/src/main/java/com/github/yitter/test/GenTest.java +++ b/Java/source/src/test/java/com/github/yitter/test/GenTest.java @@ -1,8 +1,6 @@ package com.github.yitter.test; -import com.github.yitter.contract.IIdGenerator; import com.github.yitter.idgen.YitIdHelper; - import java.util.HashSet; import java.util.Set; diff --git a/Java/source/src/test/java/com/github/yitter/test/README.md b/Java/source/src/test/java/com/github/yitter/test/README.md new file mode 100644 index 0000000..571f6c4 --- /dev/null +++ b/Java/source/src/test/java/com/github/yitter/test/README.md @@ -0,0 +1,7 @@ +JMH 基准测试 漂移算法 macbook pro 13(2020) i5 jdk1.8.0_301 +``` +Benchmark Mode Cnt Score Error Units +StartUpJmh.testGetPojo thrpt 40 58835.536 ± 535.605 ops/s +StartUpJmh.testGetPojo avgt 40 ≈ 10⁻⁵ s/op +``` + diff --git a/Java/source/src/main/java/com/github/yitter/test/StartUp.java b/Java/source/src/test/java/com/github/yitter/test/StartUp.java similarity index 90% rename from Java/source/src/main/java/com/github/yitter/test/StartUp.java rename to Java/source/src/test/java/com/github/yitter/test/StartUp.java index 359c88b..a2334d4 100644 --- a/Java/source/src/main/java/com/github/yitter/test/StartUp.java +++ b/Java/source/src/test/java/com/github/yitter/test/StartUp.java @@ -1,8 +1,6 @@ package com.github.yitter.test; -import com.github.yitter.contract.IIdGenerator; import com.github.yitter.contract.IdGeneratorOptions; -import com.github.yitter.idgen.DefaultIdGenerator; import com.github.yitter.idgen.YitIdHelper; public class StartUp { diff --git a/Java/source/src/test/java/com/github/yitter/test/StartUpJmh.java b/Java/source/src/test/java/com/github/yitter/test/StartUpJmh.java new file mode 100644 index 0000000..5ba25f7 --- /dev/null +++ b/Java/source/src/test/java/com/github/yitter/test/StartUpJmh.java @@ -0,0 +1,62 @@ +package com.github.yitter.test; + +import com.github.yitter.contract.IdGeneratorOptions; +import com.github.yitter.idgen.YitIdHelper; +import java.util.concurrent.TimeUnit; +import org.openjdk.jmh.annotations.Benchmark; +import org.openjdk.jmh.annotations.BenchmarkMode; +import org.openjdk.jmh.annotations.Level; +import org.openjdk.jmh.annotations.Mode; +import org.openjdk.jmh.annotations.OutputTimeUnit; +import org.openjdk.jmh.annotations.Scope; +import org.openjdk.jmh.annotations.Setup; +import org.openjdk.jmh.annotations.State; +import org.openjdk.jmh.annotations.Threads; +import org.openjdk.jmh.runner.Runner; +import org.openjdk.jmh.runner.RunnerException; +import org.openjdk.jmh.runner.options.Options; +import org.openjdk.jmh.runner.options.OptionsBuilder; + +/** + * @author suzhenyu + * @date 2021/9/27 + */ +// 测试方法平均执行时间 +@BenchmarkMode({Mode.All}) +// 输出结果的时间粒度为微秒 +@OutputTimeUnit(TimeUnit.MILLISECONDS) +@State(Scope.Thread) +//@Threads(2) +public class StartUpJmh { + + //1-漂移算法,2-传统算法 + final static short method = 1; + + public static void main(String[] args) throws RunnerException { + Options options = new OptionsBuilder().include(StartUpJmh.class.getSimpleName()) + .warmupIterations(1).measurementIterations(5).forks(1).build(); + new Runner(options).run(); + } + + /** + * setup初始化容器的时候只执行一次 + */ + @Setup(Level.Trial) + public void init() { + IdGeneratorOptions options = new IdGeneratorOptions(); + options.WorkerIdBitLength = 6; + options.SeqBitLength = 10; + options.BaseTime = System.currentTimeMillis(); + options.Method = method; + options.WorkerId = 1; + + // 首先测试一下 IdHelper 方法,获取单个Id + YitIdHelper.setIdGenerator(options); + YitIdHelper.nextId(); + } + + @Benchmark + public void testNextId() { + YitIdHelper.nextId(); + } +} diff --git a/Java/source/src/test/java/com/github/yitter/test/StartUpJmh2.java b/Java/source/src/test/java/com/github/yitter/test/StartUpJmh2.java new file mode 100644 index 0000000..07632b0 --- /dev/null +++ b/Java/source/src/test/java/com/github/yitter/test/StartUpJmh2.java @@ -0,0 +1,63 @@ +package com.github.yitter.test; + +import com.github.yitter.contract.IdGeneratorOptions; +import com.github.yitter.idgen.YitIdHelper; +import java.util.concurrent.TimeUnit; +import org.openjdk.jmh.annotations.Benchmark; +import org.openjdk.jmh.annotations.BenchmarkMode; +import org.openjdk.jmh.annotations.Level; +import org.openjdk.jmh.annotations.Mode; +import org.openjdk.jmh.annotations.OutputTimeUnit; +import org.openjdk.jmh.annotations.Scope; +import org.openjdk.jmh.annotations.Setup; +import org.openjdk.jmh.annotations.State; +import org.openjdk.jmh.annotations.Threads; +import org.openjdk.jmh.runner.Runner; +import org.openjdk.jmh.runner.RunnerException; +import org.openjdk.jmh.runner.options.Options; +import org.openjdk.jmh.runner.options.OptionsBuilder; + +/** + * @author suzhenyu + * @date 2021/9/27 + */ +// 测试方法平均执行时间 +@BenchmarkMode({Mode.All}) +// 输出结果的时间粒度为微秒 +@OutputTimeUnit(TimeUnit.MILLISECONDS) +@State(Scope.Thread) +//@Threads(2) +public class StartUpJmh2 { + + //1-漂移算法,2-传统算法 + final static short method = 2; + + public static void main(String[] args) throws RunnerException { + Options options = new OptionsBuilder().include(StartUpJmh2.class.getName() + ".*") + .warmupIterations(1).measurementIterations(5).forks(2).build(); + new Runner(options).run(); + } + + /** + * setup初始化容器的时候只执行一次 + */ + @Setup(Level.Trial) + public void init() { + IdGeneratorOptions options = new IdGeneratorOptions(); + options.WorkerIdBitLength = 6; + options.SeqBitLength = 10; + options.BaseTime = System.currentTimeMillis(); + options.Method = method; + options.WorkerId = 1; + + // 首先测试一下 IdHelper 方法,获取单个Id + YitIdHelper.setIdGenerator(options); + YitIdHelper.nextId(); + } + + @Benchmark + public void testGetPojo() { + + YitIdHelper.nextId(); + } +} From fcb64f79e81bc676f4ffa02c3aecb3131b7cc2ce Mon Sep 17 00:00:00 2001 From: yitter Date: Fri, 15 Oct 2021 16:39:39 +0800 Subject: [PATCH 3/5] auto commit --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index b82dabc..bd8c4ba 100644 --- a/README.md +++ b/README.md @@ -108,7 +108,7 @@ QQ群:646049993 387750301904971 (运行3年) 646093214093387 (运行5年) 1292658282840139 (运行10年) -9007199254740992 (js Number 最大值) +9007199254740992 (js Number 最大值,可以支撑70年) 165399880288699493 (普通雪花算法生成的ID) ``` @@ -160,7 +160,7 @@ QQ群:646049993 第二版增加参数(非必须): -❄ ***DataCenterId***,数据中心ID(默认0),请确保全局唯一。 +❄ ***DataCenterId***,数据中心ID(机房ID,默认0),请确保全局唯一。 ❄ ***DataCenterIdBitLength***,数据中心ID长度(默认0)。 From c8ca844ce6056c0a7436ee0fd32c64349ac53219 Mon Sep 17 00:00:00 2001 From: yitter Date: Mon, 18 Oct 2021 11:30:40 +0800 Subject: [PATCH 4/5] auto commit --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index bd8c4ba..8f515fa 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,7 @@ QQ群:646049993 ❄ 兼容所有雪花算法(号段模式或经典模式,大厂或小厂),将来你可做任意的升级切换。(一般无须升级,但理论上支持) -❄ 这是计算机历史上最全面的雪花ID生成工具,期待你来超越😀 +❄ 这是计算机历史上最全面的雪花ID生成工具。 #### 需求来源 @@ -211,7 +211,7 @@ QQ群:646049993 🔍 当然,如果你的服务无需自动扩容,那就不必自动注册WorkerId,而是为它们分别设置全局唯一值。 -🔍 发挥你的想象力,方法还有很多。此处抛砖引玉:开发中心化的ID生成服务,由它为各端点服务(单个或批量)生成可用ID。 +🔍 方法还有很多,例如:开发中心化的ID生成服务,由它为各端点服务(单个或批量)生成可用ID。 #### 自动注册流程图 From 91459b1538b24a03174b20eba10db410d8ef5268 Mon Sep 17 00:00:00 2001 From: bonn Date: Wed, 27 Oct 2021 11:54:38 +0800 Subject: [PATCH 5/5] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=5FIsOverCost=E5=8F=82?= =?UTF-8?q?=E6=95=B0=E9=94=99=E8=AF=AF=E7=9A=84=E9=97=AE=E9=A2=98=EF=BC=8C?= =?UTF-8?q?=E8=A7=A3=E5=86=B3bigint=E7=9A=84=E9=97=AE=E9=A2=98=EF=BC=8C?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0demo=20(#18)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 修改demo程序的一个错误 * 修正demo上一个错误 * 修复_IsOverCost参数错误的问题,解决bigint的问题,增加demo * 统一输入为bigint类型 * 增加测试demo * Update README.md Co-authored-by: yitter <80446528+yitter@users.noreply.github.com> --- JavaScript/test/test2.js | 9 +-- TypeScript/README.md | 153 +++++++++++++++++++++++++++++++++++++++++++- TypeScript/snowflakeIdv1.ts | 12 ++-- TypeScript/test/test1.ts | 6 +- TypeScript/test/test3.ts | 9 +++ TypeScript/test/test4.ts | 9 +++ TypeScript/test/test5.ts | 18 ++++++ 7 files changed, 199 insertions(+), 17 deletions(-) create mode 100644 TypeScript/test/test3.ts create mode 100644 TypeScript/test/test4.ts create mode 100644 TypeScript/test/test5.ts diff --git a/JavaScript/test/test2.js b/JavaScript/test/test2.js index 1aa6048..1e7fd4b 100644 --- a/JavaScript/test/test2.js +++ b/JavaScript/test/test2.js @@ -8,10 +8,11 @@ function test1() { } } function test2() { - const genid = new GenId({ WorkerId: 1 }) - const id = genid.NextId() - console.log(typeof (id)) - console.log(id, id.toString().length) + const genid = new GenId({ WorkerId: 1, SeqBitLength: 14 }) + for (let i = 0; i < 10; i++) { + let id1 = genid.NextId() + console.log(`${i} ID:${id1} ${typeof id1} 长度:${id1.toString().length}`) + } } function main() { diff --git a/TypeScript/README.md b/TypeScript/README.md index 00e643e..f0e47b5 100644 --- a/TypeScript/README.md +++ b/TypeScript/README.md @@ -6,6 +6,10 @@ 代码贡献者:zhupengfei(在 bubao 布宝 的JS基础上改版,感谢bubao 布宝) +js Number 类型最大数值:9007199254740992(16位), + +在JS中没有bigint类型,所以建议将ID控制在16位以内,统一使用number类型 + 执行测试代码 @@ -22,12 +26,155 @@ import { snowflakeIdv1 } from '../snowflakeIdv1' const WorkerId = process.env.WorkerId == undefined ? 1 : process.env.WorkerId -const Method = process.env.Method == undefined ? 1 : process.env.Method - -let gen1 = new snowflakeIdv1({ WorkerId: WorkerId, Method: Method }) +let gen1 = new snowflakeIdv1({ WorkerId: WorkerId}) let id1 = gen1.NextId() console.log(id1, id1.toString().length) ``` +## 示例 + +```js +import { snowflakeIdv1 } from '../snowflakeIdv1' + +const WorkerId = process.env.WorkerId == undefined ? 1 : process.env.WorkerId + +let gen1 = new snowflakeIdv1({ WorkerId: WorkerId, SeqBitLength: 6 }) +for (let i = 0; i < 10; i++) { + let id1 = gen1.NextId() + console.log(`${i} ID:${id1} 长度:${id1.toString().length}`) +} + +$ ts-node test/test4.ts +0 ID:234712552579141 长度:15 +1 ID:234712552587333 长度:15 +2 ID:234712552587334 长度:15 +3 ID:234712552587335 长度:15 +4 ID:234712552587336 长度:15 +5 ID:234712552591429 长度:15 +6 ID:234712552591430 长度:15 +7 ID:234712552591431 长度:15 +8 ID:234712552591432 长度:15 +9 ID:234712552591433 长度:15 + + +import { snowflakeIdv1 } from '../snowflakeIdv1' + +const WorkerId = process.env.WorkerId == undefined ? 1 : process.env.WorkerId + +let gen1 = new snowflakeIdv1({ WorkerId: WorkerId, SeqBitLength: 11 }) +for (let i = 0; i < 10; i++) { + let id1 = gen1.NextId() + console.log(`${i} ID:${id1} ${typeof id1} 长度:${id1.toString().length}`) +} + + +$ ts-node test/test4.ts +0 ID:7510958933018629 number 长度:16 +1 ID:7510958933280773 number 长度:16 +2 ID:7510958933280774 number 长度:16 +3 ID:7510958933280775 number 长度:16 +4 ID:7510958933411845 number 长度:16 +5 ID:7510958933411846 number 长度:16 +6 ID:7510958933542917 number 长度:16 +7 ID:7510958933542918 number 长度:16 +8 ID:7510958933542919 number 长度:16 +9 ID:7510958933673989 number 长度:16 + +import { snowflakeIdv1 } from '../snowflakeIdv1' + +const WorkerId = process.env.WorkerId == undefined ? 1 : process.env.WorkerId + +let gen1 = new snowflakeIdv1({ WorkerId: WorkerId, SeqBitLength: 12 }) +for (let i = 0; i < 10; i++) { + let id1 = gen1.NextId() + console.log(`${i} ID:${id1} ${typeof id1} 长度:${id1.toString().length}`) +} + +$ ts-node test/test4.ts +0 ID:15021931987734533 bigint 长度:17 +1 ID:15021931987996677 bigint 长度:17 +2 ID:15021931987996678 bigint 长度:17 +3 ID:15021931987996679 bigint 长度:17 +4 ID:15021931987996680 bigint 长度:17 +5 ID:15021931988258821 bigint 长度:17 +6 ID:15021931988258822 bigint 长度:17 +7 ID:15021931988258823 bigint 长度:17 +8 ID:15021931988258824 bigint 长度:17 +9 ID:15021931988520965 bigint 长度:17 + + +import { snowflakeIdv1 } from '../snowflakeIdv1' + +const WorkerId = process.env.WorkerId == undefined ? 1 : process.env.WorkerId + +let gen1 = new snowflakeIdv1({ WorkerId: WorkerId, SeqBitLength: 13 }) +for (let i = 0; i < 10; i++) { + let id1 = gen1.NextId() + console.log(`${i} ID:${id1} ${typeof id1} 长度:${id1.toString().length}`) +} + +$ ts-node test/test4.ts +0 ID:30043877337997317 bigint 长度:17 +1 ID:30043877338521605 bigint 长度:17 +2 ID:30043877338521606 bigint 长度:17 +3 ID:30043877339045893 bigint 长度:17 +4 ID:30043877339045894 bigint 长度:17 +5 ID:30043877339045895 bigint 长度:17 +6 ID:30043877339045896 bigint 长度:17 +7 ID:30043877339570181 bigint 长度:17 +8 ID:30043877339570182 bigint 长度:17 +9 ID:30043877339570183 bigint 长度:17 + + + + + + + + + + + + + + + + +``` + +## 同时兼容number和bigint的写法 + +如果您觉得这个用法更好,可以手动替换对应方法 + +```js + /** + * 生成ID + * @returns + */ + public NextId(): number | bigint { + if (this._IsOverCost) { + // + let id = this.NextOverCostId() + if (id >= 9007199254740992n) + return id + else + return parseInt(id.toString()) + } else { + // + let id = this.NextNormalId() + if (id >= 9007199254740992n) + return id + else + return parseInt(id.toString()) + } + } + +``` + +## 其他帮助 + +在mysql中int类型最大长度是10位数字,由于本算法默认生成的是15位,最短也是11位,所以在mysql中需要使用bigint数据类型 + + diff --git a/TypeScript/snowflakeIdv1.ts b/TypeScript/snowflakeIdv1.ts index eb1aff4..f84a22f 100644 --- a/TypeScript/snowflakeIdv1.ts +++ b/TypeScript/snowflakeIdv1.ts @@ -328,13 +328,13 @@ export class snowflakeIdv1 { * 生成ID * @returns */ - public NextId(): number { - if (this.Method == BigInt(1)) { - //雪花漂移算法 - return parseInt(this.NextOverCostId().toString()) + public NextId(): bigint { + if (this._IsOverCost) { + // + return this.NextOverCostId() } else { - //常规雪花算法 - return parseInt(this.NextNormalId().toString()) + // + return this.NextNormalId() } } } diff --git a/TypeScript/test/test1.ts b/TypeScript/test/test1.ts index 1957567..5041b27 100644 --- a/TypeScript/test/test1.ts +++ b/TypeScript/test/test1.ts @@ -2,8 +2,6 @@ import { snowflakeIdv1 } from '../snowflakeIdv1' const WorkerId = process.env.WorkerId == undefined ? 1 : process.env.WorkerId -const Method = process.env.Method == undefined ? 1 : process.env.Method - -let gen1 = new snowflakeIdv1({ WorkerId: WorkerId, Method: Method }) +let gen1 = new snowflakeIdv1({ WorkerId: WorkerId }) let id1 = gen1.NextId() -console.log(id1, id1.toString().length) \ No newline at end of file +console.log(`ID:${id1} 长度:${id1.toString().length}`) \ No newline at end of file diff --git a/TypeScript/test/test3.ts b/TypeScript/test/test3.ts new file mode 100644 index 0000000..1957567 --- /dev/null +++ b/TypeScript/test/test3.ts @@ -0,0 +1,9 @@ +import { snowflakeIdv1 } from '../snowflakeIdv1' + +const WorkerId = process.env.WorkerId == undefined ? 1 : process.env.WorkerId + +const Method = process.env.Method == undefined ? 1 : process.env.Method + +let gen1 = new snowflakeIdv1({ WorkerId: WorkerId, Method: Method }) +let id1 = gen1.NextId() +console.log(id1, id1.toString().length) \ No newline at end of file diff --git a/TypeScript/test/test4.ts b/TypeScript/test/test4.ts new file mode 100644 index 0000000..ab51f36 --- /dev/null +++ b/TypeScript/test/test4.ts @@ -0,0 +1,9 @@ +import { snowflakeIdv1 } from '../snowflakeIdv1' + +const WorkerId = process.env.WorkerId == undefined ? 1 : process.env.WorkerId + +let gen1 = new snowflakeIdv1({ WorkerId: WorkerId, SeqBitLength: 10 }) +for (let i = 0; i < 10; i++) { + let id1 = gen1.NextId() + console.log(`${i} ID:${id1} ${typeof id1} 长度:${id1.toString().length}`) +} diff --git a/TypeScript/test/test5.ts b/TypeScript/test/test5.ts new file mode 100644 index 0000000..5877aca --- /dev/null +++ b/TypeScript/test/test5.ts @@ -0,0 +1,18 @@ +import { snowflakeIdv1 } from '../snowflakeIdv1' + +const WorkerId = process.env.WorkerId == undefined ? 1 : process.env.WorkerId + +let gen1 = new snowflakeIdv1({ WorkerId: WorkerId, SeqBitLength: 10 }) +// for (let i = 0; i < 10; i++) { +// let id1 = gen1.NextId() +// console.log(`${i} ID:${id1} ${typeof id1} 长度:${id1.toString().length}`) +// } + +console.time("Test Run") +const HSet = new Set() +for (let index = 0; index < 500000; index++) { + HSet.add(gen1.NextId()) +} +console.timeEnd("Test Run") +console.log([...HSet.values()].join("\n")) +console.log(HSet.size) \ No newline at end of file