File: bench_umask.rb

package info (click to toggle)
ruby-ffi 1.12.2%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 1,652 kB
  • sloc: ruby: 8,078; ansic: 7,157; xml: 151; sh: 51; makefile: 14
file content (62 lines) | stat: -rw-r--r-- 1,437 bytes parent folder | download
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
62
require_relative 'bench_helper'

module BenchUmask
  module Posix
    extend FFI::Library
    ffi_lib 'c'
    attach_function 'umask', [ :int ], :int
  end
  module NativeFile
    extend FFI::Library
    ffi_lib 'c'
    # Attaching the function to this module is about 10% faster than calling Posix.umask
    if FFI::Platform.windows?
      attach_function :_umask, '_umask', [ :int ], :int
    else
      attach_function :_umask, 'umask', [ :int ], :int
    end
    def self.umask(mask = nil)
      if mask
        _umask(mask)
      else
        old = _umask(0)
        _umask(old)
        old
      end
    end
  end
  puts "FFI umask=#{NativeFile.umask} File.umask=#{File.umask}"
  puts "Benchmark File.umask(0777) performance, #{ITER}x"
  10.times {
    puts Benchmark.measure {
      ITER.times { File.umask(0777) }
    }
  }
  puts "Benchmark FFI File.umask(0777) performance, #{ITER}x"

  10.times {
    puts Benchmark.measure {
      ITER.times { NativeFile.umask(0777) }
    }
  }
  puts "Benchmark FFI Posix.umask(0777) performance, #{ITER}x"

  10.times {
    puts Benchmark.measure {
      ITER.times { Posix.umask(0777) }
    }
  }
  puts "Benchmark File.umask() performance, #{ITER}x"
  10.times {
    puts Benchmark.measure {
      ITER.times { File.umask }
    }
  }
  puts "Benchmark FFI File.umask() performance, #{ITER}x"

  10.times {
    puts Benchmark.measure {
      ITER.times { NativeFile.umask }
    }
  }
end