kgb 0.9.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +6 -0
- data/Gemfile +4 -0
- data/LICENSE +19 -0
- data/README.md +87 -0
- data/Rakefile +1 -0
- data/kgb.gemspec +26 -0
- data/lib/kgb/version.rb +3 -0
- data/lib/kgb.rb +57 -0
- metadata +64 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
Copyright (c) 2011 by David Albert, http://dave.is/
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
+
of this software and associated documentation files (the "Software"), to deal
|
5
|
+
in the Software without restriction, including without limitation the rights
|
6
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
|
+
copies of the Software, and to permit persons to whom the Software is
|
8
|
+
furnished to do so, subject to the following conditions:
|
9
|
+
|
10
|
+
The above copyright notice and this permission notice shall be included in
|
11
|
+
all copies or substantial portions of the Software.
|
12
|
+
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
19
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,87 @@
|
|
1
|
+
KGB
|
2
|
+
===
|
3
|
+
|
4
|
+
KGB spies on your classes and tells you what methods are being called. I wrote it to let me see what methods were being called on the `socket` library while writing [Eventless](http://github.com/davidbalbert/eventless).
|
5
|
+
|
6
|
+
##How it works
|
7
|
+
|
8
|
+
Tell KGB what you want it to watch, and it reports back after the program has run:
|
9
|
+
|
10
|
+
```ruby
|
11
|
+
# kgb_test.rb
|
12
|
+
|
13
|
+
require 'kgb'
|
14
|
+
|
15
|
+
class Winston
|
16
|
+
def pretend_to_be_a_sheep
|
17
|
+
puts "Big Brother knows best"
|
18
|
+
end
|
19
|
+
|
20
|
+
def think_for_yourself!
|
21
|
+
puts "Freedom is the freedom to say two plus two equals four"
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
KGB.spy_on(Winston)
|
26
|
+
|
27
|
+
winston = Winston.new
|
28
|
+
winston.pretend_to_be_a_sheep
|
29
|
+
winston.pretend_to_be_a_sheep
|
30
|
+
winston.pretend_to_be_a_sheep
|
31
|
+
winston.think_for_yourself!
|
32
|
+
winston.pretend_to_be_a_sheep
|
33
|
+
```
|
34
|
+
|
35
|
+
```
|
36
|
+
$ ruby kgb_test.rb
|
37
|
+
Big Brother knows best
|
38
|
+
Big Brother knows best
|
39
|
+
Big Brother knows best
|
40
|
+
Freedom is the freedom to say two plus two equals four
|
41
|
+
Big Brother knows best
|
42
|
+
|
43
|
+
Winston:
|
44
|
+
|
45
|
+
method invocations
|
46
|
+
pretend_to_be_a_sheep 4
|
47
|
+
think_for_yourself! 1
|
48
|
+
```
|
49
|
+
|
50
|
+
KGB prints to standard error so you can isolate it's output from the rest of the program:
|
51
|
+
|
52
|
+
```
|
53
|
+
$ ruby kgb_test.rb >/dev/null
|
54
|
+
|
55
|
+
Winston:
|
56
|
+
|
57
|
+
method invocations
|
58
|
+
pretend_to_be_a_sheep 4
|
59
|
+
think_for_yourself! 1
|
60
|
+
```
|
61
|
+
|
62
|
+
KGB only tracks calls to instance methods defined on the object itself, not it's parents. Thus if you want to watch all of the `Socket` methods, you would want to do something like this:
|
63
|
+
|
64
|
+
```ruby
|
65
|
+
KGB.spy_on(Socket, BasicSocket, IO)
|
66
|
+
```
|
67
|
+
|
68
|
+
##Install
|
69
|
+
|
70
|
+
```
|
71
|
+
$ gem install kgb
|
72
|
+
```
|
73
|
+
|
74
|
+
##Contributing
|
75
|
+
|
76
|
+
1. Fork
|
77
|
+
2. Branch
|
78
|
+
3. Write code
|
79
|
+
4. Commit
|
80
|
+
5. Push
|
81
|
+
6. Create Pull Request
|
82
|
+
7. ...
|
83
|
+
8. Profit!
|
84
|
+
|
85
|
+
##License
|
86
|
+
|
87
|
+
KGB is licensed under the MIT License. See LICENSE for more information.
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/kgb.gemspec
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "kgb/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "kgb"
|
7
|
+
s.version = KGB::VERSION
|
8
|
+
s.authors = ["David Albert"]
|
9
|
+
s.email = ["davidbalbert@gmail.com"]
|
10
|
+
s.homepage = ""
|
11
|
+
s.summary = %q{A little library for counting method invocations}
|
12
|
+
s.description = %q{A little library for spying on your classes and counting method invocations}
|
13
|
+
|
14
|
+
s.rubyforge_project = "kgb"
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split("\n")
|
17
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
|
+
s.require_paths = ["lib"]
|
20
|
+
|
21
|
+
s.add_dependency "rcapture"
|
22
|
+
|
23
|
+
# specify any dependencies here; for example:
|
24
|
+
# s.add_development_dependency "rspec"
|
25
|
+
# s.add_runtime_dependency "rest-client"
|
26
|
+
end
|
data/lib/kgb/version.rb
ADDED
data/lib/kgb.rb
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
require "kgb/version"
|
2
|
+
require 'rcapture'
|
3
|
+
|
4
|
+
module KGB
|
5
|
+
def self.spy_on(*classes)
|
6
|
+
@agents ||= []
|
7
|
+
classes.each do |klass|
|
8
|
+
@agents << Agent.new(klass)
|
9
|
+
end
|
10
|
+
|
11
|
+
nil
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.print_reports
|
15
|
+
@agents.each { |agent| agent.report }
|
16
|
+
end
|
17
|
+
|
18
|
+
PADDING = 4
|
19
|
+
|
20
|
+
class Agent
|
21
|
+
def initialize(klass)
|
22
|
+
@invocations = {}
|
23
|
+
|
24
|
+
@class = klass
|
25
|
+
@class.class_eval do
|
26
|
+
include RCapture::Interceptable
|
27
|
+
end
|
28
|
+
|
29
|
+
@class.capture :methods => @class.instance_methods(false) do |info|
|
30
|
+
@invocations[info.method] ||= 0
|
31
|
+
@invocations[info.method] += 1
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def report
|
36
|
+
max_length = @invocations.keys.map { |key| key.length }.max
|
37
|
+
max_length = 6 if max_length < "method".length
|
38
|
+
|
39
|
+
say
|
40
|
+
say "#{@class}:"
|
41
|
+
say
|
42
|
+
|
43
|
+
say "method" + " " * (max_length - 6 + PADDING) + "invocations"
|
44
|
+
@invocations.sort { |a, b| b[1] <=> a[1] }.each do |method, times|
|
45
|
+
say method.to_s + " " * (max_length - method.length + PADDING) + times.to_s
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
END {
|
52
|
+
KGB.print_reports
|
53
|
+
}
|
54
|
+
|
55
|
+
def say(*args)
|
56
|
+
STDERR.puts(*args)
|
57
|
+
end
|
metadata
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: kgb
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.9.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- David Albert
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2011-10-20 00:00:00.000000000Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rcapture
|
16
|
+
requirement: &70150658171700 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70150658171700
|
25
|
+
description: A little library for spying on your classes and counting method invocations
|
26
|
+
email:
|
27
|
+
- davidbalbert@gmail.com
|
28
|
+
executables: []
|
29
|
+
extensions: []
|
30
|
+
extra_rdoc_files: []
|
31
|
+
files:
|
32
|
+
- .gitignore
|
33
|
+
- Gemfile
|
34
|
+
- LICENSE
|
35
|
+
- README.md
|
36
|
+
- Rakefile
|
37
|
+
- kgb.gemspec
|
38
|
+
- lib/kgb.rb
|
39
|
+
- lib/kgb/version.rb
|
40
|
+
homepage: ''
|
41
|
+
licenses: []
|
42
|
+
post_install_message:
|
43
|
+
rdoc_options: []
|
44
|
+
require_paths:
|
45
|
+
- lib
|
46
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
47
|
+
none: false
|
48
|
+
requirements:
|
49
|
+
- - ! '>='
|
50
|
+
- !ruby/object:Gem::Version
|
51
|
+
version: '0'
|
52
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
53
|
+
none: false
|
54
|
+
requirements:
|
55
|
+
- - ! '>='
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: '0'
|
58
|
+
requirements: []
|
59
|
+
rubyforge_project: kgb
|
60
|
+
rubygems_version: 1.8.10
|
61
|
+
signing_key:
|
62
|
+
specification_version: 3
|
63
|
+
summary: A little library for counting method invocations
|
64
|
+
test_files: []
|