class_stat 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 0bf4b1b0fe06e8381456cbbb41f2fe53501b8f77
4
+ data.tar.gz: 4e8ed794604d320399ab37c39b6f4e2e789f06f6
5
+ SHA512:
6
+ metadata.gz: e106e86e0a6f936ad2f7bcb21abad4b9d21590abdac2b2043120e52529f3fed380015fae5f4adc5acdce375064e6d2fa22fc8764a6795c2007658f3d2b27e76b
7
+ data.tar.gz: 92493db59285b5c2c0d066134de7c420d242618444a50aacc671661ee5a43e83e049d5651a67a57e1adf760921e53429bd03ed5ca5389f59dcd722aeed3855b8
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.0
4
+ - 2.2.0
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in class_stat.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Koichi Sasada
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,47 @@
1
+ # ClassStat
2
+
3
+ Collect statistics about Class and Modules.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'class_stat'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install class_stat
20
+
21
+ ## Usage
22
+
23
+ Please add this line into your application.
24
+
25
+ ```ruby
26
+ require 'class_stat/setup'
27
+ ```
28
+
29
+ And you will have a statistics result at the end of application.
30
+
31
+ All of result puts on STDOUT. If you want to store it into a file,
32
+ then use 'CLASS_STAT_FILE' environment variable.
33
+
34
+ ```
35
+ $ CLASS_STAT_FILE=/tmp/class_stat_result ruby ...
36
+ ```
37
+
38
+ Please send your result to Koichi Sasada <ko1@atdot.net>.
39
+ This information will help me to improve MRI.
40
+
41
+ ## Contributing
42
+
43
+ 1. Fork it ( https://github.com/[my-github-username]/class_stat/fork )
44
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
45
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
46
+ 4. Push to the branch (`git push origin my-new-feature`)
47
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,21 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/extensiontask"
3
+ require 'rake/testtask'
4
+
5
+ spec = Gem::Specification.load('class_stat.gemspec')
6
+
7
+ Rake::ExtensionTask.new("class_stat", spec){|ext|
8
+ ext.lib_dir = "lib/class_stat"
9
+ }
10
+
11
+ Rake::TestTask.new 'test' => 'compile' do |t|
12
+ t.libs << 'test'
13
+ t.pattern = "test/*_test.rb"
14
+ end
15
+
16
+ task :run => 'compile' do
17
+ ruby %q{-I ./lib test.rb}
18
+ end
19
+
20
+ task default: :test
21
+
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'class_stat/version'
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "class_stat"
7
+ spec.version = ClassStat::VERSION
8
+ spec.authors = ["Koichi Sasada"]
9
+ spec.email = ["ko1@atdot.net"]
10
+ spec.summary = %q{Collect statistics about Class and Modules.}
11
+ spec.description = %q{Collect statistics about Class and Modules.}
12
+ spec.homepage = "https://github.com/ko1/class_stat"
13
+ spec.license = "MIT"
14
+
15
+ spec.extensions = %w[ext/class_stat/extconf.rb]
16
+ spec.required_ruby_version = '>= 2.1.0'
17
+
18
+ spec.files = `git ls-files -z`.split("\x0")
19
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
20
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.7"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+ spec.add_development_dependency "rake-compiler"
26
+ spec.add_development_dependency "minitest", "~> 5.0.0"
27
+ end
@@ -0,0 +1,86 @@
1
+
2
+ #include <ruby/ruby.h>
3
+
4
+ void rb_objspace_each_objects(
5
+ int (*callback)(void *start, void *end, size_t stride, void *data),
6
+ void *data);
7
+
8
+ struct class_stat_struct {
9
+ VALUE klass_iclassnum; /* { klass => iclassnum } */
10
+ VALUE klass_msize; /* { klass => method_count_size } */
11
+ };
12
+
13
+ #define RICLASS_IS_ORIGIN FL_USER5
14
+
15
+ static void
16
+ countup(VALUE h, VALUE k, int n)
17
+ {
18
+ VALUE cn = INT2FIX(0);
19
+
20
+ if (!RBASIC_CLASS(k)) {
21
+ k = rb_sprintf("#<hidden_class:%p>", (void *)k);
22
+ }
23
+
24
+ cn = rb_hash_lookup2(h, k, cn);
25
+ cn = INT2FIX(FIX2INT(cn) + n);
26
+ rb_hash_aset(h, k, cn);
27
+ }
28
+
29
+ static int
30
+ msize(VALUE klass)
31
+ {
32
+ if (RCLASS(klass)->m_tbl) {
33
+ return RCLASS(klass)->m_tbl->num_entries;
34
+ }
35
+ else {
36
+ return 0;
37
+ }
38
+ }
39
+
40
+ static int
41
+ stat_i(void *vstart, void *vend, size_t stride, void *data)
42
+ {
43
+ struct class_stat_struct *d = (struct class_stat_struct *)data;
44
+ VALUE v = (VALUE)vstart;
45
+
46
+ for (;v != (VALUE)vend; v += stride) {
47
+ if (RBASIC(v)->flags) {
48
+ switch (BUILTIN_TYPE(v)) {
49
+ case T_ICLASS: {
50
+ VALUE klass = RBASIC_CLASS(v);
51
+ if (FL_TEST(v, RICLASS_IS_ORIGIN)) {
52
+ countup(d->klass_msize, klass, msize(klass));
53
+ }
54
+ else {
55
+ countup(d->klass_iclassnum, klass, 1);
56
+ }
57
+ break;
58
+ }
59
+ case T_CLASS:
60
+ case T_MODULE:
61
+ countup(d->klass_msize, v, msize(v));
62
+ break;
63
+ }
64
+ }
65
+ }
66
+ return 0;
67
+ }
68
+
69
+ static VALUE
70
+ class_stat(VALUE self)
71
+ {
72
+ struct class_stat_struct data;
73
+ data.klass_iclassnum = rb_hash_new();
74
+ data.klass_msize =rb_hash_new();
75
+
76
+ rb_objspace_each_objects(stat_i, &data);
77
+
78
+ return rb_ary_new_from_args(2, data.klass_iclassnum, data.klass_msize);
79
+ }
80
+
81
+ void
82
+ Init_class_stat(void)
83
+ {
84
+ VALUE mod = rb_define_module("ClassStat");
85
+ rb_define_singleton_method(mod, "stat", class_stat, 0);
86
+ }
@@ -0,0 +1,2 @@
1
+ require 'mkmf'
2
+ create_makefile('class_stat')
data/lib/class_stat.rb ADDED
@@ -0,0 +1,6 @@
1
+ require "class_stat/version"
2
+ require 'class_stat/class_stat'
3
+
4
+ module ClassStat
5
+
6
+ end
@@ -0,0 +1,48 @@
1
+ require 'class_stat'
2
+ require 'pp'
3
+
4
+ END{
5
+ file = ENV['CLASS_STAT_FILE']
6
+ begin
7
+ if file
8
+ prev_stdout = $stdout
9
+ $stdout = open(file, 'w')
10
+ end
11
+
12
+ iclass, methods = ClassStat.stat
13
+ total_dup = 0
14
+ total_iclasses = 0
15
+ total_methods = 0
16
+
17
+ iclass.map{|k, v|
18
+ [k, v, methods[k] * v]
19
+ }.sort_by{|(k, n, t)|
20
+ [-t, k.to_s]
21
+ }.each{|(k, v, t)|
22
+ total_dup += t
23
+ total_iclasses += v
24
+ puts "#{k}\t#{v}\t#{t}"
25
+ }
26
+ puts '-- '
27
+
28
+ methods.sort_by{|k, v|
29
+ [-v, k.to_s]
30
+ }.each{|k, v|
31
+ total_methods += v
32
+ puts "#{k}\t#{v}"
33
+ }
34
+ puts '-- '
35
+
36
+ pp GC.stat
37
+ pp ObjectSpace.count_objects
38
+ puts '-- '
39
+
40
+ puts "total_klasses\t#{methods.size}"
41
+ puts "total_included\t#{iclass.size}"
42
+ puts "total_iclasses\t#{total_iclasses}"
43
+ puts "total_methods\t#{total_methods}"
44
+ puts "total_dup\t#{total_dup}"
45
+ ensure
46
+ $stdout = prev_stdout if prev_stdout
47
+ end
48
+ }
@@ -0,0 +1,3 @@
1
+ module ClassStat
2
+ VERSION = "0.0.1"
3
+ end
data/test.rb ADDED
@@ -0,0 +1 @@
1
+ require 'class_stat/setup'
@@ -0,0 +1,12 @@
1
+ require "minitest/autorun"
2
+ require 'class_stat'
3
+
4
+ class ClassStatTest < Minitest::Test
5
+ def test_classstat
6
+ s = ClassStat.stat
7
+ assert_equal(2, s.size)
8
+ assert_kind_of(Array, s)
9
+ assert_kind_of(Hash, s[0])
10
+ assert_kind_of(Hash, s[1])
11
+ end
12
+ end
metadata ADDED
@@ -0,0 +1,116 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: class_stat
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Koichi Sasada
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-07-03 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake-compiler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: minitest
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 5.0.0
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 5.0.0
69
+ description: Collect statistics about Class and Modules.
70
+ email:
71
+ - ko1@atdot.net
72
+ executables: []
73
+ extensions:
74
+ - ext/class_stat/extconf.rb
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".gitignore"
78
+ - ".travis.yml"
79
+ - Gemfile
80
+ - LICENSE.txt
81
+ - README.md
82
+ - Rakefile
83
+ - class_stat.gemspec
84
+ - ext/class_stat/class_stat.c
85
+ - ext/class_stat/extconf.rb
86
+ - lib/class_stat.rb
87
+ - lib/class_stat/setup.rb
88
+ - lib/class_stat/version.rb
89
+ - test.rb
90
+ - test/class_stat_test.rb
91
+ homepage: https://github.com/ko1/class_stat
92
+ licenses:
93
+ - MIT
94
+ metadata: {}
95
+ post_install_message:
96
+ rdoc_options: []
97
+ require_paths:
98
+ - lib
99
+ required_ruby_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: 2.1.0
104
+ required_rubygems_version: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ requirements: []
110
+ rubyforge_project:
111
+ rubygems_version: 2.5.0
112
+ signing_key:
113
+ specification_version: 4
114
+ summary: Collect statistics about Class and Modules.
115
+ test_files:
116
+ - test/class_stat_test.rb