stopwatch 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,3 @@
1
+ pkg/*
2
+ *.gem
3
+ .bundle
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "http://rubygems.org"
2
+
3
+ gemspec
data/README.rdoc ADDED
@@ -0,0 +1,20 @@
1
+ = Stopwatch
2
+
3
+ This is a simple gem to load a Rack middleware that works together with the
4
+ Rails 3 Notification API to display the page load time and amount of queries
5
+ executed
6
+
7
+ == Installation
8
+
9
+ just add the following to the development group in your Gemfile.
10
+
11
+ gem 'stopwatch', :git => https://github.com/bittersweet/stopwatch
12
+
13
+ == Caveats
14
+
15
+ * It removes etags so we can display it fresh every page load, otherwise the response would get cached by the browser because Rails would return a 304 Not Modified.
16
+
17
+ == Todo
18
+
19
+ * Basic configuration
20
+ * Differentiate between cached and non-cached queries
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
data/lib/load_speed.rb ADDED
@@ -0,0 +1,51 @@
1
+ module Rack
2
+ class LoadSpeed
3
+ def initialize(app)
4
+ @app = app
5
+ end
6
+
7
+ def call(env)
8
+ env.delete("HTTP_IF_NONE_MATCH")
9
+ status, headers, response = @app.call(env)
10
+
11
+ if status == 200
12
+ body = response.body
13
+ index = body.rindex("</body>")
14
+ if index
15
+ body.insert(index, performance_code)
16
+ headers["Content-Length"] = body.length.to_s
17
+ response = [body]
18
+ end
19
+ end
20
+
21
+ [status, headers, response]
22
+ end
23
+
24
+ protected
25
+
26
+ def performance_code
27
+ html = <<-EOF
28
+ <style>
29
+ #performance_code {
30
+ position: absolute;
31
+ top: 0;
32
+ right: 0;
33
+ height: 25px;
34
+ background-color: #DE7A93;
35
+ color: white;
36
+ padding: 0 10px 0 10px;
37
+ line-height: 20pt;
38
+ font-family: "menlo";
39
+ font-size: 10pt;
40
+ text-align: right;
41
+ }
42
+ </style>
43
+ <div id="performance_code">
44
+ <strong>#{StopwatchLog.event.duration.to_i}</strong> ms
45
+ <strong>#{StopwatchLog.query_count}</strong> queries
46
+ </div>
47
+ EOF
48
+ html
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,3 @@
1
+ module Stopwatch
2
+ VERSION = "0.0.1"
3
+ end
data/lib/stopwatch.rb ADDED
@@ -0,0 +1,22 @@
1
+ require 'load_speed'
2
+ require 'stopwatch_log'
3
+
4
+ module Stopwatch
5
+ class Railtie < Rails::Railtie
6
+ initializer "newplugin.initialize" do |app|
7
+ app.config.middleware.use "Rack::LoadSpeed"
8
+
9
+ ActiveSupport::Notifications.subscribe "start_processing.action_controller" do |*args|
10
+ StopwatchLog.reset_query_count
11
+ end
12
+
13
+ ActiveSupport::Notifications.subscribe "process_action.action_controller" do |*args|
14
+ StopwatchLog.event = ActiveSupport::Notifications::Event.new(*args)
15
+ end
16
+
17
+ ActiveSupport::Notifications.subscribe "sql.active_record" do |*args|
18
+ StopwatchLog.increment_query_count
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,17 @@
1
+ class StopwatchLog
2
+ @@query_count = 0
3
+
4
+ cattr_accessor :event
5
+
6
+ def self.query_count
7
+ @@query_count
8
+ end
9
+
10
+ def self.reset_query_count
11
+ @@query_count = 0
12
+ end
13
+
14
+ def self.increment_query_count
15
+ @@query_count += 1
16
+ end
17
+ end
data/stopwatch.gemspec ADDED
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "stopwatch/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "stopwatch"
7
+ s.version = Stopwatch::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Mark Mulder"]
10
+ s.email = ["markmulder@gmail.com"]
11
+ s.homepage = "http://ikbenbitterzoet.com"
12
+ s.summary = "Show the page load duration."
13
+ s.description = "This gem uses Rack middleware and the Rails 3 instrumentation to display page load time."
14
+
15
+ s.rubyforge_project = "stopwatch"
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ["lib"]
21
+ end
metadata ADDED
@@ -0,0 +1,76 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: stopwatch
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Mark Mulder
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-06-22 00:00:00 +02:00
19
+ default_executable:
20
+ dependencies: []
21
+
22
+ description: This gem uses Rack middleware and the Rails 3 instrumentation to display page load time.
23
+ email:
24
+ - markmulder@gmail.com
25
+ executables: []
26
+
27
+ extensions: []
28
+
29
+ extra_rdoc_files: []
30
+
31
+ files:
32
+ - .gitignore
33
+ - Gemfile
34
+ - README.rdoc
35
+ - Rakefile
36
+ - lib/load_speed.rb
37
+ - lib/stopwatch.rb
38
+ - lib/stopwatch/version.rb
39
+ - lib/stopwatch_log.rb
40
+ - stopwatch.gemspec
41
+ has_rdoc: true
42
+ homepage: http://ikbenbitterzoet.com
43
+ licenses: []
44
+
45
+ post_install_message:
46
+ rdoc_options: []
47
+
48
+ require_paths:
49
+ - lib
50
+ required_ruby_version: !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ hash: 3
56
+ segments:
57
+ - 0
58
+ version: "0"
59
+ required_rubygems_version: !ruby/object:Gem::Requirement
60
+ none: false
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ hash: 3
65
+ segments:
66
+ - 0
67
+ version: "0"
68
+ requirements: []
69
+
70
+ rubyforge_project: stopwatch
71
+ rubygems_version: 1.3.7
72
+ signing_key:
73
+ specification_version: 3
74
+ summary: Show the page load duration.
75
+ test_files: []
76
+