Permalink
Browse files

Add timings for all costly logic

  • Loading branch information...
1 parent c9aa186 commit 4101cea74f2126dfe6d6c79763172dca9f7051a2 @fevrisn committed Mar 2, 2012
Showing with 87 additions and 37 deletions.
  1. +87 −37 lib/apns/core.rb
View
@@ -34,6 +34,8 @@ class PemFileError < RuntimeError;end
@@ssl = {}
@@sock = {}
+ @@timings = {}
+
class << self
attr_accessor :host, :port, :feedback_host, :feedback_port, :logger
def pem(stream = :_global, new_pem = nil)
@@ -49,6 +51,22 @@ def pass(stream = :_global, new_pass = nil)
def pass=(new_pass); @pass[:_global] = new_pass; end
end
+ def self.time_logged label
+ start = Time.now.to_f
+ (yield).tap do
+ @@timings[label] ||= 0
+ @@timings[label] += Time.now.to_f - start
+ end
+ end
+ def self.flush_timing_logg
+ if logger.present?
+ @@timings.each do |label, time|
+ logger.warn "#{label}: #{time}s"
+ end
+ true
+ end
+ end
+
# send one or many payloads
#
# Connection
@@ -69,39 +87,61 @@ def pass=(new_pass); @pass[:_global] = new_pass; end
# Send to a pem stream
def self.send_stream(stream, *payloads)
- payloads.flatten!
-
- # retain valid payloads only
- payloads.reject!{ |p| !(p.is_a?(APNS::Payload) && p.valid?) }
-
- return if (payloads.nil? || payloads.count < 1)
-
- # loop through each payloads
- payloads.each do |payload|
- retry_delay = 2
-
- # !ToDo! do a better job by using a select to poll the socket for a possible response from apple to inform us about an error in the sent payload
- #
- begin
- @@sock[stream], @@ssl[stream] = self.push_connection(stream) if @@ssl[stream].nil?
- @@ssl[stream].write(payload.to_ssl); @@ssl[stream].flush
- rescue PemPathError, PemFileError => e
- raise e
- rescue
- @@ssl[stream].close; @@sock[stream].close
- @@ssl[stream] = nil; @@sock[stream] = nil # cleanup
-
- retry_delay *= 2
- if retry_delay <= 8
- logger.warn "Failed to write payload, sleeping for #{retry_delay}s!" if logger.present?
- sleep retry_delay
- retry
- else
- raise
- end
- end # begin block
-
- end # each payloads
+ time_logged :send_stream do
+
+ time_logged :flatten_payloads do
+ payloads.flatten!
+ end
+
+ # retain valid payloads only
+ time_logged :validate_payload do
+ payloads.reject!{ |p| !(p.is_a?(APNS::Payload) && p.valid?) }
+ end
+
+ return if (payloads.nil? || payloads.count < 1)
+
+ # loop through each payloads
+ payloads.each do |payload|
+ retry_delay = 2
+
+ # !ToDo! do a better job by using a select to poll the socket for a possible response from apple to inform us about an error in the sent payload
+ #
+ begin
+ time_logged :connect do
+ if @@ssl[stream].nil?
+ @@sock[stream], @@ssl[stream] = self.push_connection(stream)
+ end
+ end
+ ssl_payload = time_logged :make_payload do
+ payload.to_ssl
+ end
+ time_logged :write do
+ @@ssl[stream].write(ssl_payload);
+ end
+ time_logged :flush do
+ @@ssl[stream].flush
+ end
+ rescue PemPathError, PemFileError => e
+ raise e
+ rescue
+ @@ssl[stream].close; @@sock[stream].close
+ @@ssl[stream] = nil; @@sock[stream] = nil # cleanup
+
+ retry_delay *= 2
+ if retry_delay <= 8
+ logger.warn "Failed to write payload, sleeping for #{retry_delay}!" if logger.present?
+ time_logged :sleep do
+ sleep retry_delay
+ end
+ retry
+ else
+ raise
+ end
+ end # begin block
+
+ end # each payloads
+ end
+ flush_timing_logg
end
def self.send_payloads(*payloads)
@@ -144,10 +184,20 @@ def self.ssl_context(stream = :_global)
end
def self.connect_to(aps_host, aps_port, stream = :_global)
- context = self.ssl_context(stream)
- sock = TCPSocket.new(aps_host, aps_port)
- ssl = OpenSSL::SSL::SSLSocket.new(sock, context)
- ssl.connect
+
+ context, sock, ssl = nil, nil, nil
+ context = time_logged :connect__ssl_context do
+ self.ssl_context(stream)
+ end
+ sock = time_logged :connect__tcp_socket_new do
+ TCPSocket.new(aps_host, aps_port)
+ end
+ ssl = time_logged :connect__ssl_socket_new do
+ OpenSSL::SSL::SSLSocket.new(sock, context)
+ end
+ time_logged :connect__ssl_connect do
+ ssl.connect
+ end
return sock, ssl
end

0 comments on commit 4101cea

Please sign in to comment.