Skip to content

Commit

Permalink
Merge PR #67: Split Ruby code to match other languages
Browse files Browse the repository at this point in the history
  • Loading branch information
smarr authored Jun 22, 2022
2 parents 5141912 + 1c01720 commit fbe00c8
Show file tree
Hide file tree
Showing 20 changed files with 96 additions and 141 deletions.
12 changes: 5 additions & 7 deletions benchmarks/Ruby/.rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ Layout/LineLength:
- 'json.rb'
- 'havlak.rb'
- 'harness.rb'
- 'harness-rbx.rb'

Metrics/CyclomaticComplexity:
Enabled: false
Expand Down Expand Up @@ -153,12 +152,7 @@ Layout/EmptyLineAfterGuardClause:

Style/PerlBackrefs:
Exclude:
- 'harness.rb'
- 'harness-rbx.rb'

Naming/FileName:
Exclude:
- 'harness-rbx.rb'
- 'run.rb'

Naming/MethodParameterName:
Enabled: false
Expand All @@ -180,3 +174,7 @@ Layout/SpaceInsideReferenceBrackets:

Style/OrAssignment:
Enabled: false

Style/RedundantCondition:
Exclude:
- 'cd.rb'
37 changes: 37 additions & 0 deletions benchmarks/Ruby/benchmark.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Copyright (c) 2015-2016 Stefan Marr <[email protected]>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the 'Software'), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

class Benchmark
def inner_benchmark_loop(inner_iterations)
inner_iterations.times do
return false unless verify_result(benchmark)
end
true
end

def benchmark
raise 'subclass_responsibility'
end

# noinspection RubyUnusedLocalVariable
def verify_result(_result)
raise 'subclass_responsibility'
end
end
1 change: 1 addition & 0 deletions benchmarks/Ruby/bounce.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

require_relative 'benchmark'
require_relative 'som'

class Bounce < Benchmark
Expand Down
2 changes: 2 additions & 0 deletions benchmarks/Ruby/cd.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

require_relative 'benchmark'
require_relative 'som'

## PREFER == nil over .nil?
Expand Down
2 changes: 2 additions & 0 deletions benchmarks/Ruby/deltablue.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
# the layout & logic from the original. (Ugh.)
#
# .. _`V8's source code`: (http://code.google.com/p/v8/source/browse/branches/bleeding_edge/benchmarks/deltablue.js)

require_relative 'benchmark'
require_relative 'som'

class DeltaBlue < Benchmark
Expand Down
78 changes: 1 addition & 77 deletions benchmarks/Ruby/harness.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,84 +17,8 @@
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
class Benchmark
def inner_benchmark_loop(inner_iterations)
inner_iterations.times do
return false unless verify_result(benchmark)
end
true
end

def benchmark
raise 'subclass_responsibility'
end

# noinspection RubyUnusedLocalVariable
def verify_result(_result)
raise 'subclass_responsibility'
end
end

class Run
attr_accessor :name, :benchmark_suite, :num_iterations, :inner_iterations

def initialize(name)
@name = name
@benchmark_suite = load_benchmark_suite(name)
@total = 0
@num_iterations = 1
@inner_iterations = 1
end

def load_benchmark_suite(benchmark_name)
if File.exist?("#{File.dirname(__FILE__)}/#{benchmark_name.downcase}.rb")
benchmark_file = benchmark_name.downcase
else
# fallback, for benchmark files that use
# Ruby naming conventions instead of classic names
benchmark_file = benchmark_name.gsub(/([a-z])([A-Z])/) { "#{$1}-#{$2}" }.downcase
end
unless require_relative(benchmark_file)
raise "failed loading #{benchmark_file}"
end
Object.const_get(benchmark_name)
end

def run_benchmark
puts "Starting #{@name} benchmark ..."
do_runs(@benchmark_suite.new)
report_benchmark
puts ''
end

def measure(bench)
start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC, :nanosecond)
unless bench.inner_benchmark_loop(@inner_iterations)
raise 'Benchmark failed with incorrect result'
end
end_time = Process.clock_gettime(Process::CLOCK_MONOTONIC, :nanosecond)

run_time = (end_time - start_time) / 1000
print_result(run_time)
@total += run_time
end

def do_runs(bench)
@num_iterations.times { measure(bench) }
end

def report_benchmark
puts "#{@name}: iterations=#{@num_iterations} average: #{@total / @num_iterations}us total: #{@total}us\n"
end

def print_result(run_time)
puts "#{@name}: iterations=1 runtime: #{run_time}us"
end

def print_total
puts "Total Runtime: #{@total}us"
end
end
require_relative 'run'

def process_arguments(args)
run = Run.new(args[0])
Expand Down
2 changes: 2 additions & 0 deletions benchmarks/Ruby/havlak.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

require_relative 'benchmark'
require_relative 'som'

class Havlak < Benchmark
Expand Down
1 change: 1 addition & 0 deletions benchmarks/Ruby/json.rb

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions benchmarks/Ruby/list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

require_relative 'benchmark'

