diff options
author | Wayne Meissner <wmeissner@gmail.com> | 2011-08-12 17:44:35 +1000 |
---|---|---|
committer | Wayne Meissner <wmeissner@gmail.com> | 2011-08-18 19:19:17 +1000 |
commit | 251392913e4fcca1a299df9b14978d1c2bd15ef5 (patch) | |
tree | 1d3ae1f589e225fb696e679e7af30bf7d5131bd4 | |
parent | 6711fb945552b59654ff26296cdec87ff4f3af8e (diff) | |
download | ffi-251392913e4fcca1a299df9b14978d1c2bd15ef5.tar.gz |
Add non-ruby-thread callback benchmarks
-rw-r--r-- | bench/bench_closure_VrV.rb | 17 | ||||
-rw-r--r-- | libtest/ClosureTest.c | 41 | ||||
-rw-r--r-- | libtest/NumberTest.c | 6 |
3 files changed, 63 insertions, 1 deletions
diff --git a/bench/bench_closure_VrV.rb b/bench/bench_closure_VrV.rb index f7501fc..20a92a4 100644 --- a/bench/bench_closure_VrV.rb +++ b/bench/bench_closure_VrV.rb @@ -4,9 +4,15 @@ module LibTest extend FFI::Library ffi_lib LIBTEST_PATH callback :closureVrV, [ ], :void + attach_function :ffi_bench, :testClosureVrV, [ :closureVrV ], :void - def self.rb_bench(&block); yield; end + attach_function :threaded_bench, :testThreadedClosureVrV, [ :closureVrV, :int ], :void + + def self.rb_bench(&block) + yield + end end + unless RUBY_PLATFORM == "java" && JRUBY_VERSION < "1.3.0" require 'dl' require 'dl/import' @@ -44,6 +50,15 @@ puts "Benchmark [ ], :void pre-allocated closure performance, #{ITER}x calls" } } +puts "Benchmark [ ], :void non-ruby thread closure performance, #{ITER}x calls" +10.times { + fn = FFI::Function.new(:void, []) {} + puts Benchmark.measure { + LibTest.threaded_bench(fn, ITER) + } +} + + #unless RUBY_PLATFORM == "java" && JRUBY_VERSION < "1.3.0" #puts "Benchmark DL void bench() performance, #{ITER}x calls" #10.times { diff --git a/libtest/ClosureTest.c b/libtest/ClosureTest.c index 6c4c9e6..914e8dd 100644 --- a/libtest/ClosureTest.c +++ b/libtest/ClosureTest.c @@ -20,6 +20,9 @@ #include <stdlib.h> #include <stdbool.h> +#ifndef _WIN32 +# include <pthread.h> +#endif #define R(T, rtype) rtype testClosureVr##T(rtype (*closure)(void)) { \ return closure != NULL ? (*closure)() : (rtype) 0; \ @@ -65,6 +68,44 @@ void testOptionalClosureBrV(void (*closure)(char), char a1) } } + +struct ThreadVrV { + void (*closure)(void); + int count; +}; +static void * +threadVrV(void *arg) +{ + struct ThreadVrV* t = (struct ThreadVrV *) arg; + + int i; + for (i = 0; i < t->count; i++) { + (*t->closure)(); + } + + return NULL; +} + +void testThreadedClosureVrV(void (*closure)(void), int n) +{ +#ifndef _WIN32 + pthread_t t; + struct ThreadVrV arg; + + arg.closure = closure; + arg.count = n; + + pthread_create(&t, NULL, threadVrV, &arg); + + pthread_join(t, NULL); +#else + int i; + for (i = 0; i < t->count; i++) { + (*closure)(); + } +#endif +} + struct s8f32s32 { char s8; float f32; diff --git a/libtest/NumberTest.c b/libtest/NumberTest.c index 1c4904d..af8565c 100644 --- a/libtest/NumberTest.c +++ b/libtest/NumberTest.c @@ -136,3 +136,9 @@ TEST2(u64) TEST3(s64) +void +foo6(intptr_t i1, intptr_t i2, intptr_t i3, intptr_t i4, intptr_t i5, intptr_t i6) { } + +void +foo5(intptr_t i1, intptr_t i2, intptr_t i3, intptr_t i4, intptr_t i5) { } + |