pinbo 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,21 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Andrew Rudenko
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,18 @@
1
+ = pinbo
2
+
3
+ Description goes here.
4
+
5
+ == Note on Patches/Pull Requests
6
+
7
+ * Fork the project.
8
+ * Make your feature addition or bug fix.
9
+ * Add tests for it. This is important so I don't break it in a
10
+ future version unintentionally.
11
+ * Commit, do not mess with rakefile, version, or history.
12
+ (if you want to have your own version, that is fine but
13
+ bump version in a commit by itself I can ignore when I pull)
14
+ * Send me a pull request. Bonus points for topic branches.
15
+
16
+ == Copyright
17
+
18
+ Copyright (c) 2009 Andrew Rudenko. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,52 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "pinbo"
8
+ gem.summary = %Q{ruby client for Pinba}
9
+ gem.description = %Q{ruby client for Pinba}
10
+ gem.email = "ceo@prepor.ru"
11
+ gem.homepage = "http://github.com/prepor/pinbo"
12
+ gem.authors = ["Andrew Rudenko"]
13
+ gem.add_development_dependency "thoughtbot-shoulda", ">= 0"
14
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
15
+ end
16
+ rescue LoadError
17
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
18
+ end
19
+
20
+ require 'rake/testtask'
21
+ Rake::TestTask.new(:test) do |test|
22
+ test.libs << 'lib' << 'test'
23
+ test.pattern = 'test/**/test_*.rb'
24
+ test.verbose = true
25
+ end
26
+
27
+ begin
28
+ require 'rcov/rcovtask'
29
+ Rcov::RcovTask.new do |test|
30
+ test.libs << 'test'
31
+ test.pattern = 'test/**/test_*.rb'
32
+ test.verbose = true
33
+ end
34
+ rescue LoadError
35
+ task :rcov do
36
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
37
+ end
38
+ end
39
+
40
+ task :test => :check_dependencies
41
+
42
+ task :default => :test
43
+
44
+ require 'rake/rdoctask'
45
+ Rake::RDocTask.new do |rdoc|
46
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
47
+
48
+ rdoc.rdoc_dir = 'rdoc'
49
+ rdoc.title = "pinbo #{version}"
50
+ rdoc.rdoc_files.include('README*')
51
+ rdoc.rdoc_files.include('lib/**/*.rb')
52
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,13 @@
1
+ require 'rubygems'
2
+ require 'sinatra'
3
+ require 'pinbo'
4
+
5
+ Object.send :include, Pinbo::Timer
6
+
7
+ use Pinbo::Middleware
8
+ get '/' do
9
+ timer :layout => :index, :action => :generate_all do
10
+ 2 + 2 * 4
11
+ end
12
+ "Hello World!"
13
+ end
@@ -0,0 +1,14 @@
1
+ module Pinbo
2
+ class Middleware
3
+ def initialize(app)
4
+ @app = app
5
+ end
6
+
7
+ def call(env)
8
+ Pinbo.start :script_name => env['REQUEST_PATH']
9
+ res = @app.call(env)
10
+ Pinbo.stop :status => res[0], :server_name => env['SERVER_NAME'], :document_size => res[1]['Content-Length'].to_i
11
+ res
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,47 @@
1
+ module Pinbo
2
+ class Request
3
+ attr_accessor :options
4
+ def initialize(options)
5
+ self.options = options
6
+ end
7
+
8
+ def cmd
9
+ req = Proto::Pinbo::Request.new
10
+ req.hostname = Config[:host]
11
+ req.server_name = options[:data][:server_name]
12
+ req.script_name = options[:data][:script_name]
13
+ req.request_count = options[:data][:request_count]
14
+ req.document_size = options[:data][:document_size]
15
+ req.memory_peak = options[:data][:memory_peak]
16
+ req.request_time = options[:data][:request_time]
17
+ req.ru_utime = options[:data][:ru_utime]
18
+ req.ru_stime = options[:data][:ru_stime]
19
+ Pinbo.timers.each do |timer|
20
+ req.timer_hit_count << timer[:counter]
21
+ req.timer_value << timer[:period]
22
+ req.timer_tag_count << timer[:tags].size
23
+ timer[:normalized_tags].each do |tag|
24
+ req.timer_tag_name << tag[:name_id]
25
+ req.dictionary << tag[:name]
26
+ req.timer_tag_value << tag[:value_id]
27
+ req.dictionary << tag[:value]
28
+ end
29
+ end
30
+ req.status = options[:data][:status]
31
+ # require 'ruby-debug'
32
+ # debugger
33
+ req.serialize_to_string
34
+ end
35
+
36
+ def perform
37
+ sock = nil
38
+ begin
39
+ sock = UDPSocket.open
40
+ sock.send(cmd, 0, Config[:pinba_host], Config[:pinba_port])
41
+ rescue IOError, SystemCallError
42
+ ensure
43
+ sock.close if sock
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,5 @@
1
+ module Pinbo::Timer
2
+ def timer(tags = {}, &block)
3
+ Pinbo.timer(tags, &block)
4
+ end
5
+ end
data/lib/pinbo.rb ADDED
@@ -0,0 +1,100 @@
1
+ require 'rubygems'
2
+ require 'socket'
3
+ require 'timeout'
4
+ require 'ruby_protobuf'
5
+ require "socket"
6
+ require 'proto/pinbo.pb'
7
+
8
+ module Pinbo
9
+
10
+ require 'pinbo/timer'
11
+ require 'pinbo/request'
12
+ require 'pinbo/middleware'
13
+
14
+ Config = {
15
+ :host => Socket.gethostname,
16
+ :pinba_host => '74.86.33.154', #:pinba_host => 'localhost',
17
+ :pinba_port => 30002
18
+ }
19
+
20
+ class << self
21
+
22
+ def counter
23
+ Thread.current[:counter] ||= 1
24
+ end
25
+
26
+ def counter=(val)
27
+ Thread.current[:counter] = val
28
+ end
29
+
30
+ def data
31
+ Thread.current[:data]
32
+ end
33
+
34
+ def data=(val)
35
+ Thread.current[:data] = val
36
+ end
37
+
38
+ def timers
39
+ Thread.current[:timers]
40
+ end
41
+
42
+ def timers=(val)
43
+ Thread.current[:timers] = val
44
+ end
45
+
46
+ def increase_counter
47
+ self.counter += 1
48
+ end
49
+
50
+ def start(options = {})
51
+ Thread.current[:request_start] = Time.now
52
+ Thread.current[:timer_counter] = 0
53
+ self.data = {
54
+ :server_name => Socket.gethostname,
55
+ :script_name => '/test.rb',
56
+ :request_count => counter,
57
+ :document_size => 0,
58
+ :memory_peak => 0,
59
+ :request_time => 0,
60
+ :ru_utime => 0,
61
+ :ru_stime => 0,
62
+ :status => 200 }.merge(options)
63
+ self.timers = []
64
+ end
65
+
66
+ def timer_counter
67
+ Thread.current[:timer_counter] += 1
68
+ Thread.current[:timer_counter] - 1
69
+ end
70
+
71
+ def stop(options = {})
72
+ data.merge!( :request_time => Time.now - Thread.current[:request_start] ).merge!(options)
73
+ Request.new( :data => data, :timers => timers).perform
74
+ increase_counter
75
+ end
76
+
77
+ def timer(tags = {}, &block)
78
+ t = get_timer(tags)
79
+ t[:start] = Time.now
80
+ block.call
81
+ time = Time.now
82
+ t[:counter] += 1
83
+ t[:stop] = time
84
+ t[:period] += t[:stop] - t[:start]
85
+ end
86
+
87
+ def normalize_tags(raw_tags)
88
+ raw_tags.map { |k, v| { :name_id => timer_counter, :value_id => timer_counter, :name => k.to_s, :value => v.to_s } }
89
+ end
90
+
91
+ def get_timer(tags)
92
+ unless (t = self.timers.detect { |v| v[:tags] == tags } )
93
+ t = { :tags => tags, :normalized_tags => normalize_tags(tags), :counter => 0, :period => 0 }
94
+ self.timers << t
95
+ end
96
+ t
97
+ end
98
+ end
99
+
100
+ end
@@ -0,0 +1,54 @@
1
+ ### Generated by rprotoc. DO NOT EDIT!
2
+ ### <proto file: pinbo.proto>
3
+ # package Proto.Pinbo;
4
+ # option optimize_for = SPEED;
5
+ #
6
+ # message Request {
7
+ # required string hostname = 1;
8
+ # required string server_name = 2;
9
+ # required string script_name = 3;
10
+ # required uint32 request_count = 4;
11
+ # required uint32 document_size = 5;
12
+ # required uint32 memory_peak = 6;
13
+ # required float request_time = 7;
14
+ # required float ru_utime = 8;
15
+ # required float ru_stime = 9;
16
+ #
17
+ # repeated uint32 timer_hit_count = 10;
18
+ # repeated float timer_value = 11;
19
+ # repeated uint32 timer_tag_count = 12;
20
+ # repeated uint32 timer_tag_name = 13;
21
+ # repeated uint32 timer_tag_value = 14;
22
+ # repeated string dictionary = 15;
23
+ # optional uint32 status = 16;
24
+ # }
25
+
26
+ require 'protobuf/message/message'
27
+ require 'protobuf/message/enum'
28
+ require 'protobuf/message/service'
29
+ require 'protobuf/message/extend'
30
+
31
+ module Proto
32
+ module Pinbo
33
+ ::Protobuf::OPTIONS[:"optimize_for"] = :SPEED
34
+ class Request < ::Protobuf::Message
35
+ defined_in __FILE__
36
+ required :string, :hostname, 1
37
+ required :string, :server_name, 2
38
+ required :string, :script_name, 3
39
+ required :uint32, :request_count, 4
40
+ required :uint32, :document_size, 5
41
+ required :uint32, :memory_peak, 6
42
+ required :float, :request_time, 7
43
+ required :float, :ru_utime, 8
44
+ required :float, :ru_stime, 9
45
+ repeated :uint32, :timer_hit_count, 10
46
+ repeated :float, :timer_value, 11
47
+ repeated :uint32, :timer_tag_count, 12
48
+ repeated :uint32, :timer_tag_name, 13
49
+ repeated :uint32, :timer_tag_value, 14
50
+ repeated :string, :dictionary, 15
51
+ optional :uint32, :status, 16
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,22 @@
1
+ package Proto.Pinbo;
2
+ option optimize_for = SPEED;
3
+
4
+ message Request {
5
+ required string hostname = 1;
6
+ required string server_name = 2;
7
+ required string script_name = 3;
8
+ required uint32 request_count = 4;
9
+ required uint32 document_size = 5;
10
+ required uint32 memory_peak = 6;
11
+ required float request_time = 7;
12
+ required float ru_utime = 8;
13
+ required float ru_stime = 9;
14
+
15
+ repeated uint32 timer_hit_count = 10;
16
+ repeated float timer_value = 11;
17
+ repeated uint32 timer_tag_count = 12;
18
+ repeated uint32 timer_tag_name = 13;
19
+ repeated uint32 timer_tag_value = 14;
20
+ repeated string dictionary = 15;
21
+ optional uint32 status = 16;
22
+ }
data/pinbo.gemspec ADDED
@@ -0,0 +1,60 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{pinbo}
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Andrew Rudenko"]
12
+ s.date = %q{2009-11-08}
13
+ s.description = %q{ruby client for Pinba}
14
+ s.email = %q{ceo@prepor.ru}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".gitignore",
22
+ "LICENSE",
23
+ "README.rdoc",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "examples/sinatra_app.rb",
27
+ "lib/pinbo.rb",
28
+ "lib/pinbo/middleware.rb",
29
+ "lib/pinbo/request.rb",
30
+ "lib/pinbo/timer.rb",
31
+ "lib/proto/pinbo.pb.rb",
32
+ "lib/proto/pinbo.proto",
33
+ "spec/pinbo_spec.rb",
34
+ "spec/spec.opts",
35
+ "spec/spec_helper.rb"
36
+ ]
37
+ s.homepage = %q{http://github.com/prepor/pinbo}
38
+ s.rdoc_options = ["--charset=UTF-8"]
39
+ s.require_paths = ["lib"]
40
+ s.rubygems_version = %q{1.3.5}
41
+ s.summary = %q{ruby client for Pinba}
42
+ s.test_files = [
43
+ "spec/pinbo_spec.rb",
44
+ "spec/spec_helper.rb",
45
+ "examples/sinatra_app.rb"
46
+ ]
47
+
48
+ if s.respond_to? :specification_version then
49
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
50
+ s.specification_version = 3
51
+
52
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
53
+ s.add_development_dependency(%q<thoughtbot-shoulda>, [">= 0"])
54
+ else
55
+ s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
56
+ end
57
+ else
58
+ s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
59
+ end
60
+ end
@@ -0,0 +1,11 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "Pinbo" do
4
+
5
+ describe "common statistics" do
6
+ it "should description" do
7
+
8
+ end
9
+ end
10
+
11
+ end
data/spec/spec.opts ADDED
@@ -0,0 +1 @@
1
+ --color
@@ -0,0 +1,9 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+ require 'pinbo'
4
+ require 'spec'
5
+ require 'spec/autorun'
6
+
7
+ Spec::Runner.configure do |config|
8
+
9
+ end
metadata ADDED
@@ -0,0 +1,83 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pinbo
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Andrew Rudenko
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-11-18 00:00:00 +03:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: thoughtbot-shoulda
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ version:
25
+ description: ruby client for Pinba
26
+ email: ceo@prepor.ru
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files:
32
+ - LICENSE
33
+ - README.rdoc
34
+ files:
35
+ - .document
36
+ - .gitignore
37
+ - LICENSE
38
+ - README.rdoc
39
+ - Rakefile
40
+ - VERSION
41
+ - examples/sinatra_app.rb
42
+ - lib/pinbo.rb
43
+ - lib/pinbo/middleware.rb
44
+ - lib/pinbo/request.rb
45
+ - lib/pinbo/timer.rb
46
+ - lib/proto/pinbo.pb.rb
47
+ - lib/proto/pinbo.proto
48
+ - pinbo.gemspec
49
+ - spec/pinbo_spec.rb
50
+ - spec/spec.opts
51
+ - spec/spec_helper.rb
52
+ has_rdoc: true
53
+ homepage: http://github.com/prepor/pinbo
54
+ licenses: []
55
+
56
+ post_install_message:
57
+ rdoc_options:
58
+ - --charset=UTF-8
59
+ require_paths:
60
+ - lib
61
+ required_ruby_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: "0"
66
+ version:
67
+ required_rubygems_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: "0"
72
+ version:
73
+ requirements: []
74
+
75
+ rubyforge_project:
76
+ rubygems_version: 1.3.5
77
+ signing_key:
78
+ specification_version: 3
79
+ summary: ruby client for Pinba
80
+ test_files:
81
+ - spec/pinbo_spec.rb
82
+ - spec/spec_helper.rb
83
+ - examples/sinatra_app.rb