class List < Benchmark
def benchmark
result = tail(make_list(15),
Expand Down
2 changes: 2 additions & 0 deletions benchmarks/Ruby/mandelbrot.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@

# http://benchmarksgame.alioth.debian.org/u64q/program.php?test=mandelbrot&lang=yarv&id=3

require_relative 'benchmark'

class Mandelbrot < Benchmark
def inner_benchmark_loop(inner_iterations)
verify_result(mandelbrot(inner_iterations), inner_iterations)
Expand Down
3 changes: 3 additions & 0 deletions benchmarks/Ruby/nbody.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
# modified slightly by Chad Whipkey
#
# Based on nbody.java ported to SOM and Ruby by Stefan Marr.

require_relative 'benchmark'

PI = 3.141592653589793
SOLAR_MASS = 4.0 * PI * PI
DAYS_PER_YEAR = 365.24
Expand Down
2 changes: 2 additions & 0 deletions benchmarks/Ruby/permute.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

require_relative 'benchmark'

class Permute < Benchmark
def initialize
@count = 0
Expand Down
2 changes: 2 additions & 0 deletions benchmarks/Ruby/queens.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

require_relative 'benchmark'

class Queens < Benchmark
def initialize
@free_maxs = nil
Expand Down
2 changes: 2 additions & 0 deletions benchmarks/Ruby/richards.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
# Derived from http://pws.prserv.net/dlissett/ben/bench1.htm
# Licensed CC BY-NC-SA 1.0

require_relative 'benchmark'

NO_TASK = nil
NO_WORK = nil

Expand Down
80 changes: 25 additions & 55 deletions benchmarks/Ruby/harness-rbx.rb → benchmarks/Ruby/run.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,6 @@
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
class Benchmark
def inner_benchmark_loop(inner_iterations)
inner_iterations.times do
return false unless verify_result(benchmark)
end
true
end

def benchmark
raise 'subclass_responsibility'
end

# noinspection RubyUnusedLocalVariable
def verify_result(_result)
raise 'subclass_responsibility'
end
end

class Run
attr_accessor :name, :benchmark_suite, :num_iterations, :inner_iterations
Expand All @@ -55,28 +38,43 @@ def load_benchmark_suite(benchmark_name)
benchmark_file = benchmark_name.gsub(/([a-z])([A-Z])/) { "#{$1}-#{$2}" }.downcase
end
unless require_relative(benchmark_file)
raise "failed loading #{benchmark_file}"
raise "#{benchmark_file} was already loaded"
end
Object.const_get(benchmark_name)
end

def run_benchmark
@total = 0
puts "Starting #{@name} benchmark ..."
do_runs(@benchmark_suite.new)
report_benchmark
puts ''
end

def measure(bench)
start_time = Time.now
unless bench.inner_benchmark_loop(@inner_iterations)
raise 'Benchmark failed with incorrect result'
if RUBY_ENGINE != 'rbx' # not Rubinius
def measure(bench)
start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC, :nanosecond)
unless bench.inner_benchmark_loop(@inner_iterations)
raise 'Benchmark failed with incorrect result'
end
end_time = Process.clock_gettime(Process::CLOCK_MONOTONIC, :nanosecond)

run_time = (end_time - start_time) / 1000
print_result(run_time)
@total += run_time
end
else
def measure(bench)
start_time = Time.now
unless bench.inner_benchmark_loop(@inner_iterations)
raise 'Benchmark failed with incorrect result'
end
end_time = Time.now

run_time = ((end_time - start_time) * 1_000_000).to_i
print_result(run_time)
@total += run_time
end
end_time = Time.now

run_time = ((end_time - start_time) * 1_000_000).to_i
print_result(run_time)
@total += run_time
end

def do_runs(bench)
Expand All @@ -95,31 +93,3 @@ def print_total
puts "Total Runtime: #{@total}us"
end
end

def process_arguments(args)
run = Run.new(args[0])

if args.size > 1
run.num_iterations = Integer(args[1])
run.inner_iterations = Integer(args[2]) if args.size > 2
end
run
end

def print_usage
puts './harness.rb [benchmark] [num-iterations [inner-iter]]'
puts ''
puts ' benchmark - benchmark class name '
puts ' num-iterations - number of times to execute benchmark, default: 1'
puts ' inner-iter - number of times the benchmark is executed in an inner loop, '
puts ' which is measured in total, default: 1'
end

if ARGV.size < 1
print_usage
exit 1
end

run = process_arguments(ARGV)
run.run_benchmark
run.print_total
2 changes: 2 additions & 0 deletions benchmarks/Ruby/sieve.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

require_relative 'benchmark'

class Sieve < Benchmark
def benchmark
flags = Array.new(5000, true)
Expand Down
1 change: 1 addition & 0 deletions benchmarks/Ruby/storage.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

require_relative 'benchmark'
require_relative 'som'

class Storage < Benchmark
Expand Down
2 changes: 2 additions & 0 deletions benchmarks/Ruby/towers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

require_relative 'benchmark'

class TowersDisk
attr_accessor :next
attr_reader :size
Expand Down
2 changes: 1 addition & 1 deletion rebench.conf
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ benchmark_suites:
ruby-rbx:
gauge_adapter: RebenchLog
location: benchmarks/Ruby
command: "harness-rbx.rb %(benchmark)s %(variable)s "
command: "harness.rb %(benchmark)s %(variable)s "
variable_values: [*VERY_SLOW_VM] ## the number iterations measured
max_invocation_time: 6000
benchmarks: *BENCHMARKS
Expand Down
2 changes: 1 addition & 1 deletion test.conf
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ benchmark_suites:
test-rbx:
gauge_adapter: RebenchLog
location: benchmarks/Ruby
command: "harness-rbx.rb %(benchmark)s 1 "
command: "harness.rb %(benchmark)s 1 "
benchmarks: *BENCHMARKS
test-js:
gauge_adapter: RebenchLog
Expand Down

0 comments on commit fbe00c8

Please sign in to comment.