Skip to content

Instantly share code, notes, and snippets.

@andrewvc andrewvc/jexec.rb
Last active Aug 8, 2017

Embed
What would you like to do?
JRuby method invocations from java performance tests
#!/usr/bin/env ruby
require './logstash-core/build/libs/logstash-core-6.0.0-beta1.jar'
ITERATIONS = 50_000_000
class InterfaceTest
include java.lang.Runnable
def initialize
@i = 1
end
def run
@i += 1
if @i == ITERATIONS
puts "DONE #{@i}"
end
end
end
class Test
def run_tests
proc_i = 1
simple_proc = proc do
proc_i += 1
if proc_i == ITERATIONS
puts "DONE #{proc_i}"
end
end
interface = InterfaceTest.new
#benchmark_runnable("Executor Proc", simple_proc)
#benchmark_runnable("Executor Runnable Class", interface)
benchmark_block("Direct Ruby Invocation") do
i = 1
ITERATIONS.times do
i += 1
if i == ITERATIONS
puts "DONE #{i}"
end
end
end
runnable = InterfaceTest.new
benchmark_block("Direct Runnable Invocation From Java") do
ITERATIONS.times do
org.logstash.Util.runRunnable(runnable)
end
end
runnableIter = InterfaceTest.new
benchmark_block("Direct Iteration and Runnable Invocation From Java") do
org.logstash.Util.runRunnableTimes(runnable, ITERATIONS)
end
runnable_proc_i = 1
runnable_proc = proc do
runnable_proc_i += 1
if runnable_proc_i == ITERATIONS
puts "DONE #{runnable_proc_i}"
end
end
benchmark_block("Direct Proc Runnable Invocation From Java") do
ITERATIONS.times do
org.logstash.Util.runRunnable(runnable_proc)
end
end
end
def benchmark_block(name)
executor = java.util.concurrent.Executors.newFixedThreadPool(1)
puts "-" * 80
puts name
start = java.lang.System.nanoTime
yield executor
finish = java.lang.System.nanoTime
executor.shutdown()
until executor.awaitTermination(1000,java.util.concurrent.TimeUnit::HOURS)
#noop
end
elapsed = (finish-start) / 1000000000.0
puts "#{ITERATIONS} iterations in #{elapsed} seconds (#{ITERATIONS/elapsed} per second)"
end
def benchmark_runnable(name, runnable)
benchmark_block(name) do |executor|
ITERATIONS.times do
executor.execute(runnable)
end
end
end
end
puts "WARMUP"
Test.new.run_tests
puts "\n----\n"
puts "REAL"
Test.new.run_tests
----
REAL
--------------------------------------------------------------------------------
Executor Proc
DONE 50000000
50000000 iterations in 18.661450623 seconds (2679320.1134308195 per second)
--------------------------------------------------------------------------------
Executor Runnable Class
DONE 50000000
50000000 iterations in 13.962164402 seconds (3581106.665155567 per second)
--------------------------------------------------------------------------------
Direct Ruby Invocation
DONE 50000000
50000000 iterations in 1.571114168 seconds (31824549.11195225 per second)
--------------------------------------------------------------------------------
Direct Runnable Invocation From Java
DONE 50000000
50000000 iterations in 20.850528508 seconds (2398020.7494891956 per second)
--------------------------------------------------------------------------------
Direct Iteration and Runnable Invocation From Java
50000000 iterations in 2.695628722 seconds (18548548.46735084 per second)
--------------------------------------------------------------------------------
Direct Proc Runnable Invocation From Java
DONE 50000000
50000000 iterations in 26.871085529 seconds (1860736.1413084208 per second)
class Util { // Added to org.logstash.Util
public static void runRunnable(Runnable runnable) {
runnable.run();
}
public static void runRunnableTimes(Runnable runnable, int times) {
for (int i = 0; i < times; i++) {
runnable.run();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.