Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

nicer logging

  • Loading branch information...
commit 67af5b0f072b78e82fd05f3d36dab6fbe8f865df 1 parent 7324b54
@tobi authored
Showing with 26 additions and 12 deletions.
  1. +1 −2  benchmark.rb
  2. +5 −0 client.rb
  3. +20 −10 server.rb
View
3  benchmark.rb
@@ -3,7 +3,7 @@
expectations = []
-# Make rand predictable
+# make this predictable
srand(0)
# Build a series of 5000 tests, every test has format [expectation, method, *args]
@@ -25,7 +25,6 @@
fork do
socket = MessagePipe.new(TcpTransport.new('localhost', 9191))
-
ms = Benchmark.realtime do
expectations.each do |expectation, method, *args|
raise 'fail' unless socket.call(method, *args) == expectation
View
5 client.rb
@@ -68,6 +68,11 @@ def test_simple_rpc
assert_equal 'hello', $socket.call(:hi)
end
+ def test_large_rpc
+ data = 'x' * 500_000
+ assert_equal data, $socket.call(:echo, data)
+ end
+
def test_rpc_with_params
assert_equal 3, $socket.call(:add, 1, 2)
assert_equal 2000000, $socket.call(:add, 1000000, 1000000)
View
30 server.rb
@@ -1,6 +1,7 @@
require 'rubygems'
require 'eventmachine'
require 'msgpack'
+require 'benchmark'
# Server that receives MessagePack RPC
class MessagePipeServer < EventMachine::Connection
@@ -18,27 +19,36 @@ def receive_data(data)
pac.feed(data)
pac.each do |msg|
begin
- result = receive_object(msg)
- send_data([RET_OK, result].to_msgpack)
- rescue => e
- send_data([RET_E, "#{e.class.name}: #{e.message}"].to_msgpack)
+
+ response = nil
+
+ secs = Benchmark.realtime do
+ response = begin
+ [RET_OK, receive_object(msg)]
+ rescue => e
+ [RET_E, "#{e.class.name}: #{e.message}"]
+ end
+ end
+
+ send_data(response.to_msgpack)
+
+ puts "#{object_id} - #{msg[1]}(#{msg[2].length} args) - [%.4f ms] [#{response[0] == RET_OK ? 'ok' : 'error'}]" % [secs||0]
+
end
end
end
def receive_object(msg)
- cmd, method, args = *msg
-
- puts "* #{method} with #{args.length} arg(s)"
-
+ cmd, method, args = *msg
if cmd != CMD_CALL
- close
+ unbind
raise 'Bad client'
end
+
if method and public_methods.include?(method)
- __send__(method, *args)
+ return __send__(method, *args)
else
raise NoMethodError, "no method #{method} found."
end
Please sign in to comment.
Something went wrong with that request. Please try again.