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
176
177
178
179
180
181
182
183
184
185
// use std::{
// hint::black_box,
// sync::atomic::{AtomicBool, AtomicUsize},
// time::Duration,
// };
//
// use atomo::{Atomo, Context, SerdeBackend};
// use rand::Rng;
//
// type UserId = u8;
//
// fn mint<S: SerdeBackend>(ctx: &mut Context<UserId, u128, S>, user: UserId, amount: u128) -> u128
// { let balance = ctx.get(&user).map(|s| *s).unwrap_or(0);
// ctx.insert(user, balance + amount);
// balance + amount
// }
//
// fn balance<S: SerdeBackend>(ctx: &mut Context<UserId, u128, S>, user: UserId) -> u128 {
// ctx.get(&user).map(|s| *s).unwrap_or(0)
// }
//
// fn main() {
// let (mut u, q) = Atomo::<UserId, u128>::new().split();
//
// let num_query_threads: usize = get_arg("-t").unwrap_or(4);
// let num_query_per_thread: usize = get_arg("-q").unwrap_or(1_000_000);
// let num_mint_per_update: usize = get_arg("-u").unwrap_or(16);
//
// println!("To configure use `-t [num threads] -q [num query per thread] -u [num tx per
// update]"); println!("STARTING BENCHMARK");
// println!("NUM THREADS = {}", num_query_threads);
// println!("NUM QUERY/THREAD = {}", num_query_per_thread);
// println!("NUM TX/UPDATE = {}", num_mint_per_update);
// println!("\n");
//
// static PENDING: AtomicUsize = AtomicUsize::new(0);
// static START: AtomicBool = AtomicBool::new(false);
//
// PENDING.store(num_query_threads, std::sync::atomic::Ordering::Relaxed);
//
// let mut handles = Vec::<std::thread::JoinHandle<Duration>>::new();
//
// for _ in 0..num_query_threads {
// let q = q.clone();
// let handle = std::thread::spawn(move || {
// while !START.load(std::sync::atomic::Ordering::Relaxed) {
// std::hint::spin_loop()
// }
//
// let mut rng: u32 = rand::thread_rng().gen();
//
// let now = std::time::Instant::now();
//
// for _ in 0..num_query_per_thread {
// rng = rng.wrapping_mul(48271) % 0x7fffffff;
// q.run(|ctx| black_box(balance(ctx, rng as u8)));
// }
//
// let duration = now.elapsed();
// PENDING.fetch_sub(1, std::sync::atomic::Ordering::Relaxed);
// duration
// });
//
// handles.push(handle);
// }
//
// let mut rng: u32 = rand::thread_rng().gen();
//
// // signal every threads that's waiting to start.
// START.store(true, std::sync::atomic::Ordering::Relaxed);
//
// let now = std::time::Instant::now();
// let updates = if num_query_threads == 0 {
// for _ in 0..num_query_per_thread {
// let r = rng;
// rng = u.run(move |ctx| {
// let mut rng = r;
// for _ in 0..num_mint_per_update {
// rng = rng.wrapping_mul(48271) % 0x7fffffff;
// black_box(mint(ctx, rng as u8, 17));
// }
// rng
// });
// }
//
// num_query_per_thread
// } else {
// let mut updates = 0;
// loop {
// let r = rng;
// rng = u.run(move |ctx| {
// let mut rng = r;
// for _ in 0..num_mint_per_update {
// rng = rng.wrapping_mul(48271) % 0x7fffffff;
// black_box(mint(ctx, rng as u8, 17));
// }
// rng
// });
// updates += 1;
// if updates % 10 == 0 && PENDING.load(std::sync::atomic::Ordering::Relaxed) == 0 {
// break;
// }
// }
// updates
// };
// let updates_duration = now.elapsed();
//
// // Compute stuff
// let query_times: Vec<Duration> = handles.into_iter().map(|q| q.join().unwrap()).collect();
// let total_query_time: Duration = query_times.iter().sum();
//
// println!("<-UPDATES--------------------------------->");
// print_report(updates_duration, updates);
//
// println!("<-TOTAL NUM OF MINTS---------------------->");
// print_report(updates_duration, updates * num_mint_per_update);
//
// if num_query_threads > 0 {
// println!("<-QUERIES [REAL TIME]--------------------->");
// print_report(updates_duration, num_query_threads * num_query_per_thread);
// }
//
// if num_query_threads > 1 {
// println!("<-QUERIES [CPU TIME]---------------------->");
// print_report(total_query_time, num_query_threads * num_query_per_thread);
//
// println!("<-QUERIES [EACH THREAD]------------------->");
// print_report_vec(&query_times, num_query_per_thread);
// }
// }
//
// fn print_report(duration: Duration, count: usize) {
// print_report_vec(&[duration], count);
// }
//
// fn print_report_vec(duration: &[Duration], count: usize) {
// println!(
// " Count = {}",
// format_vec(duration.iter(), |_| format!("{count}"))
// );
// println!(
// " Took = {}",
// format_vec(duration.iter(), |d| format!("{:.2}s", d.as_secs_f64()))
// );
// println!(
// " Op/s = {}",
// format_vec(duration.iter(), |d| format!("{:.2}", op_per_sec(d, count)))
// );
// println!(
// " t/Op = {}",
// format_vec(duration.iter(), |d| format!(
// "{:.3}μs",
// (d.as_micros() as f64) / (count as f64)
// ))
// );
// }
//
// fn format_vec<T>(v: impl Iterator<Item = T>, f: impl Fn(&T) -> String) -> String {
// let mut result = String::new();
// for item in v {
// result.push_str(&format!("{: ^12}", f(&item)));
// }
// result
// }
//
// fn op_per_sec(duration: &Duration, count: usize) -> usize {
// let duration_sec = duration.as_secs_f64();
// ((count as f64) / duration_sec) as usize
// }
//
// fn get_arg(name: &str) -> Option<usize> {
// let mut args = std::env::args();
//
// while let Some(arg) = args.next() {
// if arg == name {
// let value = args.next().expect("invalid cmd");
// let number = value.parse::<usize>().expect("invalid arg");
// return Some(number);
// }
// }
//
// None
// }
//