Created
March 19, 2015 19:30
-
-
Save jordansissel/17872ec62351be544053 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "ffi" | |
class RUsage < FFI::Struct | |
module LibC | |
extend FFI::Library | |
ffi_lib ["libc.so.6", "libc"] | |
attach_function :getrusage, [:int, :pointer], :int | |
end | |
class TimeVal < FFI::Struct | |
layout :tv_sec, :long, | |
:tv_usec, :int32 | |
def to_f | |
return self[:tv_sec] + (self[:tv_usec] / 1_000_000.0) | |
end | |
end | |
layout :utime, TimeVal, | |
:stime, TimeVal, | |
:maxrss, :long, | |
:ixrss, :long, | |
:idrss, :long, | |
:isrss, :long, | |
:minflt, :long, | |
:majflt, :long, | |
:nswap, :long, | |
:inblock, :long, | |
:oublock, :long, | |
:msgsnd, :long, | |
:msgrcv, :long, | |
:nsignals, :long, | |
:nvcsw, :long, | |
:nivcsw, :long | |
def self.get | |
usage = RUsage.new | |
LibC.getrusage(0, usage) | |
return usage | |
end | |
def user | |
return self[:utime].to_f | |
end | |
def system | |
return self[:stime].to_f | |
end | |
end # class RUsage |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "logstash/plugin" | |
require "logstash/event" | |
require "/Users/jls/projects/experiments/ruby/ffi/rusage" | |
require "logstash/version" | |
module Tests | |
class NullOutput | |
def initialize | |
@plugin = LogStash::Plugin.lookup("output", "null").new | |
end | |
def run(obj) | |
@plugin.receive(obj) | |
end | |
end | |
class NullOutputPipelineWorker | |
def initialize | |
require "logstash/pipeline" | |
@pipeline = LogStash::Pipeline.new(<<-CONFIG) | |
output { null { } } | |
CONFIG | |
end | |
def run(event) | |
@pipeline.output(event) | |
end | |
end | |
class NullOutputPipelineQueue | |
def initialize | |
require "logstash/pipeline" | |
@pipeline = LogStash::Pipeline.new(<<-CONFIG) | |
output { null { } } | |
CONFIG | |
@queue = @pipeline.instance_eval { @filter_to_output } | |
@worker = Thread.new { @pipeline.outputworker } | |
end | |
def run(event) | |
@queue.push(event) | |
end | |
end | |
end | |
2.times do |i| | |
if i == 0 # Warmup | |
iterations = 500_000 | |
else | |
iterations = 5_000_000 | |
end | |
env = [iterations, RUBY_ENGINE + "-" + RUBY_VERSION, "jruby " + JRUBY_VERSION, "logstash " + LOGSTASH_VERSION].join(", ") | |
tests = [ Tests::NullOutput.new, Tests::NullOutputPipelineWorker.new, Tests::NullOutputPipelineQueue.new ] | |
tests.each do |test| | |
event = LogStash::Event.new | |
count = iterations | |
initial = RUsage.get | |
while count > 0 | |
test.run(event) | |
count -= 1 | |
end | |
after = RUsage.get | |
# print report only after first (warmup) run | |
if i > 0 | |
user = after.user.to_f - initial.user.to_f | |
system = after.system.to_f - initial.system.to_f | |
puts format("%32s | %s", test.class.name, env) | |
puts format("%35s [run %d] %0.4f, %0.4f (cpu time)", "", i, user, system) | |
#puts format("%35s [rate %d] %0.4f, %0.4f (events per sec)", "", i, iterations / user, iterations / system) | |
end | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment