crossbeam.rs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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, BenchmarkId, Criterion, BatchSize};
  19. use easy_parallel::Parallel;
  20. use std::{collections::HashMap, sync::{Arc, Mutex, atomic::{AtomicBool, Ordering}}};
  21. use rand::{rngs::OsRng, Rng};
  22. use crossbeam_skiplist::SkipMap;
  23. fn crossbeam(c: &mut Criterion) {
  24. let mut group = c.benchmark_group("crossbeam");
  25. for k in 1..10 {
  26. let stopped = Arc::new(AtomicBool::new(false));
  27. let map = Arc::new(SkipMap::new());
  28. let stopped2 = stopped.clone();
  29. let map2 = map.clone();
  30. // Start n threads all doing continuous inserts until we tell them to stop
  31. let parallel_inserts = std::thread::spawn(move || {
  32. Parallel::new()
  33. .each(0..k, |_| {
  34. let stopped = stopped2.clone();
  35. let map = map2.clone();
  36. while !stopped.load(Ordering::Relaxed) {
  37. let key: usize = OsRng.gen();
  38. let val: usize = OsRng.gen();
  39. map.insert(key, val);
  40. }
  41. })
  42. .run();
  43. });
  44. group.bench_with_input(BenchmarkId::from_parameter(k), &k, |b, &_| {
  45. b.iter_batched(
  46. || {
  47. let key: usize = OsRng.gen();
  48. let val: usize = OsRng.gen();
  49. (key, val)
  50. },
  51. |(key, val)| {
  52. // Do 10k inserts
  53. map.insert(key, val);
  54. },
  55. BatchSize::SmallInput
  56. )
  57. });
  58. stopped.store(true, Ordering::Relaxed);
  59. parallel_inserts.join().unwrap();
  60. }
  61. group.finish();
  62. // Now try normal Mutex hashmap
  63. // This is not an async Mutex, but async Mutexes are always slower than sync ones anyway
  64. // since they just implement an async interface on top of sync Mutexes.
  65. let mut group = c.benchmark_group("mutex_hashmap");
  66. for k in 1..10 {
  67. let stopped = Arc::new(AtomicBool::new(false));
  68. let map = Arc::new(Mutex::new(HashMap::new()));
  69. let stopped2 = stopped.clone();
  70. let map2 = map.clone();
  71. // Start n threads all doing continuous inserts until we tell them to stop
  72. let parallel_inserts = std::thread::spawn(move || {
  73. Parallel::new()
  74. .each(0..k, |_| {
  75. let stopped = stopped2.clone();
  76. let map = map2.clone();
  77. while !stopped.load(Ordering::Relaxed) {
  78. let key: usize = OsRng.gen();
  79. let val: usize = OsRng.gen();
  80. map.lock().unwrap().insert(key, val);
  81. }
  82. })
  83. .run();
  84. });
  85. group.bench_with_input(BenchmarkId::from_parameter(k), &k, |b, &_| {
  86. b.iter_batched(
  87. || {
  88. let key: usize = OsRng.gen();
  89. let val: usize = OsRng.gen();
  90. (key, val)
  91. },
  92. |(key, val)| {
  93. // Do 10k inserts
  94. map.lock().unwrap().insert(key, val);
  95. },
  96. BatchSize::SmallInput
  97. )
  98. });
  99. stopped.store(true, Ordering::Relaxed);
  100. parallel_inserts.join().unwrap();
  101. }
  102. group.finish();
  103. }
  104. criterion_group!(bench, crossbeam);
  105. criterion_main!(bench);