tacho 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 649ae475b526cb6a76fec040c7ba1f1519a1035a980eecc51b3b57fb593ca950
4
+ data.tar.gz: d16cce60864b220c7b376dd4248f35cce5584f56332cd716d3db9ad0943b76e4
5
+ SHA512:
6
+ metadata.gz: 6eaea8985e1dcbdb92156fe6ef0655978a94a960a3ebb119c91ddd769e9ecb0f042d39b48956c17dd64ca1d38c94dbbb569174a1758160b9ab93d377a24d0316
7
+ data.tar.gz: 7587dd2b0b9a843981cf083ba38abb54529e2c890906cb2d5b3114a4155a98046b24d875c9c5573919bb7c6febc01904cf788161519db8764466d6d19f596cfa
@@ -0,0 +1,15 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ *.bundle
10
+ *.so
11
+ *.o
12
+ *.a
13
+ mkmf.log
14
+
15
+ .idea/
@@ -0,0 +1 @@
1
+ 2.5.3
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in tacho.gemspec
6
+ gemspec
@@ -0,0 +1,25 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ tacho (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ minitest (5.11.3)
10
+ rake (10.5.0)
11
+ rake-compiler (1.0.7)
12
+ rake
13
+
14
+ PLATFORMS
15
+ ruby
16
+
17
+ DEPENDENCIES
18
+ bundler (~> 1.17)
19
+ minitest (~> 5.0)
20
+ rake (~> 10.0)
21
+ rake-compiler
22
+ tacho!
23
+
24
+ BUNDLED WITH
25
+ 1.17.2
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 TODO: Write your name
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,49 @@
1
+ # Tacho
2
+
3
+ Profiles ruby code and generates profiles that can be read by the tacho viewer
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'tacho'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install tacho
20
+
21
+ ## Usage
22
+
23
+ Surround the code to be profiled like so:
24
+
25
+ ```ruby
26
+ profile = Tacho.start 'some_Name'
27
+ some_code()
28
+ profile.stop
29
+ ```
30
+
31
+ This will generate a file in `./tachometer/<timestamp>.tch` which can be read by the viewer
32
+
33
+ ## Development
34
+
35
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
36
+
37
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
38
+
39
+ ## Contributing
40
+
41
+ Bug reports and pull requests are welcome on GitHub at https://github.com/divanburger/tacho. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
42
+
43
+ ## License
44
+
45
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
46
+
47
+ ## Code of Conduct
48
+
49
+ Everyone interacting in the Tacho project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/divanburger/tacho/blob/master/CODE_OF_CONDUCT.md).
@@ -0,0 +1,18 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList["test/**/*_test.rb"]
8
+ end
9
+
10
+ require "rake/extensiontask"
11
+
12
+ task :build => :compile
13
+
14
+ Rake::ExtensionTask.new("tacho") do |ext|
15
+ ext.lib_dir = "lib/tacho"
16
+ end
17
+
18
+ task :default => [:clobber, :compile, :test]
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "tacho"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,3 @@
1
+ require "mkmf"
2
+
3
+ create_makefile("tacho/tacho")
@@ -0,0 +1,176 @@
1
+ #include "tacho.h"
2
+
3
+ #include <ruby/ruby.h>
4
+ #include <ruby/debug.h>
5
+
6
+ static inline void write_header(tch_profile* profile, char type, uint64_t time) {
7
+ uint64_t c = ((time - profile->start_time) << 8) | type;
8
+ fwrite(&c, sizeof(uint64_t), 1, profile->output);
9
+ }
10
+
11
+ static void tacho_event(VALUE tpval, void *data) {
12
+ tch_profile* profile = (tch_profile*)data;
13
+
14
+ struct timespec time;
15
+ uint64_t itime;
16
+
17
+ VALUE path;
18
+ VALUE method_id;
19
+ const char* method_name;
20
+ int method_length;
21
+
22
+ int file_len;
23
+ char* file_str;
24
+ int cur_len;
25
+ char* cur_str;
26
+ int i;
27
+
28
+ tch_call_body call_body;
29
+
30
+ rb_trace_arg_t* trace_arg;
31
+ rb_event_flag_t event_flag;
32
+
33
+ trace_arg = rb_tracearg_from_tracepoint(tpval);
34
+ event_flag = rb_tracearg_event_flag(trace_arg);
35
+
36
+ clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time);
37
+ itime = (long)time.tv_sec * 1000000000 + (long)time.tv_nsec;
38
+
39
+ if (event_flag == RUBY_EVENT_CALL) {
40
+ write_header(profile, 'C', itime);
41
+ call_body.line_no = FIX2INT(rb_tracearg_lineno(trace_arg));
42
+
43
+ path = rb_tracearg_path(trace_arg);
44
+
45
+ method_id = SYM2ID(rb_tracearg_method_id(trace_arg));
46
+
47
+ method_name = rb_id2name(method_id);
48
+ method_length = strlen(method_name);
49
+
50
+ cur_len = file_len = (int)RSTRING_LEN(path);
51
+ cur_str = file_str = RSTRING_PTR(path);
52
+
53
+ for (i = 0; i < file_len && i < profile->buffer_count; i++) {
54
+ if (*cur_str != profile->buffer[i]) break;
55
+ cur_len--;
56
+ cur_str++;
57
+ }
58
+
59
+ profile->buffer_count = file_len;
60
+ memcpy(profile->buffer, file_str, file_len);
61
+
62
+ call_body.method_name_length = method_length;
63
+ call_body.filename_offset = file_len - cur_len;
64
+ call_body.filename_length = cur_len;
65
+ fwrite(&call_body, CALL_BODY_BYTES, 1, profile->output);
66
+ fwrite(method_name, call_body.method_name_length, 1, profile->output);
67
+ fwrite(cur_str, call_body.filename_length, 1, profile->output);
68
+ } else if (event_flag == RUBY_EVENT_RETURN) {
69
+ write_header(profile, 'R', itime);
70
+ }
71
+ }
72
+
73
+ VALUE rb_mTacho;
74
+ VALUE rb_cTachoProfile;
75
+
76
+ static tch_profile* tch_profile_get_profile(VALUE self) {
77
+ return (tch_profile*)RDATA(self)->data;
78
+ }
79
+
80
+ static size_t tch_profile_size(const void* data) {
81
+ return sizeof(tch_profile);
82
+ }
83
+
84
+ static void tch_profile_mark(void* data)
85
+ {
86
+ rb_gc_mark(((tch_profile*)data)->tracepoint);
87
+ }
88
+
89
+ static const rb_data_type_t tch_profile_type = {
90
+ .wrap_struct_name = "tacho_profile",
91
+ .function = {
92
+ .dfree = RUBY_DEFAULT_FREE,
93
+ .dsize = tch_profile_size,
94
+ .dmark = tch_profile_mark,
95
+ },
96
+ .flags = RUBY_TYPED_FREE_IMMEDIATELY,
97
+ };
98
+
99
+ static VALUE tch_profile_allocate(VALUE self) {
100
+ tch_profile* profile;
101
+ return TypedData_Make_Struct(self, tch_profile, &tch_profile_type, profile);
102
+ }
103
+
104
+ static VALUE tch_profile_initialize(VALUE self) {
105
+ tch_profile* profile = tch_profile_get_profile(self);
106
+ profile->recording = 0;
107
+ return self;
108
+ }
109
+
110
+ static VALUE tch_profile_start(VALUE self, VALUE filename, VALUE name) {
111
+ tch_profile* profile = tch_profile_get_profile(self);
112
+
113
+ struct timespec time;
114
+ uint32_t name_length;
115
+
116
+ profile->output = fopen(StringValueCStr(filename), "wb");
117
+ profile->buffer_count = 0;
118
+
119
+ if (!profile->output) {
120
+ rb_raise(rb_eRuntimeError, "Could not open file %s", StringValueCStr(filename));
121
+ RB_GC_GUARD(filename);
122
+ return Qnil;
123
+ }
124
+ RB_GC_GUARD(filename);
125
+
126
+ profile->recording = 1;
127
+
128
+ clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time);
129
+ profile->start_time = (long)time.tv_sec * 1000000000 + (long)time.tv_nsec;
130
+ write_header(profile, 'S', profile->start_time);
131
+
132
+ name_length = (uint32_t)RSTRING_LEN(name);
133
+ fwrite(&name_length, sizeof(uint32_t), 1, profile->output);
134
+ fwrite(RSTRING_PTR(name), name_length, 1, profile->output);
135
+ RB_GC_GUARD(name);
136
+
137
+ profile->tracepoint = rb_tracepoint_new(Qnil, RUBY_EVENT_CALL | RUBY_EVENT_RETURN, tacho_event, profile);
138
+ rb_tracepoint_enable(profile->tracepoint);
139
+
140
+ return Qnil;
141
+ }
142
+
143
+ static VALUE tch_profile_stop(VALUE self) {
144
+ tch_profile* profile = tch_profile_get_profile(self);
145
+
146
+ struct timespec time;
147
+ uint64_t itime;
148
+
149
+ if (profile->recording) {
150
+ clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time);
151
+ itime = (long)time.tv_sec * 1000000000 + (long)time.tv_nsec;
152
+ write_header(profile, 'F', itime);
153
+
154
+ fflush(profile->output);
155
+ fclose(profile->output);
156
+
157
+ rb_tracepoint_disable(profile->tracepoint);
158
+
159
+ profile->recording = 0;
160
+ } else {
161
+ rb_raise(rb_eRuntimeError, "Not currently recording");
162
+ }
163
+
164
+ return Qnil;
165
+ }
166
+
167
+ void
168
+ Init_tacho(void) {
169
+ rb_mTacho = rb_define_module("Tacho");
170
+
171
+ rb_cTachoProfile = rb_define_class_under(rb_mTacho, "Profile", rb_cObject);
172
+ rb_define_alloc_func(rb_cTachoProfile, tch_profile_allocate);
173
+ rb_define_method(rb_cTachoProfile, "initialize", tch_profile_initialize, 0);
174
+ rb_define_method(rb_cTachoProfile, "start", tch_profile_start, 2);
175
+ rb_define_method(rb_cTachoProfile, "stop", tch_profile_stop, 0);
176
+ }
@@ -0,0 +1,26 @@
1
+ #ifndef TACHOMETER_H
2
+ #define TACHOMETER_H 1
3
+
4
+ #include "ruby.h"
5
+ #include <time.h>
6
+ #include <stdint.h>
7
+
8
+ typedef struct {
9
+ uint32_t line_no;
10
+ uint16_t method_name_length;
11
+ uint16_t filename_offset;
12
+ uint16_t filename_length;
13
+ } tch_call_body;
14
+ #define CALL_BODY_BYTES 10
15
+
16
+ typedef struct {
17
+ VALUE tracepoint;
18
+
19
+ int recording;
20
+ FILE* output;
21
+ uint64_t start_time;
22
+ int64_t buffer_count;
23
+ char buffer[4096];
24
+ } tch_profile;
25
+
26
+ #endif /* TACHOMETER_H */
@@ -0,0 +1,14 @@
1
+ require "tacho/version"
2
+ require "tacho/tacho"
3
+ require "tacho/middleware"
4
+
5
+ module Tacho
6
+ class Error < StandardError;
7
+ end
8
+
9
+ def self.start(name = '')
10
+ profile = Profile.new()
11
+ profile.start("tacho/#{Time.now.strftime("%Y%m%d_%H%M%S_%N")}.tch", name)
12
+ return profile
13
+ end
14
+ end
@@ -0,0 +1,14 @@
1
+ module Tacho
2
+ class Middleware
3
+ def initialize app
4
+ @app = app
5
+ end
6
+
7
+ def call(env)
8
+ Tacho.enable
9
+ response = @app.call(env)
10
+ Tacho.disable
11
+ response
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,3 @@
1
+ module Tacho
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,43 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "tacho/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "tacho"
8
+ spec.version = Tacho::VERSION
9
+ spec.authors = ["Divan Burger"]
10
+ spec.email = ["divan@labs.epiuse.com"]
11
+
12
+ spec.summary = %q{Records performance data and displays it.}
13
+ spec.homepage = "https://github.com/divanburger/tacho"
14
+ spec.license = "MIT"
15
+
16
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
17
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
18
+ if spec.respond_to?(:metadata)
19
+ spec.metadata["allowed_push_host"] = "https://rubygems.org"
20
+
21
+ spec.metadata["homepage_uri"] = spec.homepage
22
+ spec.metadata["source_code_uri"] = "https://github.com/divanburger/tacho"
23
+ spec.metadata["changelog_uri"] = "https://github.com/divanburger/tacho/blob/master/README.md"
24
+ else
25
+ raise "RubyGems 2.0 or newer is required to protect against " \
26
+ "public gem pushes."
27
+ end
28
+
29
+ # Specify which files should be added to the gem when it is released.
30
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
31
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
32
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
33
+ end
34
+ spec.bindir = "exe"
35
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
36
+ spec.require_paths = ["lib"]
37
+ spec.extensions = ["ext/tacho/extconf.rb"]
38
+
39
+ spec.add_development_dependency "bundler", "~> 1.17"
40
+ spec.add_development_dependency "rake", "~> 10.0"
41
+ spec.add_development_dependency "rake-compiler", "~> 0"
42
+ spec.add_development_dependency "minitest", "~> 5.0"
43
+ end
metadata ADDED
@@ -0,0 +1,121 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tacho
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Divan Burger
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-01-08 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.17'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.17'
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'
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'
69
+ description:
70
+ email:
71
+ - divan@labs.epiuse.com
72
+ executables: []
73
+ extensions:
74
+ - ext/tacho/extconf.rb
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".gitignore"
78
+ - ".ruby-version"
79
+ - Gemfile
80
+ - Gemfile.lock
81
+ - LICENSE.txt
82
+ - README.md
83
+ - Rakefile
84
+ - bin/console
85
+ - bin/setup
86
+ - ext/tacho/extconf.rb
87
+ - ext/tacho/tacho.c
88
+ - ext/tacho/tacho.h
89
+ - lib/tacho.rb
90
+ - lib/tacho/middleware.rb
91
+ - lib/tacho/version.rb
92
+ - tacho.gemspec
93
+ homepage: https://github.com/divanburger/tacho
94
+ licenses:
95
+ - MIT
96
+ metadata:
97
+ allowed_push_host: https://rubygems.org
98
+ homepage_uri: https://github.com/divanburger/tacho
99
+ source_code_uri: https://github.com/divanburger/tacho
100
+ changelog_uri: https://github.com/divanburger/tacho/blob/master/README.md
101
+ post_install_message:
102
+ rdoc_options: []
103
+ require_paths:
104
+ - lib
105
+ required_ruby_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ required_rubygems_version: !ruby/object:Gem::Requirement
111
+ requirements:
112
+ - - ">="
113
+ - !ruby/object:Gem::Version
114
+ version: '0'
115
+ requirements: []
116
+ rubyforge_project:
117
+ rubygems_version: 2.7.6
118
+ signing_key:
119
+ specification_version: 4
120
+ summary: Records performance data and displays it.
121
+ test_files: []