1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
pub mod idgen;
pub use idgen::id_generator::DefaultIdGenerator;
pub use idgen::id_generator_options::IdGeneratorOptions;
pub use idgen::id_helper::{IdHelper, IdVecHelper, IdMapHelper};
#[cfg(test)]
mod tests {
use crate::{IdHelper, IdVecHelper, IdMapHelper, IdGeneratorOptions};
use chrono::Utc;
use std::collections::HashSet;
#[test]
fn single_thread_test() {
let mut times = 5000000;
IdHelper::init();
let mut options = IdGeneratorOptions::new(24);
options.seq_bit_len = 10;
IdHelper::set_id_generator(options);
let start = Utc::now().timestamp_millis();
let mut new_id: i64 = 0;
while times > 0 {
new_id = IdHelper::next_id();
times -= 1;
}
let end = Utc::now().timestamp_millis();
println!("Last one {}-ID: {}", 50000 - times, new_id);
println!("Single thread time: {} ms", end - start);
}
#[test]
fn single_thread_check() {
let mut set: HashSet<i64> = HashSet::new();
let mut times = 500000;
IdHelper::init();
let options = IdGeneratorOptions::new(23);
IdHelper::set_id_generator(options);
let start = Utc::now().timestamp_millis();
while times > 0 {
let new_id = IdHelper::next_id();
if !set.contains(&new_id) {
set.insert(new_id);
} else {
panic!("Check fails! Same id!");
}
times -= 1;
}
let end = Utc::now().timestamp_millis();
println!("Single thread check time: {} ms", end - start);
}
#[test]
fn single_thread_test_map() {
let mut times = 5000000;
let worker_id: u32 = 36;
IdMapHelper::init(vec![worker_id]);
let mut options = IdGeneratorOptions::new(worker_id);
options.seq_bit_len = 10;
IdMapHelper::set_id_generator(options);
let start = Utc::now().timestamp_millis();
let mut new_id: i64 = 0;
while times > 0 {
new_id = IdMapHelper::next_id(worker_id);
times -= 1;
}
let end = Utc::now().timestamp_millis();
println!("Last one {}-ID: {}", 500000 - times, new_id);
println!("Single thread time: {} ms", end - start);
}
#[test]
fn single_thread_check_map() {
let mut set: HashSet<i64> = HashSet::new();
let mut times = 500000;
let worker_id: u32 = 38;
IdMapHelper::init(vec![worker_id, worker_id+1]);
let options = IdGeneratorOptions::new(worker_id);
IdMapHelper::set_id_generator(options);
let start = Utc::now().timestamp_millis();
while times > 0 {
let new_id = IdMapHelper::next_id(worker_id);
if !set.contains(&new_id) {
set.insert(new_id);
} else {
panic!("Check fails! Same id!");
}
times -= 1;
}
let end = Utc::now().timestamp_millis();
println!("Single thread check time: {} ms", end - start);
let options = IdGeneratorOptions::new(worker_id+1);
IdMapHelper::set_id_generator(options);
times = 500000;
let start = Utc::now().timestamp_millis();
while times > 0 {
let new_id = IdMapHelper::next_id(worker_id+1);
if !set.contains(&new_id) {
set.insert(new_id);
} else {
panic!("Check fails! Same id!");
}
times -= 1;
}
let end = Utc::now().timestamp_millis();
println!("Single thread check time: {} ms", end - start);
}
#[test]
fn single_thread_test_vec() {
let mut times = 5000000;
let worker_id: u32 = 1;
IdVecHelper::init(worker_id, 2, 2);
let mut options = IdGeneratorOptions::new(worker_id+2);
options.seq_bit_len = 10;
IdVecHelper::set_id_generator(options);
let start = Utc::now().timestamp_millis();
let mut new_id: i64 = 0;
while times > 0 {
new_id = IdVecHelper::next_id(worker_id+2);
times -= 1;
}
let end = Utc::now().timestamp_millis();
println!("Last one {}-ID: {}", 500000 - times, new_id);
println!("Single thread time: {} ms", end - start);
}
#[test]
fn single_thread_check_vec() {
let mut set: HashSet<i64> = HashSet::new();
let mut times = 500000;
let worker_id: u32 = 10;
IdVecHelper::init(worker_id, 2, 3);
let options = IdGeneratorOptions::new(worker_id);
IdVecHelper::set_id_generator(options);
let start = Utc::now().timestamp_millis();
while times > 0 {
let new_id = IdVecHelper::next_id(worker_id);
if !set.contains(&new_id) {
set.insert(new_id);
} else {
panic!("Check fails! Same id!");
}
times -= 1;
}
let end = Utc::now().timestamp_millis();
println!("Single thread check time: {} ms", end - start);
times = 500000;
let options = IdGeneratorOptions::new(worker_id+4);
IdVecHelper::set_id_generator(options);
let start = Utc::now().timestamp_millis();
while times > 0 {
let new_id = IdVecHelper::next_id(worker_id+4);
if !set.contains(&new_id) {
set.insert(new_id);
} else {
panic!("Check fails! Same id!");
}
times -= 1;
}
let end = Utc::now().timestamp_millis();
println!("Single thread check time: {} ms", end - start);
}
}