Module: RDKit::SlowLog::ClassMethods

Included in:
RDKit::SlowLog
Defined in:
lib/rdkit/slow_log.rb

Constant Summary collapse

@@logs =
[]
@@sequence_id =
0

Instance Method Summary collapse

Instance Method Details

#countObject



9
10
11
# File 'lib/rdkit/slow_log.rb', line 9

def count
  @@logs.size
end

#monitor(cmd, &block) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/rdkit/slow_log.rb', line 27

def monitor(cmd, &block)
  t0 = Time.now
  result = block.call
  elapsed_in_usec = ((Time.now - t0) * 1_000_000).to_i
  elapsed_in_milliseconds = elapsed_in_usec / 1_000

  if (threshold = Configuration.get_i('slowlog-log-slower-than')) >= 0
    if elapsed_in_milliseconds >= threshold
      @@logs << [@@sequence_id, Time.now.to_i, elapsed_in_milliseconds, cmd]
      @@sequence_id += 1

      if (max_len = Configuration.get_i('slowlog-max-len')) > 0
        @@logs = @@logs[-max_len..-1]
      end

      NotificationCenter.publish('slowlog', [cmd, elapsed_in_usec])
    end
  end

  [result, elapsed_in_usec]
end

#recent(count) ⇒ Object



17
18
19
20
21
22
23
24
25
# File 'lib/rdkit/slow_log.rb', line 17

def recent(count)
  if count == 0
    []
  elsif count > 0
    (@@logs[-count..-1] || @@logs).try(:reverse)
  else
    @@logs.try(:reverse)
  end
end

#resetObject



13
14
15
# File 'lib/rdkit/slow_log.rb', line 13

def reset
  @@logs.clear
end