zenprofile 1.0.2 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data.tar.gz.sig CHANGED
Binary file
@@ -1,3 +1,13 @@
1
+ === 1.1.0 / 2009-06-23
2
+
3
+ * 1 major enhancement:
4
+
5
+ * Added memory_profiler.rb. It is rad.
6
+
7
+ * 1 minor enhancement:
8
+
9
+ * Updated rakefile for new hoe capabilities.
10
+
1
11
  === 1.0.0 / 2008-09-17
2
12
 
3
13
  * 1 major enhancement
@@ -3,6 +3,7 @@ Manifest.txt
3
3
  README.txt
4
4
  Rakefile
5
5
  bin/zenprofile
6
+ lib/memory_profiler.rb
6
7
  lib/spy_on.rb
7
8
  lib/zenprofile.rb
8
9
  lib/zenprofiler.rb
data/Rakefile CHANGED
@@ -2,15 +2,17 @@
2
2
 
3
3
  require 'rubygems'
4
4
  require 'hoe'
5
- require './lib/zenprofiler.rb'
6
5
 
7
- Hoe.new('zenprofile', ZenProfiler::VERSION) do |p|
8
- p.rubyforge_name = 'seattlerb'
9
- p.developer('Ryan Davis', 'ryand-ruby@zenspider.com')
6
+ Hoe.plugin :seattlerb
7
+ Hoe.plugin :inline
10
8
 
11
- p.extra_deps << 'ZenTest'
12
- p.extra_deps << 'event_hook'
13
- p.extra_deps << 'RubyInline'
9
+ Hoe.spec 'zenprofile' do
10
+ developer 'Ryan Davis', 'ryand-ruby@zenspider.com'
11
+
12
+ self.rubyforge_name = 'seattlerb'
13
+
14
+ extra_deps << 'ZenTest'
15
+ extra_deps << 'event_hook'
14
16
  end
15
17
 
16
- # vim: syntax=Ruby
18
+ # vim: syntax=ruby
@@ -0,0 +1,108 @@
1
+
2
+ class MemoryProfiler
3
+
4
+ # profile is a CLEAN, totally minimal memory profiler:
5
+ #
6
+ # * 1 thread
7
+ # * 2 hashes (curr, prev)
8
+ # * classes as keys
9
+ # * fixnums as values.
10
+ #
11
+ # No extra memory required if I can help it. Keep it as simple as
12
+ # possible. The code at the link above is WAY to complex for such a
13
+ # simple task. It does use a sort_by, so that produces a bit extra
14
+ # junk... but that shouldn't be more than an extra array and hash
15
+ # per iteration.
16
+ #
17
+ # Just fire it up as MemoryProfiler.profile or use it with a block
18
+ # of code:
19
+ #
20
+ # MemoryProfiler.profile do
21
+ # # ...
22
+ # end
23
+
24
+ def self.profile(delay = 2, limit=10, output=$stderr)
25
+ Thread.abort_on_exception = true
26
+ totals = Hash.new(0)
27
+ t = Thread.new do
28
+ prev = Hash.new(0)
29
+ curr = Hash.new(0)
30
+ loop do
31
+ curr.clear
32
+ ObjectSpace.each_object do |o|
33
+ curr[o.class] += 1
34
+ end
35
+
36
+ curr.each do |k,v|
37
+ curr[k] -= prev[k]
38
+ end
39
+
40
+ puts
41
+ curr.sort_by { |k,v| -v }.first(limit).each do |k,v|
42
+ output.printf "%+5d: %s\n", v, k.name unless v == 0
43
+ end
44
+
45
+ prev.clear
46
+ prev.update curr
47
+ curr.each do |k,n| totals[k] += n end
48
+ sleep delay
49
+ end
50
+ end
51
+ if block_given? then
52
+ yield
53
+ t.exit
54
+ end
55
+ totals
56
+ end
57
+
58
+ @@locations = {}
59
+ @@allocations = Hash.new { |h,k| h[k] = Hash.new(0) }
60
+
61
+ def self.log(o)
62
+ loc = caller[1]
63
+ unless @@locations.has_key? loc then
64
+ @@locations[loc] = @@locations.size
65
+ end
66
+ loc = @@locations[loc]
67
+ @@allocations[o.class][loc] += 1
68
+ end
69
+
70
+ def self.spy_on *klasses
71
+ klasses.each do |klass|
72
+ hook klass, :initialize
73
+ if klass == String then
74
+ hook klass, :%
75
+ hook klass, :strip
76
+ end
77
+ end
78
+ end
79
+
80
+ def self.hook klass, meth
81
+ klass.module_eval do
82
+ old = meth.to_s.sub(/%/, 'pct').sub(/^/, 'old_')
83
+ alias_method old, meth
84
+ eval "def #{meth}(*args, &block)
85
+ MemoryProfiler.log(self)
86
+ #{old}(*args, &block)
87
+ end"
88
+ end
89
+ end
90
+
91
+ def self.report
92
+ keys = @@locations.invert
93
+
94
+ puts
95
+ puts "############################################################"
96
+ puts "Spy Report:"
97
+
98
+ @@allocations.each do |klass, locations|
99
+ puts
100
+ puts klass
101
+ puts
102
+ locations.sort_by { |k,v| -v }.first(10).each do |location, count|
103
+ printf "%6d: %s\n", count, keys[location]
104
+ end
105
+ end
106
+ p @@locations, @@allocations, keys if $DEBUG
107
+ end
108
+ end
@@ -17,12 +17,12 @@ require 'event_hook'
17
17
  # % ruby -rzenprofile misc/factorial.rb
