Skip to content

Commit

Permalink
adding benchmark tests
Browse files Browse the repository at this point in the history
Note: erlang files from test/benchmarks/ have to be manually compiled and moved to ebin/ folder
  • Loading branch information
foobarto committed Aug 19, 2010
1 parent ebd30c9 commit 82adab9
Show file tree
Hide file tree
Showing 10 changed files with 128 additions and 5 deletions.
17 changes: 17 additions & 0 deletions test/benchmarks/doloops_benchmark.erl
@@ -0,0 +1,17 @@
-module (doloops_benchmark).

-export ([run/0]).

run() ->
% TODO: a better way to test loop?
List = lists:seq(0, 1000000),
loop(List).

loop([], Result) ->
Result;

loop([H|T], Result) ->
loop(T, [H | Result]).

loop([H|T]) ->
loop(T, [H]).
9 changes: 9 additions & 0 deletions test/benchmarks/doloops_benchmark.re
@@ -0,0 +1,9 @@
module DoloopsBenchmark
def run
range = 1..1000000
list = range.to_list()
list.map do |n|
n
end
end
end
17 changes: 17 additions & 0 deletions test/benchmarks/forloops_benchmark.erl
@@ -0,0 +1,17 @@
-module (forloops_benchmark).

-export ([run/0]).

run() ->
% TODO: a better way to test loop?
List = lists:seq(0, 1000000),
loop(List).

loop([], Result) ->
Result;

loop([H|T], Result) ->
loop(T, [H | Result]).

loop([H|T]) ->
loop(T, [H]).
7 changes: 7 additions & 0 deletions test/benchmarks/forloops_benchmark.re
@@ -0,0 +1,7 @@
module ForloopsBenchmark
def run
range = 1..1000000
#list = range.to_list() # Note: weird thing happens when you uncoment this, it affects the speed of the NEXT test drastically
[ n for n in range ]
end
end
12 changes: 12 additions & 0 deletions test/benchmarks/recursion_benchmark.erl
@@ -0,0 +1,12 @@
-module (recursion_benchmark).

-export ([run/0]).

run() ->
loop(1000000).

loop(0) ->
ok;

loop(N) ->
loop(N - 1).
14 changes: 14 additions & 0 deletions test/benchmarks/recursion_benchmark.re
@@ -0,0 +1,14 @@
module RecursionBenchmark
def loop(0)
:ok
end

def loop(n)
k = n - 1
loop(k)
end

def run
loop(1000000)
end
end
7 changes: 7 additions & 0 deletions test/benchmarks/sleep_benchmark.erl
@@ -0,0 +1,7 @@
-module (sleep_benchmark).

-export ([run/0]).


run() ->
timer:sleep(1000).
5 changes: 5 additions & 0 deletions test/benchmarks/sleep_benchmark.re
@@ -0,0 +1,5 @@
module SleepBenchmark
def run
erl.timer.sleep(1000)
end
end
39 changes: 34 additions & 5 deletions test/runner.re
Expand Up @@ -20,6 +20,10 @@ tests = [
("lib", ["json"])
]
benchmarks = [
"recursion", "sleep", "forloops", "doloops"
]
results = tests.map do |(group, modules)|
modules.map do |name|
try
Expand Down Expand Up @@ -52,12 +56,37 @@ errors.each do |(:error, test, ex)|
end
end
# run benchmarks in Reia and Erlang
bench_repeat = 10
Main.puts("Running benchmark tests #{bench_repeat} times each")
bench = benchmarks.map do |name|
Main.load("test/benchmarks/#{name}_benchmark.re")
mod = "#{name.capitalize()}Benchmark".to_module()
reia_bench_start = erl.now()
(1..bench_repeat).to_list().map do |n|
mod.run()
end
reia_bench_end = erl.now()
erl_bench_start = erl.now()
(1..bench_repeat).to_list().map do |n|
erl.apply("#{name}_benchmark".to_atom(), :run, [])
end
erl_bench_end = erl.now()
reia_duration = TestHelper.duration(reia_bench_start, reia_bench_end)
erl_duration = TestHelper.duration(erl_bench_start, erl_bench_end)
ratio = reia_duration / erl_duration
Main.puts("#{name} Reia:#{reia_duration}s Erlang:#{erl_duration}s that's #{ratio}x slower")
end
finished_at = erl.now()
started_seconds = (started_at[0] * 1000000 + started_at[1] + started_at[2] * 0.000001)
finished_seconds = (finished_at[0] * 1000000 + finished_at[1] + finished_at[2] * 0.000001)
duration = TestHelper.duration(started_at, finished_at)
Main.puts("\nFinished in #{duration} seconds\n")
Main.puts("#{results.size()} assertions, #{benchmarks.size()} benchmarks, #{failures.size()} failures, #{errors.size()} errors\n")
Main.puts("#{bench.size()} benchmarks")
duration = finished_seconds - started_seconds
Main.puts("Finished in #{duration} seconds\n")
Main.puts("#{results.size()} assertions, #{failures.size()} failures, #{errors.size()} errors")
System.halt(1) if failures.size() > 0 or errors.size() > 0
6 changes: 6 additions & 0 deletions test/test_helper.re
Expand Up @@ -16,4 +16,10 @@ module TestHelper
(:error, group, description, expected, actual)
end
end
def duration(started_at, finished_at)
started_seconds = (started_at[0] * 1000000 + started_at[1] + started_at[2] * 0.000001)
finished_seconds = (finished_at[0] * 1000000 + finished_at[1] + finished_at[2] * 0.000001)
finished_seconds - started_seconds
end
end

0 comments on commit 82adab9

Please sign in to comment.