crossbeam.rs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2024 Dyne.org foundation
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU Affero General Public License as
  7. * published by the Free Software Foundation, either version 3 of the
  8. * License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU Affero General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Affero General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. use criterion::{criterion_group, criterion_main, BatchSize, BenchmarkId, Criterion};
  19. use crossbeam_skiplist::SkipMap;
  20. use easy_parallel::Parallel;
  21. use rand::{rngs::OsRng, Rng};
  22. use std::{
  23. collections::HashMap,
  24. sync::{
  25. atomic::{AtomicBool, Ordering},
  26. Arc, Mutex,
  27. },
  28. };
  29. fn crossbeam(c: &mut Criterion) {
  30. let mut group = c.benchmark_group("crossbeam");
  31. for k in 1..10 {
  32. let stopped = Arc::new(AtomicBool::new(false));
  33. let map = Arc::new(SkipMap::new());
  34. let stopped2 = stopped.clone();
  35. let map2 = map.clone();
  36. // Start n threads all doing continuous inserts until we tell them to stop
  37. let parallel_inserts = std::thread::spawn(move || {
  38. Parallel::new()
  39. .each(0..k, |_| {
  40. let stopped = stopped2.clone();
  41. let map = map2.clone();
  42. while !stopped.load(Ordering::Relaxed) {
  43. let key: usize = OsRng.gen();
  44. let val: usize = OsRng.gen();
  45. map.insert(key, val);
  46. }
  47. })
  48. .run();
  49. });
  50. group.bench_with_input(BenchmarkId::from_parameter(k), &k, |b, &_| {
  51. b.iter_batched(
  52. || {
  53. let key: usize = OsRng.gen();
  54. let val: usize = OsRng.gen();
  55. (key, val)
  56. },
  57. |(key, val)| {
  58. // Do 10k inserts
  59. map.insert(key, val);
  60. },
  61. BatchSize::SmallInput,
  62. )
  63. });
  64. stopped.store(true, Ordering::Relaxed);
  65. parallel_inserts.join().unwrap();
  66. }
  67. group.finish();
  68. // Now try normal Mutex hashmap
  69. // This is not an async Mutex, but async Mutexes are always slower than sync ones anyway
  70. // since they just implement an async interface on top of sync Mutexes.
  71. let mut group = c.benchmark_group("mutex_hashmap");
  72. for k in 1..10 {
  73. let stopped = Arc::new(AtomicBool::new(false));
  74. let map = Arc::new(Mutex::new(HashMap::new()));
  75. let stopped2 = stopped.clone();
  76. let map2 = map.clone();
  77. // Start n threads all doing continuous inserts until we tell them to stop
  78. let parallel_inserts = std::thread::spawn(move || {
  79. Parallel::new()
  80. .each(0..k, |_| {
  81. let stopped = stopped2.clone();
  82. let map = map2.clone();
  83. while !stopped.load(Ordering::Relaxed) {
  84. let key: usize = OsRng.gen();
  85. let val: usize = OsRng.gen();
  86. map.lock().unwrap().insert(key, val);
  87. }
  88. })
  89. .run();
  90. });
  91. group.bench_with_input(BenchmarkId::from_parameter(k), &k, |b, &_| {
  92. b.iter_batched(
  93. || {
  94. let key: usize = OsRng.gen();
  95. let val: usize = OsRng.gen();
  96. (key, val)
  97. },
  98. |(key, val)| {
  99. // Do 10k inserts
  100. map.lock().unwrap().insert(key, val);
  101. },
  102. BatchSize::SmallInput,
  103. )
  104. });
  105. stopped.store(true, Ordering::Relaxed);
  106. parallel_inserts.join().unwrap();
  107. }
  108. group.finish();
  109. }
  110. criterion_group!(bench, crossbeam);
  111. criterion_main!(bench);