summaryrefslogtreecommitdiff
path: root/bench/bench_struct.rb
blob: 2d5f7b92c32924309b38df51ec0621805b9660f7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
require_relative 'bench_helper'

module BenchStruct
  module StructBench
    extend FFI::Library
    extend FFI::Library
    ffi_lib LIBTEST_PATH
    attach_function :bench_s32_v, [ :int ], :void
    begin
      attach_function :bench_struct_in, :ptr_ret_int32_t, [ :buffer_in, :int ], :void
    rescue FFI::NotFoundError
      # NetBSD uses #define instead of typedef for these
      attach_function :bench_struct_in, :ptr_ret___int32_t, [ :buffer_in, :int ], :void
    end
    begin
      attach_function :bench_struct_out, :ptr_ret_int32_t, [ :buffer_out, :int ], :void
    rescue FFI::NotFoundError
      # NetBSD uses #define instead of typedef for these
      attach_function :bench_struct_out, :ptr_ret___int32_t, [ :buffer_out, :int ], :void
    end
    begin
      attach_function :bench_struct_inout, :ptr_ret_int32_t, [ :buffer_inout, :int ], :void
    rescue FFI::NotFoundError
      # NetBSD uses #define instead of typedef for these
      attach_function :bench_struct_inout, :ptr_ret___int32_t, [ :buffer_inout, :int ], :void
    end
  end
  class TestStruct < FFI::Struct
    layout :i, :int, :p, :pointer
  end

  puts "Benchmark FFI call(Struct.alloc_in) performance, #{ITER}x"
  10.times {
    puts Benchmark.measure {
      i = 0; while i < ITER
        StructBench.bench_struct_in(TestStruct.alloc_in, 0)
        i += 1
      end
    }
  }

  puts "Benchmark FFI call(Struct.alloc_out) performance, #{ITER}x"
  10.times {
    puts Benchmark.measure {
      i = 0; while i < ITER
        StructBench.bench_struct_out(TestStruct.alloc_out, 0)
        i += 1
      end
    }
  }

  puts "Benchmark FFI call(Struct.alloc_inout) performance, #{ITER}x"
  10.times {
    puts Benchmark.measure {
      i = 0; while i < ITER
        StructBench.bench_struct_inout(TestStruct.alloc_inout, 0)
        i += 1
      end
    }
  }
end