Permalink
Browse files

adding benchmark tests

Note: erlang files from test/benchmarks/ have to be manually compiled and moved to ebin/ folder
  • Loading branch information...
1 parent ebd30c9 commit 82adab9aa8f4a0b7a9f0360f888651c98502b366 @foobarto foobarto committed Aug 19, 2010
View
17 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]).
View
9 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
View
17 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]).
View
7 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
View
12 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).
View
14 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
View
7 test/benchmarks/sleep_benchmark.erl
@@ -0,0 +1,7 @@
+-module (sleep_benchmark).
+
+-export ([run/0]).
+
+
+run() ->
+ timer:sleep(1000).
View
5 test/benchmarks/sleep_benchmark.re
@@ -0,0 +1,5 @@
+module SleepBenchmark
+ def run
+ erl.timer.sleep(1000)
+ end
+end
View
39 test/runner.re
@@ -20,6 +20,10 @@ tests = [
("lib", ["json"])
]
+benchmarks = [
+ "recursion", "sleep", "forloops", "doloops"
+]
+
results = tests.map do |(group, modules)|
modules.map do |name|
try
@@ -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
View
6 test/test_helper.re
@@ -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.