summaryrefslogtreecommitdiff
path: root/bench/bench_getlogin.rb
blob: 12233afb3dad4a83d97c405e5ca9629b3eed7782 (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
require_relative 'bench_helper'
require 'etc'

module BenchGetlogin
  iter = ITER

  module Posix
    extend FFI::Library
    ffi_lib FFI::Library::LIBC
    attach_function :getlogin, [], :string
  end
  if Posix.getlogin != Etc.getlogin
    raise ArgumentError, "FFI getlogin returned incorrect value: " \
      "#{Posix.getlogin.inspect} (FFI) vs #{Etc.getlogin.inspect} (Etc)"
  end

  puts "Benchmark FFI getlogin(2) performance, #{ITER}x"

  10.times {
    puts Benchmark.measure {
      iter.times { Posix.getlogin }
    }
  }

  puts "Benchmark Etc.getlogin performance, #{ITER}x"
  10.times {
    puts Benchmark.measure {
      iter.times { Etc.getlogin }
    }
  }
end