Method: RDKit::SlowLog::ClassMethods#monitor

Defined in:
lib/rdkit/slow_log.rb

#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