Sha256: 8a16b7ef67b5b38c2101b78f782147fa43952f23f67c41a260c0f088a1fb94fb

Contents?: true

Size: 1.62 KB

Versions: 41

Compression:

Stored size: 1.62 KB

Contents

require 'date'
require 'protobuf/logger'

module Protobuf
  module Rpc
    class Stat
      attr_accessor :mode, :start_time, :end_time, :request_size, :dispatcher
      attr_accessor :response_size, :client, :server, :service, :method_name

      MODES = [:SERVER, :CLIENT].freeze

      def initialize(mode = :SERVER)
        @mode = mode
        start
      end

      def client=(peer)
        @client = {:port => peer[0], :ip => peer[1]}
      end

      def client
        @client ? "#{@client[:ip]}:#{@client[:port]}" : nil
      end

      def method_name
        @method_name ||= @dispatcher.try(:service).try(:rpc)
      end

      def server=(peer)
        @server = {:port => peer[0], :ip => peer[1]}
      end

      def server
        @server ? "#{@server[:ip]}:#{@server[:port]}" : nil
      end

      def service
        @service ||= @dispatcher.try(:service).class.name
      end

      def sizes
        "#{@request_size || 0}B/#{@response_size || 0}B"
      end

      def start
        @start_time ||= Time.now
      end

      def stop
        start if !@start_time
        @end_time ||= Time.now
      end

      def rpc
        service && method_name ? "#{service}##{method_name}" : nil
      end

      def elapsed_time
        (start_time && end_time ? "#{(end_time - start_time).round(4)}s" : nil)
      end

      def server?
        @mode == :SERVER
      end

      def client?
        @mode == :CLIENT
      end

      def to_s
        [
          server? ? "[SRV]" : "[CLT]",
          rpc,
          elapsed_time,
          sizes,
          server? ? server : client
        ].compact.join(' - ')
      end

    end
  end
end

Version data entries

41 entries across 41 versions & 1 rubygems

Version Path
protobuf-2.5.0-java lib/protobuf/rpc/stat.rb
protobuf-2.5.0 lib/protobuf/rpc/stat.rb
protobuf-2.4.8-java lib/protobuf/rpc/stat.rb
protobuf-2.4.8 lib/protobuf/rpc/stat.rb
protobuf-2.4.7-java lib/protobuf/rpc/stat.rb
protobuf-2.4.7 lib/protobuf/rpc/stat.rb
protobuf-2.4.6 lib/protobuf/rpc/stat.rb
protobuf-2.4.5-java lib/protobuf/rpc/stat.rb
protobuf-2.4.5 lib/protobuf/rpc/stat.rb
protobuf-2.4.4-java lib/protobuf/rpc/stat.rb
protobuf-2.4.4 lib/protobuf/rpc/stat.rb
protobuf-2.4.3-java lib/protobuf/rpc/stat.rb
protobuf-2.4.3 lib/protobuf/rpc/stat.rb
protobuf-2.4.3.rc1 lib/protobuf/rpc/stat.rb
protobuf-2.4.2-java lib/protobuf/rpc/stat.rb
protobuf-2.4.2 lib/protobuf/rpc/stat.rb
protobuf-2.4.1-java lib/protobuf/rpc/stat.rb
protobuf-2.4.1 lib/protobuf/rpc/stat.rb
protobuf-2.4.0-java lib/protobuf/rpc/stat.rb
protobuf-2.4.0 lib/protobuf/rpc/stat.rb