lib/koine/profiler.rb in koine-profiler-0.1.0 vs lib/koine/profiler.rb in koine-profiler-2.0.0
- old
+ new
@@ -1,24 +1,41 @@
-require "koine/profiler/version"
-require "koine/profiler/entries"
-require "koine/profiler/entry_group"
-require "koine/profiler/entry"
+require 'koine/profiler/version'
+require 'koine/profiler/entry'
+require 'get_process_mem'
module Koine
class Profiler
- attr_reader :entries
-
def initialize
- @entries = Entries.new
+ @entries = {}
end
def profile(name)
value = nil
start_time = Time.now.utc
+ start_memory = GetProcessMem.new.mb
value = yield if block_given?
+ finish_memory = GetProcessMem.new.mb
finish_time = Time.now.utc
-
- entries.append(name, finish_time - start_time)
+ add_entry(
+ name,
+ time: finish_time - start_time,
+ memory: finish_memory - start_memory
+ )
value
+ end
+
+ def entries
+ @entries.values
+ end
+
+ def self.instance
+ @instance ||= new
+ end
+
+ private
+
+ def add_entry(name, time:, memory:)
+ entry = @entries[name] ||= Entry.new(name)
+ entry.increment(elapsed_time: time, memory_used: memory)
end
end
end