Class: LdapSearchProfiler

Inherits:
Object
  • Object
show all
Defined in:
lib/ldapmodel/profiler.rb

Overview

Simple class for profiling LDAP Queries

Set LDAP_PROFILER=1 environment variable to enable query profiling.

Query time are printed to stdout after.

Defined Under Namespace

Classes: Timer

Instance Method Summary (collapse)

Constructor Details

- (LdapSearchProfiler) initialize(key)

Returns a new instance of LdapSearchProfiler



11
12
13
14
# File 'lib/ldapmodel/profiler.rb', line 11

def initialize(key)
  @key = key
  reset
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

- (Object) method_missing

Just dummy out profiling calls when profiling is not active



65
# File 'lib/ldapmodel/profiler.rb', line 65

def method_missing(*); end

Instance Method Details

- (Object) count(timer)



58
59
60
# File 'lib/ldapmodel/profiler.rb', line 58

def count(timer)
  store[:queries].push(timer)
end


52
53
54
55
56
# File 'lib/ldapmodel/profiler.rb', line 52

def print_search_count(target)
  duration = store[:queries].reduce(0){ |memo, q| memo + q.duration }

  puts "Did #{ store[:queries].size } LDAP queries in #{ duration }ms for #{ target }".colorize(:blue)
end

- (Object) reset



47
48
49
50
# File 'lib/ldapmodel/profiler.rb', line 47

def reset
  Thread.current["profiler:#{ @key }"] = nil
  store[:queries] = []
end

- (Object) start



32
33
34
# File 'lib/ldapmodel/profiler.rb', line 32

def start
  return Timer.new
end

- (Object) store

Query data is stored to thread local. This should be cleared before usage



43
44
45
# File 'lib/ldapmodel/profiler.rb', line 43

def store
  Thread.current["profiler:#{ @key }"] ||= {}
end