18
18
 
19
19
  class ZenProfiler < EventHook
20
- VERSION = '1.0.2'
20
+ VERSION = '1.1.0'
21
21
 
22
- @@start = nil
23
- @@stack = [[0, 0, [nil, :toplevel]], [0, 0, [nil, :dummy]]]
24
- @@map = Hash.new { |h,k| h[k] = [0, 0.0, 0.0, k] }
25
- @@map["#toplevel"] = [1, 0.0, 0.0, [nil, "#toplevel"]]
22
+ @@start = nil
23
+ @@stack = [[0, 0, [nil, :toplevel]], [0, 0, [nil, :dummy]]]
24
+ @@map = Hash.new { |h,k| h[k] = [0, 0.0, 0.0, k] }
25
+ @@map["#toplevel"] = [1, 0.0, 0.0, [nil, "#toplevel"]]
26
26
  @@percent_time_threshold = 0.5
27
27
 
28
28
  def self.run(fp = $stdout, opts = {})
@@ -17,7 +17,7 @@ end
17
17
  if $0 == __FILE__ then
18
18
  f = Factorial.new()
19
19
 
20
- max = ARGV.shift || 1000000
20
+ max = ARGV.shift || 1_000_000
21
21
  max = max.to_i
22
22
 
23
23
  tstart = Time.now
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: zenprofile
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryan Davis
@@ -30,7 +30,7 @@ cert_chain:
30
30
  FBHgymkyj/AOSqKRIpXPhjC6
31
31
  -----END CERTIFICATE-----
32
32
 
33
- date: 2009-05-20 00:00:00 -07:00
33
+ date: 2009-06-23 00:00:00 -07:00
34
34
  default_executable:
35
35
  dependencies:
36
36
  - !ruby/object:Gem::Dependency
@@ -53,16 +53,6 @@ dependencies:
53
53
  - !ruby/object:Gem::Version
54
54
  version: "0"
55
55
  version:
56
- - !ruby/object:Gem::Dependency
57
- name: RubyInline
58
- type: :runtime
59
- version_requirement:
60
- version_requirements: !ruby/object:Gem::Requirement
61
- requirements:
62
- - - ">="
63
- - !ruby/object:Gem::Version
64
- version: "0"
65
- version:
66
56
  - !ruby/object:Gem::Dependency
67
57
  name: hoe
68
58
  type: :development
@@ -71,7 +61,7 @@ dependencies:
71
61
  requirements:
72
62
  - - ">="
73
63
  - !ruby/object:Gem::Version
74
- version: 1.12.2
64
+ version: 2.3.0
75
65
  version:
76
66
  description: |-
77
67
  zenprofiler helps answer WHAT is being called the most. spy_on helps
@@ -120,6 +110,7 @@ files:
120
110
  - README.txt
121
111
  - Rakefile
122
112
  - bin/zenprofile
113
+ - lib/memory_profiler.rb
123
114
  - lib/spy_on.rb
124
115
  - lib/zenprofile.rb
125
116
  - lib/zenprofiler.rb
@@ -150,7 +141,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
150
141
  requirements: []
151
142
 
152
143
  rubyforge_project: seattlerb
153
- rubygems_version: 1.3.2
144
+ rubygems_version: 1.3.4
154
145
  signing_key:
155
146
  specification_version: 3
156
147
  summary: zenprofiler helps answer WHAT is being called the most
metadata.gz.sig CHANGED
Binary file