Skip to content

Instantly share code, notes, and snippets.

@chriskottom
Last active September 1, 2017 04:22
Show Gist options
  • Save chriskottom/22ed11c5b332cd98bfce to your computer and use it in GitHub Desktop.
Save chriskottom/22ed11c5b332cd98bfce to your computer and use it in GitHub Desktop.
Minitest::Benchmark example
Sample code that shows how to use Minitest::Benchmark in a semi-realistic way
require "rake/testtask"
Rake::TestTask.new(:test) do |t|
t.libs = %w(lib test)
t.pattern = 'test/**/*_test.rb'
end
Rake::TestTask.new(:bench) do |t|
t.libs = %w(lib test)
t.pattern = 'test/**/*_benchmark.rb'
end
task :default => :test
require "test_helper"
class SortBenchmark < Minitest::Benchmark
include RandomArrayGenerator
def self.bench_range
bench_exp(10, 10_000) << 25_000
end
def setup
@sort_targets = {}
self.class.bench_range.each do |n|
@sort_targets[n] = random_numbers(n)
end
end
def bench_insertion_sort
assert_performance_power do |n|
Sorters::Insertion.new(@sort_targets[n]).sort
end
end
def bench_merge_sort
assert_performance_power do |n|
Sorters::Merge.new(@sort_targets[n]).sort
end
end
end
require "test_helper"
class SortTest < Minitest::Test
include RandomArrayGenerator
def setup
@numbers = random_numbers(25)
@strings = random_strings(25)
end
def test_insertion_sort_numbers
sorted = @numbers.sort
sorter = Sorters::Insertion.new(@numbers)
assert_equal sorted, sorter.sort
end
def test_insertion_sort_strings
sorted = @strings.sort
sorter = Sorters::Insertion.new(@strings)
assert_equal sorted, sorter.sort
end
def test_merge_sort_numbers
sorted = @numbers.sort
sorter = Sorters::Merge.new(@numbers)
assert_equal sorted, sorter.sort
end
def test_merge_sort_strings
sorted = @strings.sort
sorter = Sorters::Merge.new(@strings)
assert_equal sorted, sorter.sort
end
end
module Sorters
class Base
attr_reader :values
def initialize(values)
@values = values.dup
end
end
# Insertion Sort
# Move elements one by one into their proper position
# within the sorted portion of the collection.
class Insertion < Base
def sort
1.upto(values.length - 1) do |i|
value = values.delete_at(i)
j = i
j -= 1 while j > 0 && value < values[j-1]
values.insert(j, value)
end
values
end
end
# Merge Sort
# Divide the collection into two halves, and sort each half.
# Then combine the sorted halves and sort the result.
class Merge < Base
def sort
return values if values.length <= 1
mid = values.length/2 - 1
left_sorted = Merge.new(values[0..mid]).sort
right_sorted = Merge.new(values[mid+1..-1]).sort
merge_halves(left_sorted, right_sorted)
end
private
def merge_halves(left, right)
res = []
l = 0
r = 0
loop do
break if r >= right.length and l >= left.length
if r >= right.length or (l < left.length and left[l] < right[r])
res << left[l]
l += 1
else
res << right[r]
r += 1
end
end
return res
end
end
end
require "minitest/autorun"
require "minitest/benchmark"
require "sorters"
module RandomArrayGenerator
def random_numbers(size = 10)
result = []
size.times do
result << rand
end
result
end
def random_strings(size = 10, length = 8)
result = []
size.times do
result << random_string(length)
end
result
end
private
def random_string(length = 8)
(1..length).inject("") { |memo, n| memo << (rand(93) + 33) }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment