rails_analyzer_tools 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/Manifest.txt ADDED
@@ -0,0 +1,6 @@
1
+ Manifest.txt
2
+ Rakefile
3
+ README
4
+ bin/rails_stat
5
+ lib/io_tail.rb
6
+ lib/rails_stat.rb
data/README ADDED
@@ -0,0 +1,17 @@
1
+ Currently, Rails Analyzer Tools contains just one tool, RailsStat, and one
2
+ library extension, IOTail.
3
+
4
+ == RailsStat
5
+
6
+ RailsStat displays the approximate number of requests, queries, and lines
7
+ logged per second in 10 second intervals. Simply give it the path to your
8
+ production log for a live Rails site and you're done:
9
+
10
+ $ rails_stat /var/log/production.log
11
+ ~ 2.1 req/sec, 23.0 queries/sec, 32.8 lines/sec
12
+
13
+ == IOTail
14
+
15
+ IOTail tails a file like the tail system utility. This lets you collect data
16
+ from a live log file.
17
+
data/Rakefile ADDED
@@ -0,0 +1,62 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'rake/testtask'
4
+ require 'rake/rdoctask'
5
+ require 'rake/gempackagetask'
6
+ require 'rake/contrib/sshpublisher'
7
+
8
+ $VERBOSE = nil
9
+
10
+ spec = Gem::Specification.new do |s|
11
+ s.name = "rails_analyzer_tools"
12
+ s.version = "1.0.0"
13
+ s.summary = "Various tools and toys"
14
+ s.author = "Eric Hodel"
15
+ s.email = "eric@robotcoop.com"
16
+
17
+ s.has_rdoc = true
18
+ s.files = File.read("Manifest.txt").split($/)
19
+ s.require_path = 'lib'
20
+ s.executables = ["rails_stat"]
21
+ end
22
+
23
+ desc "Run tests"
24
+ task :default => [ :test ]
25
+
26
+ Rake::TestTask.new("test") do |t|
27
+ t.libs << "test"
28
+ t.libs << "lib"
29
+ t.pattern = "test/test_*.rb"
30
+ t.verbose = true
31
+ end
32
+
33
+ desc "Generate RDoc"
34
+ Rake::RDocTask.new :rdoc do |rd|
35
+ rd.rdoc_dir = "doc"
36
+ rd.rdoc_files.add "lib", "README"
37
+ rd.main = "README"
38
+ rd.options << "-d" if `which dot` =~ /\/dot/
39
+ end
40
+
41
+ desc "Build Gem"
42
+ Rake::GemPackageTask.new spec do |pkg|
43
+ pkg.need_zip = true
44
+ pkg.need_tar = true
45
+ end
46
+
47
+ desc "Sends RDoc to RubyForge"
48
+ task :send_rdoc => [ :rerdoc ] do
49
+ publisher = Rake::SshDirPublisher.new('drbrain@rubyforge.org',
50
+ '/var/www/gforge-projects/rails-analyzer/hacks',
51
+ 'doc')
52
+ publisher.upload
53
+ end
54
+
55
+ desc "Clean up"
56
+ task :clean => [ :clobber_rdoc, :clobber_package ]
57
+
58
+ desc "Clean up"
59
+ task :clobber => [ :clean ]
60
+
61
+ # vim: ts=4 sts=4 sw=4 syntax=Ruby
62
+
data/bin/rails_stat ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/local/bin/ruby -w
2
+
3
+ require 'io_tail'
4
+ require 'rails_stat'
5
+
6
+ filename = ARGV.shift
7
+
8
+ if filename.nil? then
9
+ STDERR.puts "Usage: #{$0} RAILS_LOG"
10
+ exit 1
11
+ end
12
+
13
+ RailsStat.start filename
14
+
data/lib/io_tail.rb ADDED
@@ -0,0 +1,37 @@
1
+ ##
2
+ # IOTail provides a tail_lines method as a mixin. By default it is included
3
+ # into IO and StringIO, if present. If you require StringIO after IOTail,
4
+ # then simply open StringIO and include IOTail.
5
+
6
+ module IOTail
7
+
8
+ ##
9
+ # Jumps to near the end of the IO, then yields each line, waiting for new
10
+ # lines if it reaches eof?
11
+
12
+ def tail_lines(&block) # :yields: line
13
+ self.seek(-1, IO::SEEK_END)
14
+ self.gets
15
+
16
+ loop do
17
+ self.each_line &block
18
+
19
+ if self.eof? then
20
+ sleep 0.25
21
+ self.pos = self.pos # reset eof?
22
+ end
23
+ end
24
+ end
25
+
26
+ end
27
+
28
+ class IO # :nodoc:
29
+ include IOTail
30
+ end
31
+
32
+ if defined? StringIO then
33
+ class StringIO # :nodoc:
34
+ include IOTail
35
+ end
36
+ end
37
+
data/lib/rails_stat.rb ADDED
@@ -0,0 +1,86 @@
1
+ require 'thread'
2
+
3
+ ##
4
+ # RailsStat displays a the current requests, queries and lines logged per
5
+ # second in 10 second intervals.
6
+
7
+ class RailsStat
8
+
9
+ ##
10
+ # Starts a new RailsStat for +filename+.
11
+
12
+ def self.start(filename)
13
+ File.open filename do |fp|
14
+ self.new(fp).start
15
+ end
16
+ end
17
+
18
+ ##
19
+ # Creates a new RailsStat that will listen on +io+.
20
+
21
+ def initialize(io)
22
+ @io = io
23
+ @mutex = Mutex.new
24
+ @lines = 0
25
+ @count = 0
26
+ @queries = 0
27
+ end
28
+
29
+ ##
30
+ # Starts the RailsStat running.
31
+
32
+ def start
33
+ trap('INT') { exit }
34
+ start_printer
35
+ read_log
36
+ end
37
+
38
+ private
39
+
40
+ ##
41
+ # Starts a thread that prints log information every 10 seconds.
42
+
43
+ def start_printer
44
+ Thread.start do
45
+ last_len = 0
46
+ lines_sec = 0
47
+ count_sec = 0
48
+ queries_sec = 0
49
+
50
+ loop do
51
+ sleep 10
52
+
53
+ @mutex.synchronize do
54
+ lines_sec = @lines / 10.0
55
+ count_sec = @count / 10.0
56
+ queries_sec = @queries / 10.0
57
+ @lines = 0
58
+ @count = 0
59
+ @queries = 0
60
+ end
61
+
62
+ status = sprintf("~ %0.1f req/sec, %0.1f queries/sec, %0.1f lines/sec",
63
+ count_sec, queries_sec, lines_sec)
64
+
65
+ print "\r#{' ' * last_len}\r#{status}"
66
+ STDOUT.flush
67
+
68
+ last_len = status.length
69
+ end
70
+ end
71
+ end
72
+
73
+ ##
74
+ # Starts a thread that reads from +io+, updating RailsStat counters as it
75
+ # goes.
76
+
77
+ def read_log
78
+ @io.tail_lines do |line|
79
+ @mutex.synchronize { @lines += 1 }
80
+ @mutex.synchronize { @count += 1 } if line =~ /Completed in /
81
+ @mutex.synchronize { @queries += 1 } if line =~ / (?:Update|Load|SQL|Count) /
82
+ end
83
+ end
84
+
85
+ end
86
+
metadata ADDED
@@ -0,0 +1,43 @@
1
+ --- !ruby/object:Gem::Specification
2
+ rubygems_version: 0.8.10
3
+ specification_version: 1
4
+ name: rails_analyzer_tools
5
+ version: !ruby/object:Gem::Version
6
+ version: 1.0.0
7
+ date: 2005-06-04
8
+ summary: Various tools and toys
9
+ require_paths:
10
+ - lib
11
+ email: eric@robotcoop.com
12
+ homepage:
13
+ rubyforge_project:
14
+ description:
15
+ autorequire:
16
+ default_executable:
17
+ bindir: bin
18
+ has_rdoc: true
19
+ required_ruby_version: !ruby/object:Gem::Version::Requirement
20
+ requirements:
21
+ -
22
+ - ">"
23
+ - !ruby/object:Gem::Version
24
+ version: 0.0.0
25
+ version:
26
+ platform: ruby
27
+ authors:
28
+ - Eric Hodel
29
+ files:
30
+ - Manifest.txt
31
+ - Rakefile
32
+ - README
33
+ - bin/rails_stat
34
+ - lib/io_tail.rb
35
+ - lib/rails_stat.rb
36
+ test_files: []
37
+ rdoc_options: []
38
+ extra_rdoc_files: []
39
+ executables:
40
+ - rails_stat
41
+ extensions: []
42
+ requirements: []
43
+ dependencies: []