|
| 1 | +require 'benchmark' |
| 2 | +require_relative 'example_group_extensions' |
| 3 | +require_relative 'platform_helpers' |
| 4 | + |
| 5 | +require 'concurrent/atomics' |
| 6 | + |
| 7 | +def atomic_reference_test(clazz, opts = {}) |
| 8 | + threads = opts.fetch(:threads, 5) |
| 9 | + tests = opts.fetch(:tests, 100) |
| 10 | + |
| 11 | + atomic = Concurrent.const_get(clazz.to_s).new |
| 12 | + latch = Concurrent::CountDownLatch.new(threads) |
| 13 | + |
| 14 | + stats = Benchmark.measure do |
| 15 | + threads.times do |i| |
| 16 | + Thread.new do |
| 17 | + tests.times{ atomic.value = true } |
| 18 | + latch.count_down |
| 19 | + end |
| 20 | + end |
| 21 | + latch.wait |
| 22 | + end |
| 23 | + stats |
| 24 | +end |
| 25 | + |
| 26 | +describe Concurrent::Atomic do |
| 27 | + |
| 28 | + let!(:threads) { 10 } |
| 29 | + let!(:tests) { 1000 } |
| 30 | + |
| 31 | + describe Concurrent::MutexAtomic do |
| 32 | + |
| 33 | + specify 'is defined' do |
| 34 | + expect(defined?(Concurrent::MutexAtomic)).to be_truthy |
| 35 | + end |
| 36 | + |
| 37 | + specify 'runs the benchmarks' do |
| 38 | + stats = atomic_reference_test('MutexAtomic', threads: threads, tests: tests) |
| 39 | + expect(stats).to be_benchmark_results |
| 40 | + end |
| 41 | + end |
| 42 | + |
| 43 | + if jruby? |
| 44 | + |
| 45 | + describe Concurrent::JavaAtomic do |
| 46 | + |
| 47 | + specify 'Concurrent::JavaAtomic is defined' do |
| 48 | + expect(defined?(Concurrent::JavaAtomic)).to be_truthy |
| 49 | + end |
| 50 | + |
| 51 | + specify 'runs the benchmarks' do |
| 52 | + stats = atomic_reference_test('JavaAtomic', threads: threads, tests: tests) |
| 53 | + expect(stats).to be_benchmark_results |
| 54 | + end |
| 55 | + end |
| 56 | + |
| 57 | + else |
| 58 | + |
| 59 | + specify 'Concurrent::JavaAtomic is not defined' do |
| 60 | + expect(defined?(Concurrent::JavaAtomic)).to be_falsey |
| 61 | + end |
| 62 | + end |
| 63 | + |
| 64 | + if 'EXT' == ENV['TEST_PLATFORM'] |
| 65 | + |
| 66 | + describe Concurrent::CAtomic do |
| 67 | + |
| 68 | + specify 'Concurrent::CAtomic is defined' do |
| 69 | + expect(defined?(Concurrent::CAtomic)).to be_truthy |
| 70 | + end |
| 71 | + |
| 72 | + specify 'runs the benchmarks' do |
| 73 | + stats = atomic_reference_test('CAtomic', threads: threads, tests: tests) |
| 74 | + expect(stats).to be_benchmark_results |
| 75 | + end |
| 76 | + end |
| 77 | + |
| 78 | + else |
| 79 | + |
| 80 | + specify 'Concurrent::CAtomic is not defined' do |
| 81 | + expect(defined?(Concurrent::CAtomic)).to be_falsey |
| 82 | + end |
| 83 | + end |
| 84 | +end |
0 commit comments