Skip to content

Commit

Permalink
Add Minitest benchmarking file for sequential vs parallel execution
Browse files Browse the repository at this point in the history
Co-authored-by: KOTP <[email protected]>
  • Loading branch information
ccadden and kotp committed Mar 4, 2024
1 parent 2fe8ebc commit 63abc4d
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
# Instructions Append

## Benchmarking

If you are solving this exercise locally, there is an included `parallel_letter_frequency_benchmark_test.rb` file which will compare your parallel solution to a sequential one.
You can run it via `ruby parallel_letter_frequency_benchmark_test.rb`.
The output will show execution times of each implementation over a range of large text counts in a tabular format.
Feel free to change the test cases.
You may want to investigate what performance impact length of text vs number of texts has on the execution time for each implementation.

**Note:** For smaller sets of text, the sequential count _may_ be faster due to processing overhead costs.

## Further Reading

- [Ruby `Thread` Documentation](https://docs.ruby-lang.org/en/master/Thread.html)
- [Ruby `Thread::Queue` Documentation](https://docs.ruby-lang.org/en/master/Thread/Queue.html)
- [Ruby `Fiber` Documentation](https://docs.ruby-lang.org/en/master/Fiber.html)
- [Ruby `Ractor` Documentation](https://docs.ruby-lang.org/en/master/Ractor.html)
- [`Minitest::Benchmark` Documentation](https://ruby-doc.org/3.0.6/gems/minitest/Minitest/Benchmark.html)
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
require 'minitest/autorun'
require 'minitest/benchmark'
require_relative 'parallel_letter_frequency'

class ParallelLetterFrequencyBenchmarkTest < Minitest::Benchmark
def self.bench_range
bench_exp(1, 100_000)
end

def setup
@strings = {}
self.class.bench_range.each do |n|
@strings[n] = Array.new(10, 'a' * n)
end
end

def bench_sequential
assert_performance ->(_, _) { true } do |n|
sequential_letter_frequency(@strings[n])
end
end

def bench_parallel
assert_performance ->(_, _) { true } do |n|
ParallelLetterFrequency.count(@strings[n])
end
end

def sequential_letter_frequency(texts)
tally = Hash.new(0)
texts.each do |text|
text.each_grapheme_cluster do |cluster|
tally[cluster] += 1
end
end

tally
end
end

0 comments on commit 63abc4d

Please sign in to comment.