Class: Parse::Middleware::Profiling

Inherits:
Faraday::Middleware
  • Object
show all
Defined in:
lib/parse/client/profiling.rb

Overview

Faraday middleware that profiles Parse API requests.

This middleware provides detailed timing information for HTTP requests including network time and overall request duration.

Examples:

Enable profiling

Parse.profiling_enabled = true

Access profile data in callbacks

Parse.on_request_complete do |profile|
  puts "Request to #{profile[:url]} took #{profile[:duration_ms]}ms"
end

Get recent profiles

Parse.recent_profiles.each do |profile|
  puts "#{profile[:method]} #{profile[:url]}: #{profile[:duration_ms]}ms"
end

Constant Summary collapse

MAX_PROFILES =

Maximum number of profiles to keep in memory

100

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.enabledBoolean

Returns Whether profiling is enabled.

Returns:

  • (Boolean)

    Whether profiling is enabled



32
33
34
# File 'lib/parse/client/profiling.rb', line 32

def enabled
  @enabled
end

Class Method Details

.add_profile(profile) ⇒ Object

Add a profile entry

Parameters:

  • profile (Hash)

    the profile data



62
63
64
65
66
67
68
69
# File 'lib/parse/client/profiling.rb', line 62

def add_profile(profile)
  profiles << profile
  # Keep only the most recent profiles
  profiles.shift while profiles.size > MAX_PROFILES

  # Execute callbacks
  callbacks.each { |cb| cb.call(profile) }
end

.callbacksArray<Proc>

Returns Callbacks to execute on request completion.

Returns:

  • (Array<Proc>)

    Callbacks to execute on request completion



45
46
47
# File 'lib/parse/client/profiling.rb', line 45

def callbacks
  @callbacks ||= []
end

.clear_callbacks!Object

Clear all registered callbacks



56
57
58
# File 'lib/parse/client/profiling.rb', line 56

def clear_callbacks!
  @callbacks = []
end

.clear_profiles!Object

Clear all stored profiles



40
41
42
# File 'lib/parse/client/profiling.rb', line 40

def clear_profiles!
  @profiles = []
end

.on_request_complete {|Hash| ... } ⇒ Object

Register a callback to be executed when a request completes

Yields:

  • (Hash)

    the profile data for the completed request



51
52
53
# File 'lib/parse/client/profiling.rb', line 51

def on_request_complete(&block)
  callbacks << block if block_given?
end

.profilesArray<Hash>

Returns Recent profile data.

Returns:



35
36
37
# File 'lib/parse/client/profiling.rb', line 35

def profiles
  @profiles ||= []
end

.statisticsHash

Get aggregate statistics for recent profiles

Returns:

  • (Hash)

    statistics including count, avg, min, max durations



73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/parse/client/profiling.rb', line 73

def statistics
  return {} if profiles.empty?

  durations = profiles.map { |p| p[:duration_ms] }
  {
    count: profiles.size,
    total_ms: durations.sum,
    avg_ms: (durations.sum.to_f / durations.size).round(2),
    min_ms: durations.min,
    max_ms: durations.max,
    by_method: profiles.group_by { |p| p[:method] }.transform_values(&:size),
    by_status: profiles.group_by { |p| p[:status] }.transform_values(&:size),
  }
end