Skip to content

Commit 3953f40

Browse files
committed
Added simple benchmarks for Atomic, AtomicBoolean, and AtomicFixnum.
1 parent f771124 commit 3953f40

File tree

5 files changed

+130
-0
lines changed

5 files changed

+130
-0
lines changed

examples/benchmark_atomic_boolean.rb

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#!/usr/bin/env ruby
2+
3+
$:.push File.join(File.dirname(__FILE__), '../lib')
4+
5+
require 'concurrent'
6+
require 'benchmark'
7+
require 'rbconfig'
8+
9+
def atomic_test(clazz, opts = {})
10+
threads = opts.fetch(:threads, 5)
11+
tests = opts.fetch(:tests, 100)
12+
13+
atomic = clazz.new
14+
latch = Concurrent::CountDownLatch.new(threads)
15+
16+
print "Testing with #{clazz}...\n"
17+
stats = Benchmark.measure do
18+
threads.times do |i|
19+
Thread.new do
20+
tests.times{ atomic.value = true }
21+
latch.count_down
22+
end
23+
end
24+
latch.wait
25+
end
26+
print stats
27+
end
28+
29+
puts "Testing with #{RbConfig::CONFIG['ruby_install_name']} #{RUBY_VERSION}"
30+
31+
atomic_test(Concurrent::MutexAtomicBoolean, threads: 10, tests: 1_000_000)
32+
33+
if defined? Concurrent::CAtomicBoolean
34+
atomic_test(Concurrent::CAtomicBoolean, threads: 10, tests: 1_000_000)
35+
elsif RUBY_PLATFORM == 'java'
36+
atomic_test(Concurrent::JavaAtomicBoolean, threads: 10, tests: 1_000_000)
37+
end

examples/benchmark_atomic_fixnum.rb

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#!/usr/bin/env ruby
2+
3+
$:.push File.join(File.dirname(__FILE__), '../lib')
4+
5+
require 'concurrent'
6+
require 'benchmark'
7+
require 'rbconfig'
8+
9+
def atomic_test(clazz, opts = {})
10+
threads = opts.fetch(:threads, 5)
11+
tests = opts.fetch(:tests, 100)
12+
13+
num = clazz.new
14+
latch = Concurrent::CountDownLatch.new(threads)
15+
16+
print "Testing with #{clazz}...\n"
17+
stats = Benchmark.measure do
18+
threads.times do |i|
19+
Thread.new do
20+
tests.times{ num.up }
21+
latch.count_down
22+
end
23+
end
24+
latch.wait
25+
end
26+
print stats
27+
end
28+
29+
puts "Testing with #{RbConfig::CONFIG['ruby_install_name']} #{RUBY_VERSION}"
30+
31+
atomic_test(Concurrent::MutexAtomicFixnum, threads: 10, tests: 1_000_000)
32+
33+
if defined? Concurrent::CAtomicFixnum
34+
atomic_test(Concurrent::CAtomicFixnum, threads: 10, tests: 1_000_000)
35+
elsif RUBY_PLATFORM == 'java'
36+
atomic_test(Concurrent::JavaAtomicFixnum, threads: 10, tests: 1_000_000)
37+
end

lib/concurrent/atomic.rb

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,38 @@
2020
#
2121
# An object reference that may be updated atomically.
2222
#
23+
# Testing with ruby 2.1.2
24+
#
25+
# *** Sequential updates ***
26+
# user system total real
27+
# no lock 0.000000 0.000000 0.000000 ( 0.005502)
28+
# mutex 0.030000 0.000000 0.030000 ( 0.025158)
29+
# MutexAtomic 0.100000 0.000000 0.100000 ( 0.103096)
30+
# CAtomic 0.040000 0.000000 0.040000 ( 0.034012)
31+
#
32+
# *** Parallel updates ***
33+
# user system total real
34+
# no lock 0.010000 0.000000 0.010000 ( 0.009387)
35+
# mutex 0.030000 0.010000 0.040000 ( 0.032545)
36+
# MutexAtomic 0.830000 2.280000 3.110000 ( 2.146622)
37+
# CAtomic 0.040000 0.000000 0.040000 ( 0.038332)
38+
#
39+
# Testing with jruby 1.9.3
40+
#
41+
# *** Sequential updates ***
42+
# user system total real
43+
# no lock 0.170000 0.000000 0.170000 ( 0.051000)
44+
# mutex 0.370000 0.010000 0.380000 ( 0.121000)
45+
# MutexAtomic 1.530000 0.020000 1.550000 ( 0.471000)
46+
# JavaAtomic 0.370000 0.010000 0.380000 ( 0.112000)
47+
#
48+
# *** Parallel updates ***
49+
# user system total real
50+
# no lock 0.390000 0.000000 0.390000 ( 0.105000)
51+
# mutex 0.480000 0.040000 0.520000 ( 0.145000)
52+
# MutexAtomic 1.600000 0.180000 1.780000 ( 0.511000)
53+
# JavaAtomic 0.460000 0.010000 0.470000 ( 0.131000)
54+
#
2355
# @see http://docs.oracle.com/javase/8/docs/api/java/util/concurrent/atomic/AtomicReference.html
2456
# @see http://docs.oracle.com/javase/8/docs/api/java/util/concurrent/atomic/package-summary.html
2557
class Concurrent::Atomic < Concurrent::JavaAtomic

lib/concurrent/atomic/atomic_boolean.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,18 @@ module Concurrent
66
# boolean and thread-safe and guaranteed to succeed. Reads and writes may block
77
# briefly but no explicit locking is required.
88
#
9+
# Testing with ruby 2.1.2
10+
# Testing with Concurrent::MutexAtomicBoolean...
11+
# 2.790000 0.000000 2.790000 ( 2.791454)
12+
# Testing with Concurrent::CAtomicBoolean...
13+
# 0.740000 0.000000 0.740000 ( 0.740206)
14+
#
15+
# Testing with jruby 1.9.3
16+
# Testing with Concurrent::MutexAtomicBoolean...
17+
# 5.240000 2.520000 7.760000 ( 3.683000)
18+
# Testing with Concurrent::JavaAtomicBoolean...
19+
# 3.340000 0.010000 3.350000 ( 0.855000)
20+
#
921
# @see http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/atomic/AtomicBoolean.html java.util.concurrent.atomic.AtomicBoolean
1022
class MutexAtomicBoolean
1123

lib/concurrent/atomic/atomic_fixnum.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,18 @@ module Concurrent
66
# fixnum and thread-safe and guaranteed to succeed. Reads and writes may block
77
# briefly but no explicit locking is required.
88
#
9+
# Testing with ruby 2.1.2
10+
# Testing with Concurrent::MutexAtomicFixnum...
11+
# 3.130000 0.000000 3.130000 ( 3.136505)
12+
# Testing with Concurrent::CAtomicFixnum...
13+
# 0.790000 0.000000 0.790000 ( 0.785550)
14+
#
15+
# Testing with jruby 1.9.3
16+
# Testing with Concurrent::MutexAtomicFixnum...
17+
# 5.460000 2.460000 7.920000 ( 3.715000)
18+
# Testing with Concurrent::JavaAtomicFixnum...
19+
# 4.520000 0.030000 4.550000 ( 1.187000)
20+
#
921
# @see http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/atomic/AtomicLong.html java.util.concurrent.atomic.AtomicLong
1022
class MutexAtomicFixnum
1123

0 commit comments

Comments
 (0)