From f9c2afa8a23829a416126588cab3a0ebd6ef5f37 Mon Sep 17 00:00:00 2001 From: Yami An Date: Thu, 22 Jun 2023 15:50:05 +0700 Subject: [PATCH] 230622 --- .../Id Generator Snowflake Benchmarks.csproj | 1 + Id Generator Snowflake Benchmarks/Program.cs | 9 +++++++-- Id Generator Snowflake/Common/Constant.cs | 4 ++-- 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/Id Generator Snowflake Benchmarks/Id Generator Snowflake Benchmarks.csproj b/Id Generator Snowflake Benchmarks/Id Generator Snowflake Benchmarks.csproj index cfa1562..d6ab5bc 100644 --- a/Id Generator Snowflake Benchmarks/Id Generator Snowflake Benchmarks.csproj +++ b/Id Generator Snowflake Benchmarks/Id Generator Snowflake Benchmarks.csproj @@ -10,6 +10,7 @@ + diff --git a/Id Generator Snowflake Benchmarks/Program.cs b/Id Generator Snowflake Benchmarks/Program.cs index a4ed6e8..a2e5a59 100644 --- a/Id Generator Snowflake Benchmarks/Program.cs +++ b/Id Generator Snowflake Benchmarks/Program.cs @@ -7,8 +7,12 @@ using static Id_Generator_Snowflake.IdGenerator; using static System.Threading.Tasks.Parallel; using static System.Threading.Thread; +using static YANLib.YANNum; -var idGen = new IdGenerator(0, 0); +var wkrId = GenerateRandomLong(0, 31); +var dcId = GenerateRandomLong(0, 31); +WriteLine($"WorkerID (setup): {wkrId} - DatacenterID (setup): {dcId}"); +var idGen = new IdGenerator(wkrId, dcId); var genIds = new HashSet(); var numIds = 100; var top = 10; @@ -79,7 +83,8 @@ foreach (var id in genIds.Take(top)) { - WriteLine($"ID: {id} - Time: {ExtractIdComponents(id).Item1} - WorkerID: {ExtractIdComponents(id).Item2} - DatacenterID: {ExtractIdComponents(id).Item3}"); + var tupl = ExtractIdComponents(id); + WriteLine($"ID (generated): {id} - Time (extracted): {tupl.Item1} - WorkerID (extracted): {tupl.Item2} - DatacenterID (extracted): {tupl.Item3}"); } #endif diff --git a/Id Generator Snowflake/Common/Constant.cs b/Id Generator Snowflake/Common/Constant.cs index 083f160..28b8b57 100644 --- a/Id Generator Snowflake/Common/Constant.cs +++ b/Id Generator Snowflake/Common/Constant.cs @@ -2,9 +2,9 @@ internal static class Constant { - internal const int WKR_ID_BITS = 7; // Number of bits used to store the Worker Id + internal const int WKR_ID_BITS = 5; // Number of bits used to store the Worker Id internal const int DC_ID_BITS = 5; // Number of bits used to store the Datacenter Id - internal const int SEQ_BITS = 11; // Number of bits used to store the Sequence + internal const int SEQ_BITS = 13; // Number of bits used to store the Sequence internal const long MAX_WKR_ID = -1 ^ (-1 << WKR_ID_BITS); // Maximum value of the Worker Id internal const long MAX_DC_ID = -1 ^ (-1 << DC_ID_BITS); // Maximum value of the Datacenter Id