oink 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,9 @@
1
+ require "oink/oinked_request/oinked_request"
2
+
3
+ class OinkedARRequest < OinkedRequest
4
+
5
+ def display_oink_number
6
+ @oink_number
7
+ end
8
+
9
+ end
@@ -0,0 +1,9 @@
1
+ require "oink/oinked_request/oinked_request"
2
+
3
+ class OinkedMemoryRequest < OinkedRequest
4
+
5
+ def display_oink_number
6
+ "#{@oink_number} KB"
7
+ end
8
+
9
+ end
@@ -0,0 +1,16 @@
1
+ class OinkedRequest
2
+ include Comparable
3
+
4
+ attr_accessor :action, :datetime, :log_lines, :oink_number
5
+
6
+ def initialize(action, datetime, log_lines, oink_number)
7
+ @action = action
8
+ @datetime = datetime
9
+ @log_lines = log_lines
10
+ @oink_number = oink_number
11
+ end
12
+
13
+ def <=>(other)
14
+ self.oink_number <=> other.oink_number
15
+ end
16
+ end
@@ -0,0 +1,37 @@
1
+ class PriorityQueue
2
+
3
+ include Enumerable
4
+
5
+ def initialize(size)
6
+ @size = size
7
+ @queue = []
8
+ end
9
+
10
+ def push(item)
11
+ if @queue.size < @size
12
+ @queue << item
13
+ elsif item > @queue.last
14
+ @queue[@size - 1] = item
15
+ end
16
+ prioritize
17
+ end
18
+
19
+ def to_a
20
+ @queue
21
+ end
22
+
23
+ def size
24
+ @queue.size
25
+ end
26
+
27
+ def each
28
+ @queue.each { |i| yield i }
29
+ end
30
+
31
+ protected
32
+
33
+ def prioritize
34
+ @queue.sort! { |a, b| b <=> a }
35
+ end
36
+
37
+ end
data/lib/oink/rails.rb ADDED
@@ -0,0 +1,2 @@
1
+ require "oink/rails/memory_usage_logger"
2
+ require "oink/rails/instance_type_counter"
@@ -0,0 +1,86 @@
1
+ module Oink
2
+
3
+ def self.extended_active_record?
4
+ @oink_extended_active_record
5
+ end
6
+
7
+ def self.extended_active_record!
8
+ @oink_extended_active_record = true
9
+ end
10
+
11
+ module InstanceTypeCounter
12
+ def self.included(klass)
13
+ ActiveRecord::Base.send(:include, OinkInstanceTypeCounterInstanceMethods)
14
+
15
+ klass.class_eval do
16
+ after_filter :report_instance_type_count
17
+ end
18
+ end
19
+
20
+ def before_report_active_record_count(instantiation_data)
21
+ end
22
+
23
+ private
24
+
25
+ def report_instance_type_count
26
+ report_hash = ActiveRecord::Base.instantiated_hash.merge("Total" => ActiveRecord::Base.total_objects_instantiated)
27
+ breakdown = report_hash.sort{|a,b| b[1]<=>a[1]}.collect {|k,v| "#{k}: #{v}" }.join(" | ")
28
+ before_report_active_record_count(breakdown)
29
+ if logger
30
+ logger.info("Instantiation Breakdown: #{breakdown}")
31
+ end
32
+ ActiveRecord::Base.reset_instance_type_count
33
+ end
34
+ end
35
+
36
+ module OinkInstanceTypeCounterInstanceMethods
37
+
38
+ def self.included(klass)
39
+ klass.class_eval do
40
+
41
+ @@instantiated = {}
42
+ @@total = nil
43
+
44
+ def self.reset_instance_type_count
45
+ @@instantiated = {}
46
+ @@total = nil
47
+ end
48
+
49
+ def self.increment_instance_type_count
50
+ @@instantiated[base_class.name] ||= 0
51
+ @@instantiated[base_class.name] += 1
52
+ end
53
+
54
+ def self.instantiated_hash
55
+ @@instantiated
56
+ end
57
+
58
+ def self.total_objects_instantiated
59
+ @@total ||= @@instantiated.values.sum
60
+ end
61
+
62
+ unless Oink.extended_active_record?
63
+ class << self
64
+ alias_method :allocate_before_oink, :allocate
65
+
66
+ def allocate
67
+ value = allocate_before_oink
68
+ increment_instance_type_count
69
+ value
70
+ end
71
+ end
72
+
73
+ alias_method :initialize_before_oink, :initialize
74
+
75
+ def initialize(*args, &block)
76
+ value = initialize_before_oink(*args, &block)
77
+ self.class.increment_instance_type_count
78
+ value
79
+ end
80
+
81
+ Oink.extended_active_record!
82
+ end
83
+ end
84
+ end
85
+ end
86
+ end
@@ -0,0 +1,40 @@
1
+ begin
2
+ require 'win32ole'
3
+ rescue LoadError
4
+ end
5
+
6
+ module Oink
7
+ module MemoryUsageLogger
8
+ def self.included(klass)
9
+ klass.class_eval do
10
+ after_filter :log_memory_usage
11
+ end
12
+ end
13
+
14
+ private
15
+ def get_memory_usage
16
+ if defined? WIN32OLE
17
+ wmi = WIN32OLE.connect("winmgmts:root/cimv2")
18
+ mem = 0
19
+ query = "select * from Win32_Process where ProcessID = #{$$}"
20
+ wmi.ExecQuery(query).each do |wproc|
21
+ mem = wproc.WorkingSetSize
22
+ end
23
+ mem.to_i / 1000
24
+ elsif proc_file = File.new("/proc/#{$$}/smaps") rescue nil
25
+ proc_file.map do |line|
26
+ size = line[/Size: *(\d+)/, 1] and size.to_i
27
+ end.compact.sum
28
+ else
29
+ `ps -o vsz= -p #{$$}`.to_i
30
+ end
31
+ end
32
+
33
+ def log_memory_usage
34
+ if logger
35
+ memory_usage = get_memory_usage
36
+ logger.info("Memory usage: #{memory_usage} | PID: #{$$}")
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,191 @@
1
+ require File.expand_path(File.dirname(__FILE__) + "/../spec_helper")
2
+
3
+ describe Oink::ActiveRecordInstantiationReporter do
4
+
5
+ describe "short summary with frequent offenders" do
6
+
7
+ it "should report actions which exceed the threshold once" do
8
+ str = <<-STR
9
+ Feb 01 01:58:29 ey04-s00297 rails[4413]: Processing Users#show (for 92.84.151.171 at 2009-02-01 01:58:29) [GET]
10
+ Feb 01 01:58:30 ey04-s00297 rails[4413]: Instantiation Breakdown: Total: 51 | User: 51
11
+ Feb 01 01:58:31 ey04-s00297 rails[4413]: Completed in 984ms (View: 840, DB: 4) | 200 OK
12
+ STR
13
+
14
+ io = StringIO.new(str)
15
+ output = PsuedoOutput.new
16
+ Oink::ActiveRecordInstantiationReporter.new(io, 50).print(output)
17
+ output.should include("1, Users#show")
18
+ end
19
+
20
+ it "should not report actions which do not exceed the threshold" do
21
+ str = <<-STR
22
+ Feb 01 01:58:29 ey04-s00297 rails[4413]: Processing Users#show (for 92.84.151.171 at 2009-02-01 01:58:29) [GET]
23
+ Feb 01 01:58:30 ey04-s00297 rails[4413]: Instantiation Breakdown: Total: 50 | User: 50
24
+ Feb 01 01:58:31 ey04-s00297 rails[4413]: Completed in 984ms (View: 840, DB: 4) | 200 OK
25
+ STR
26
+
27
+ io = StringIO.new(str)
28
+ output = PsuedoOutput.new
29
+ Oink::ActiveRecordInstantiationReporter.new(io, 50).print(output)
30
+ output.should_not include("1, Users#show")
31
+ end
32
+
33
+ it "should report actions which exceed the threshold multiple times" do
34
+ str = <<-STR
35
+ Feb 01 01:58:29 ey04-s00297 rails[4413]: Processing Users#show (for 92.84.151.171 at 2009-02-01 01:58:29) [GET]
36
+ Feb 01 01:58:30 ey04-s00297 rails[4413]: Instantiation Breakdown: Total: 51 | User: 51
37
+ Feb 01 01:58:31 ey04-s00297 rails[4413]: Completed in 984ms (View: 840, DB: 4) | 200 OK
38
+ Feb 01 01:58:29 ey04-s00297 rails[4413]: Processing Users#show (for 92.84.151.171 at 2009-02-01 01:58:29) [GET]
39
+ Feb 01 01:58:30 ey04-s00297 rails[4413]: Instantiation Breakdown: Total: 51 | User: 51
40
+ Feb 01 01:58:31 ey04-s00297 rails[4413]: Completed in 984ms (View: 840, DB: 4) | 200 OK
41
+ STR
42
+
43
+ io = StringIO.new(str)
44
+ output = PsuedoOutput.new
45
+ Oink::ActiveRecordInstantiationReporter.new(io, 50).print(output)
46
+ output.should include("2, Users#show")
47
+ end
48
+
49
+ it "should order actions by most exceeded" do
50
+ str = <<-STR
51
+ Feb 01 01:58:29 ey04-s00297 rails[4413]: Processing Media#show (for 92.84.151.171 at 2009-02-01 01:58:29) [GET]
52
+ Feb 01 01:58:30 ey04-s00297 rails[4413]: Instantiation Breakdown: Total: 51 | User: 51
53
+ Feb 01 01:58:31 ey04-s00297 rails[4413]: Completed in 984ms (View: 840, DB: 4) | 200 OK
54
+ Feb 01 01:58:29 ey04-s00297 rails[4413]: Processing Media#show (for 92.84.151.171 at 2009-02-01 01:58:29) [GET]
55
+ Feb 01 01:58:30 ey04-s00297 rails[4413]: Instantiation Breakdown: Total: 51 | User: 51
56
+ Feb 01 01:58:31 ey04-s00297 rails[4413]: Completed in 984ms (View: 840, DB: 4) | 200 OK
57
+ Feb 01 01:58:29 ey04-s00297 rails[4413]: Processing Users#show (for 92.84.151.171 at 2009-02-01 01:58:29) [GET]
58
+ Feb 01 01:58:30 ey04-s00297 rails[4413]: Instantiation Breakdown: Total: 51 | User: 51
59
+ Feb 01 01:58:31 ey04-s00297 rails[4413]: Completed in 984ms (View: 840, DB: 4) | 200 OK
60
+ STR
61
+
62
+ io = StringIO.new(str)
63
+ output = PsuedoOutput.new
64
+ Oink::ActiveRecordInstantiationReporter.new(io, 50).print(output)
65
+ output[-2].should == "2, Media#show"
66
+ output[-1].should == "1, Users#show"
67
+ end
68
+
69
+ it "should not be bothered by incomplete requests" do
70
+ str = <<-STR
71
+ Feb 01 01:58:29 ey04-s00297 rails[4413]: Processing Media#show (for 92.84.151.171 at 2009-02-01 01:58:29) [GET]
72
+ Feb 01 01:58:30 ey04-s00297 rails[4413]: Instantiation Breakdown: Total: 24 | User: 24
73
+ Feb 01 01:58:31 ey04-s00297 rails[4413]: Completed in 984ms (View: 840, DB: 4) | 200 OK
74
+ Feb 01 01:58:29 ey04-s00297 rails[4413]: Processing Media#show (for 92.84.151.171 at 2009-02-01 01:58:29) [GET]
75
+ Feb 01 01:58:29 ey04-s00297 rails[4413]: Processing Users#show (for 92.84.151.171 at 2009-02-01 01:58:29) [GET]
76
+ Feb 01 01:58:30 ey04-s00297 rails[4413]: Instantiation Breakdown: Total: 51 | User: 51
77
+ Feb 01 01:58:31 ey04-s00297 rails[4413]: Completed in 984ms (View: 840, DB: 4) | 200 OK
78
+ STR
79
+
80
+ io = StringIO.new(str)
81
+ output = PsuedoOutput.new
82
+ Oink::ActiveRecordInstantiationReporter.new(io, 50).print(output)
83
+ output.should include("1, Users#show")
84
+ end
85
+
86
+ end
87
+
88
+ describe "summary with top 10 offenses" do
89
+
90
+ it "should only report requests over threshold" do
91
+ str = <<-STR
92
+ Feb 01 01:58:29 ey04-s00297 rails[4413]: Processing Users#show (for 92.84.151.171 at 2009-02-01 01:58:29) [GET]
93
+ Feb 01 01:58:30 ey04-s00297 rails[4413]: Instantiation Breakdown: Total: 51 | User: 51
94
+ Feb 01 01:58:31 ey04-s00297 rails[4413]: Completed in 984ms (View: 840, DB: 4) | 200 OK
95
+ STR
96
+
97
+ io = StringIO.new(str)
98
+ output = PsuedoOutput.new
99
+ Oink::ActiveRecordInstantiationReporter.new(io, 50).print(output)
100
+ output.should include("1. Feb 01 01:58:31, 51, Users#show")
101
+ end
102
+
103
+ it "should not include requests which are not over threshold" do
104
+ str = <<-STR
105
+ Feb 01 01:58:29 ey04-s00297 rails[4413]: Processing Users#show (for 92.84.151.171 at 2009-02-01 01:58:29) [GET]
106
+ Feb 01 01:58:30 ey04-s00297 rails[4413]: Instantiation Breakdown: Total: 50 | User: 50
107
+ Feb 01 01:58:31 ey04-s00297 rails[4413]: Completed in 984ms (View: 840, DB: 4) | 200 OK
108
+ STR
109
+
110
+ io = StringIO.new(str)
111
+ output = PsuedoOutput.new
112
+ Oink::ActiveRecordInstantiationReporter.new(io, 50).print(output)
113
+ output.should_not include("1. Feb 01 01:58:31, 50, Users#show")
114
+ end
115
+
116
+ it "should order offenses from biggest to smallest" do
117
+ str = <<-STR
118
+ Feb 01 01:58:29 ey04-s00297 rails[4413]: Processing DetailsController#show (for 92.84.151.171 at 2009-02-01 01:58:29) [GET]
119
+ Feb 01 01:58:30 ey04-s00297 rails[4413]: Instantiation Breakdown: Total: 75 | User: 75
120
+ Feb 01 01:58:31 ey04-s00297 rails[4413]: Completed in 984ms (View: 840, DB: 4) | 200 OK
121
+ Feb 01 01:58:32 ey04-s00297 rails[4413]: Processing MediaController#show (for 92.84.151.171 at 2009-02-01 01:58:29) [GET]
122
+ Feb 01 01:58:33 ey04-s00297 rails[4413]: Instantiation Breakdown: Total: 100 | User: 100
123
+ Feb 01 01:58:34 ey04-s00297 rails[4413]: Completed in 984ms (View: 840, DB: 4) | 200 OK
124
+ STR
125
+
126
+ io = StringIO.new(str)
127
+ output = PsuedoOutput.new
128
+ Oink::ActiveRecordInstantiationReporter.new(io, 50).print(output)
129
+ output[4].should == "1. Feb 01 01:58:34, 100, MediaController#show"
130
+ output[5].should == "2. Feb 01 01:58:31, 75, DetailsController#show"
131
+ end
132
+
133
+ end
134
+
135
+ describe "verbose format" do
136
+ it "should print the full lines of actions exceeding the threshold" do
137
+ str = <<-STR
138
+ Feb 01 01:58:32 ey04-s00297 rails[4413]: Processing MediaController#show (for 92.84.151.171 at 2009-02-01 01:58:29) [GET]
139
+ Feb 01 01:58:33 ey04-s00297 rails[4413]: Instantiation Breakdown: Total: 100 | User: 100
140
+ Feb 01 01:58:34 ey04-s00297 rails[4413]: Completed in 984ms (View: 840, DB: 4) | 200 OK
141
+ STR
142
+ io = StringIO.new(str)
143
+ output = PsuedoOutput.new
144
+ Oink::ActiveRecordInstantiationReporter.new(io, 50, :format => :verbose).print(output)
145
+ output[3..5].should == str.split("\n")[0..2].map { |o| o.strip }
146
+ end
147
+
148
+ it "should handle actions which do not complete properly" do
149
+ str = <<-STR
150
+ Feb 01 01:58:29 ey04-s00297 rails[4413]: Processing Media#show (for 92.84.151.171 at 2009-02-01 01:58:29) [GET]
151
+ Feb 01 01:58:30 ey04-s00297 rails[4413]: Instantiation Breakdown: Total: 24 | User: 24
152
+ Feb 01 01:58:31 ey04-s00297 rails[4413]: Completed in 984ms (View: 840, DB: 4) | 200 OK
153
+ Feb 01 01:58:29 ey04-s00297 rails[4413]: Processing Media#show (for 92.84.151.171 at 2009-02-01 01:58:29) [GET]
154
+ Feb 01 01:58:29 ey04-s00297 rails[4413]: Processing Users#show (for 92.84.151.171 at 2009-02-01 01:58:29) [GET]
155
+ Feb 01 01:58:30 ey04-s00297 rails[4413]: Instantiation Breakdown: Total: 51 | User: 51
156
+ Feb 01 01:58:31 ey04-s00297 rails[4413]: Completed in 984ms (View: 840, DB: 4) | 200 OK
157
+ STR
158
+
159
+ io = StringIO.new(str)
160
+ output = PsuedoOutput.new
161
+ Oink::ActiveRecordInstantiationReporter.new(io, 50, :format => :verbose).print(output)
162
+ output[3..5].should == str.split("\n")[4..6].map { |o| o.strip }
163
+ end
164
+ end
165
+
166
+ describe "multiple io streams" do
167
+ it "should accept multiple files" do
168
+
169
+ str1 = <<-STR
170
+ Feb 01 01:58:32 ey04-s00297 rails[4413]: Processing MediaController#show (for 92.84.151.171 at 2009-02-01 01:58:29) [GET]
171
+ Feb 01 01:58:33 ey04-s00297 rails[4413]: Instantiation Breakdown: Total: 100 | User: 100
172
+ Feb 01 01:58:34 ey04-s00297 rails[4413]: Completed in 984ms (View: 840, DB: 4) | 200 OK
173
+ STR
174
+
175
+ str2 = <<-STR
176
+ Feb 01 01:58:32 ey04-s00297 rails[4413]: Processing MediaController#show (for 92.84.151.171 at 2009-02-01 01:58:29) [GET]
177
+ Feb 01 01:58:33 ey04-s00297 rails[4413]: Instantiation Breakdown: Total: 100 | User: 100
178
+ Feb 01 01:58:34 ey04-s00297 rails[4413]: Completed in 984ms (View: 840, DB: 4) | 200 OK
179
+ STR
180
+
181
+ io1 = StringIO.new(str1)
182
+ io2 = StringIO.new(str2)
183
+ output = PsuedoOutput.new
184
+ Oink::ActiveRecordInstantiationReporter.new([io1, io2], 50).print(output)
185
+ output.should include("2, MediaController#show")
186
+ end
187
+
188
+ end
189
+
190
+
191
+ end
@@ -0,0 +1,265 @@
1
+ require File.expand_path(File.dirname(__FILE__) + "/../spec_helper")
2
+
3
+ describe Oink::MemoryUsageReporter do
4
+
5
+ TEN_MEGS = 10 * 1024
6
+
7
+ describe "short summary with frequent offenders" do
8
+
9
+ it "should report actions which exceed the threshold once" do
10
+ str = <<-STR
11
+ Feb 01 01:58:29 ey04-s00297 rails[4413]: Processing Users#show (for 92.84.151.171 at 2009-02-01 01:58:29) [GET]
12
+ Feb 01 01:58:30 ey04-s00297 rails[4413]: Memory usage: 0 | PID: 4413
13
+ Feb 01 01:58:30 ey04-s00297 rails[4413]: Completed in 984ms (View: 840, DB: 4) | 200 OK
14
+ Feb 01 01:58:29 ey04-s00297 rails[4413]: Processing MediaController#show (for 92.84.151.171 at 2009-02-01 01:58:29) [GET]
15
+ Feb 01 01:58:30 ey04-s00297 rails[4413]: Memory usage: #{TEN_MEGS + 1} | PID: 4413
16
+ Feb 01 01:58:30 ey04-s00297 rails[4413]: Completed in 984ms (View: 840, DB: 4) | 200 OK
17
+ STR
18
+
19
+ io = StringIO.new(str)
20
+ output = PsuedoOutput.new
21
+ Oink::MemoryUsageReporter.new(io, TEN_MEGS).print(output)
22
+ output.should include("1, MediaController#show")
23
+ end
24
+
25
+ it "should not report actions which do not exceed the threshold" do
26
+ threshold = 10
27
+
28
+ str = <<-STR
29
+ Feb 01 01:58:29 ey04-s00297 rails[4413]: Processing Users#show (for 92.84.151.171 at 2009-02-01 01:58:29) [GET]
30
+ Feb 01 01:58:30 ey04-s00297 rails[4413]: Memory usage: 0 | PID: 4413
31
+ Feb 01 01:58:30 ey04-s00297 rails[4413]: Completed in 984ms (View: 840, DB: 4) | 200 OK
32
+ Feb 01 01:58:29 ey04-s00297 rails[4413]: Processing MediaController#show (for 92.84.151.171 at 2009-02-01 01:58:29) [GET]
33
+ Feb 01 01:58:30 ey04-s00297 rails[4413]: Memory usage: #{TEN_MEGS} | PID: 4413
34
+ Feb 01 01:58:30 ey04-s00297 rails[4413]: Completed in 984ms (View: 840, DB: 4) | 200 OK
35
+ STR
36
+
37
+ io = StringIO.new(str)
38
+ output = PsuedoOutput.new
39
+ Oink::MemoryUsageReporter.new(io, TEN_MEGS).print(output)
40
+ output.should_not include("1, MediaController#show")
41
+ end
42
+
43
+ it "should report actions which exceed the threshold multiple times" do
44
+ str = <<-STR
45
+ Feb 01 01:58:29 ey04-s00297 rails[4413]: Processing Users#show (for 92.84.151.171 at 2009-02-01 01:58:29) [GET]
46
+ Feb 01 01:58:30 ey04-s00297 rails[4413]: Memory usage: 0 | PID: 4413
47
+ Feb 01 01:58:30 ey04-s00297 rails[4413]: Completed in 984ms (View: 840, DB: 4) | 200 OK
48
+ Feb 01 01:58:29 ey04-s00297 rails[4413]: Processing MediaController#show (for 92.84.151.171 at 2009-02-01 01:58:29) [GET]
49
+ Feb 01 01:58:30 ey04-s00297 rails[4413]: Memory usage: #{TEN_MEGS + 1} | PID: 4413
50
+ Feb 01 01:58:30 ey04-s00297 rails[4413]: Completed in 984ms (View: 840, DB: 4) | 200 OK
51
+ Feb 01 01:58:29 ey04-s00297 rails[4413]: Processing MediaController#show (for 92.84.151.171 at 2009-02-01 01:58:29) [GET]
52
+ Feb 01 01:58:30 ey04-s00297 rails[4413]: Memory usage: #{(TEN_MEGS * 2) + 2} | PID: 4413
53
+ Feb 01 01:58:30 ey04-s00297 rails[4413]: Completed in 984ms (View: 840, DB: 4) | 200 OK
54
+ STR
55
+
56
+ io = StringIO.new(str)
57
+ output = PsuedoOutput.new
58
+ Oink::MemoryUsageReporter.new(io, TEN_MEGS).print(output)
59
+ output.should include("2, MediaController#show")
60
+ end
61
+
62
+ it "should order actions by most exceeded" do
63
+ str = <<-STR
64
+ Feb 01 01:58:29 ey04-s00297 rails[4413]: Processing Users#show (for 92.84.151.171 at 2009-02-01 01:58:29) [GET]
65
+ Feb 01 01:58:30 ey04-s00297 rails[4413]: Memory usage: 0 | PID: 4413
66
+ Feb 01 01:58:30 ey04-s00297 rails[4413]: Completed in 984ms (View: 840, DB: 4) | 200 OK
67
+ Feb 01 01:58:29 ey04-s00297 rails[4413]: Processing Users#show (for 92.84.151.171 at 2009-02-01 01:58:29) [GET]
68
+ Feb 01 01:58:30 ey04-s00297 rails[4413]: Memory usage: #{TEN_MEGS + 1} | PID: 4413
69
+ Feb 01 01:58:30 ey04-s00297 rails[4413]: Completed in 984ms (View: 840, DB: 4) | 200 OK
70
+ Feb 01 01:58:29 ey04-s00297 rails[4413]: Processing MediaController#show (for 92.84.151.171 at 2009-02-01 01:58:29) [GET]
71
+ Feb 01 01:58:30 ey04-s00297 rails[4413]: Memory usage: #{(TEN_MEGS * 2) + 2} | PID: 4413
72
+ Feb 01 01:58:30 ey04-s00297 rails[4413]: Completed in 984ms (View: 840, DB: 4) | 200 OK
73
+ Feb 01 01:58:29 ey04-s00297 rails[4413]: Processing MediaController#show (for 92.84.151.171 at 2009-02-01 01:58:29) [GET]
74
+ Feb 01 01:58:30 ey04-s00297 rails[4413]: Memory usage: #{(TEN_MEGS * 3) + 3} | PID: 4413
75
+ Feb 01 01:58:30 ey04-s00297 rails[4413]: Completed in 984ms (View: 840, DB: 4) | 200 OK
76
+ STR
77
+
78
+ io = StringIO.new(str)
79
+ output = PsuedoOutput.new
80
+ Oink::MemoryUsageReporter.new(io, TEN_MEGS).print(output)
81
+ output[-2].should == "2, MediaController#show"
82
+ output[-1].should == "1, Users#show"
83
+ end
84
+
85
+ it "should not report actions which do not complete properly" do
86
+ threshold = 10
87
+
88
+ str = <<-STR
89
+ Feb 01 01:58:29 ey04-s00297 rails[4413]: Processing Users#show (for 92.84.151.171 at 2009-02-01 01:58:29) [GET]
90
+ Feb 01 01:58:30 ey04-s00297 rails[4413]: Memory usage: 0 | PID: 4413
91
+ Feb 01 01:58:29 ey04-s00297 rails[4413]: Processing MediaController#show (for 92.84.151.171 at 2009-02-01 01:58:29) [GET]
92
+ Feb 01 01:58:29 ey04-s00297 rails[4413]: Processing MediaController#show (for 92.84.151.171 at 2009-02-01 01:58:29) [GET]
93
+ Feb 01 01:58:30 ey04-s00297 rails[4413]: Memory usage: #{TEN_MEGS + 1} | PID: 4413
94
+ Feb 01 01:58:30 ey04-s00297 rails[4413]: Completed in 984ms (View: 840, DB: 4) | 200 OK
95
+ STR
96
+
97
+ io = StringIO.new(str)
98
+ output = PsuedoOutput.new
99
+ Oink::MemoryUsageReporter.new(io, TEN_MEGS).print(output)
100
+ output.should_not include("1, MediaController#show")
101
+ end
102
+
103
+ it "should not report actions from different pids" do
104
+ str = <<-STR
105
+ Feb 01 01:58:29 ey04-s00297 rails[4413]: Processing Users#show (for 92.84.151.171 at 2009-02-01 01:58:29) [GET]
106
+ Feb 01 01:58:30 ey04-s00297 rails[4413]: Memory usage: 0 | PID: 4413
107
+ Feb 01 01:58:30 ey04-s00297 rails[4413]: Completed in 984ms (View: 840, DB: 4) | 200 OK
108
+ Feb 01 01:58:29 ey04-s00297 rails[5513]: Processing MediaController#show (for 92.84.151.171 at 2009-02-01 01:58:29) [GET]
109
+ Feb 01 01:58:30 ey04-s00297 rails[5513]: Memory usage: #{TEN_MEGS + 1} | PID: 4413
110
+ Feb 01 01:58:30 ey04-s00297 rails[5513]: Completed in 984ms (View: 840, DB: 4) | 200 OK
111
+ STR
112
+
113
+ io = StringIO.new(str)
114
+ output = PsuedoOutput.new
115
+ Oink::MemoryUsageReporter.new(io, TEN_MEGS).print(output)
116
+ output.should_not include("1, MediaController#show")
117
+ end
118
+
119
+ describe "summary with top 10 offenses" do
120
+
121
+ it "should only report requests over threshold" do
122
+ str = <<-STR
123
+ Feb 01 01:58:29 ey04-s00297 rails[4413]: Processing Users#show (for 92.84.151.171 at 2009-02-01 01:58:29) [GET]
124
+ Feb 01 01:58:30 ey04-s00297 rails[4413]: Memory usage: 0 | PID: 4413
125
+ Feb 01 01:58:31 ey04-s00297 rails[4413]: Completed in 984ms (View: 840, DB: 4) | 200 OK
126
+ Feb 01 01:58:32 ey04-s00297 rails[4413]: Processing MediaController#show (for 92.84.151.171 at 2009-02-01 01:58:29) [GET]
127
+ Feb 01 01:58:33 ey04-s00297 rails[4413]: Memory usage: #{TEN_MEGS + 1} | PID: 4413
128
+ Feb 01 01:58:34 ey04-s00297 rails[4413]: Completed in 984ms (View: 840, DB: 4) | 200 OK
129
+ STR
130
+
131
+ io = StringIO.new(str)
132
+ output = PsuedoOutput.new
133
+ Oink::MemoryUsageReporter.new(io, TEN_MEGS).print(output)
134
+ output.should include("1. Feb 01 01:58:34, #{TEN_MEGS + 1} KB, MediaController#show")
135
+ end
136
+
137
+ it "should not include requests which are not over the threshold" do
138
+ str = <<-STR
139
+ Feb 01 01:58:29 ey04-s00297 rails[4413]: Processing Users#show (for 92.84.151.171 at 2009-02-01 01:58:29) [GET]
140
+ Feb 01 01:58:30 ey04-s00297 rails[4413]: Memory usage: 0 | PID: 4413
141
+ Feb 01 01:58:31 ey04-s00297 rails[4413]: Completed in 984ms (View: 840, DB: 4) | 200 OK
142
+ Feb 01 01:58:32 ey04-s00297 rails[4413]: Processing MediaController#show (for 92.84.151.171 at 2009-02-01 01:58:29) [GET]
143
+ Feb 01 01:58:33 ey04-s00297 rails[4413]: Memory usage: #{TEN_MEGS} | PID: 4413
144
+ Feb 01 01:58:34 ey04-s00297 rails[4413]: Completed in 984ms (View: 840, DB: 4) | 200 OK
145
+ STR
146
+
147
+ io = StringIO.new(str)
148
+ output = PsuedoOutput.new
149
+ Oink::MemoryUsageReporter.new(io, TEN_MEGS).print(output)
150
+ output.should_not include("1. Feb 01 01:58:34, #{TEN_MEGS + 1} KB, MediaController#show")
151
+ end
152
+
153
+ it "should order offenses from biggest to smallest" do
154
+ str = <<-STR
155
+ Feb 01 01:58:29 ey04-s00297 rails[4413]: Processing Users#show (for 92.84.151.171 at 2009-02-01 01:58:29) [GET]
156
+ Feb 01 01:58:30 ey04-s00297 rails[4413]: Memory usage: 0 | PID: 4413
157
+ Feb 01 01:58:31 ey04-s00297 rails[4413]: Completed in 984ms (View: 840, DB: 4) | 200 OK
158
+ Feb 01 01:58:32 ey04-s00297 rails[4413]: Processing MediaController#show (for 92.84.151.171 at 2009-02-01 01:58:29) [GET]
159
+ Feb 01 01:58:33 ey04-s00297 rails[4413]: Memory usage: #{TEN_MEGS + 1} | PID: 4413
160
+ Feb 01 01:58:34 ey04-s00297 rails[4413]: Completed in 984ms (View: 840, DB: 4) | 200 OK
161
+ Feb 01 01:58:35 ey04-s00297 rails[4413]: Processing DetailsController#show (for 92.84.151.171 at 2009-02-01 01:58:29) [GET]
162
+ Feb 01 01:58:36 ey04-s00297 rails[4413]: Memory usage: #{(TEN_MEGS * 2) + 2} | PID: 4413
163
+ Feb 01 01:58:37 ey04-s00297 rails[4413]: Completed in 984ms (View: 840, DB: 4) | 200 OK
164
+ STR
165
+
166
+ io = StringIO.new(str)
167
+ output = PsuedoOutput.new
168
+ Oink::MemoryUsageReporter.new(io, TEN_MEGS).print(output)
169
+ output[4].should == "1. Feb 01 01:58:34, #{TEN_MEGS + 1} KB, MediaController#show"
170
+ output[5].should == "2. Feb 01 01:58:37, #{TEN_MEGS + 1} KB, DetailsController#show"
171
+ end
172
+
173
+ end
174
+
175
+ # it "should report the time span" do
176
+ # str = <<-STR
177
+ # Feb 01 01:58:29 ey04-s00297 rails[4413]: Processing Users#show (for 92.84.151.171 at 2009-02-01 01:58:29) [GET]
178
+ # Feb 01 01:58:30 ey04-s00297 rails[4413]: Memory usage: 0 | PID: 4413
179
+ # Feb 01 01:58:30 ey04-s00297 rails[4413]: Completed in 984ms (View: 840, DB: 4) | 200 OK
180
+ # Mar 13 01:58:29 ey04-s00297 rails[5513]: Processing MediaController#show (for 92.84.151.171 at 2009-02-01 01:58:29) [GET]
181
+ # Mar 13 01:58:30 ey04-s00297 rails[5513]: Memory usage: #{TEN_MEGS + 1} | PID: 4413
182
+ # Mar 13 03:58:30 ey04-s00297 rails[5513]: Completed in 984ms (View: 840, DB: 4) | 200 OK
183
+ # STR
184
+ #
185
+ # io = StringIO.new(str)
186
+ # output = PsuedoOutput.new
187
+ # Oink::MemoryUsageReporter.new(io, TEN_MEGS).each_line do |line|
188
+ # output << line
189
+ # end
190
+ # output.first.should == "Feb 01 01:58:29 - Mar 13 03:58:30"
191
+ # end
192
+
193
+ end
194
+
195
+ describe "verbose format" do
196
+ it "should print the full lines of actions exceeding the threshold" do
197
+ str = <<-STR
198
+ Feb 01 01:58:29 ey04-s00297 rails[4413]: Processing Users#show (for 92.84.151.171 at 2009-02-01 01:58:29) [GET]
199
+ Feb 01 01:58:29 ey04-s00297 rails[4413]: Parameters: {"id"=>"2332", "controller"=>"users"}
200
+ Feb 01 01:58:30 ey04-s00297 rails[4413]: Memory usage: 0 | PID: 4413
201
+ Feb 01 01:58:30 ey04-s00297 rails[4413]: Completed in 984ms (View: 840, DB: 4) | 200 OK
202
+ Feb 01 01:58:29 ey04-s00297 rails[4413]: Processing MediaController#show (for 92.84.151.171 at 2009-02-01 01:58:29) [GET]
203
+ Feb 01 01:58:29 ey04-s00297 rails[4413]: Parameters: {"id"=>"22900", "controller"=>"media"}
204
+ Feb 01 01:58:30 ey04-s00297 rails[4413]: Memory usage: #{TEN_MEGS + 1} | PID: 4413
205
+ Feb 01 01:58:30 ey04-s00297 rails[4413]: Completed in 984ms (View: 840, DB: 4) | 200 OK
206
+ STR
207
+ io = StringIO.new(str)
208
+ output = PsuedoOutput.new
209
+ Oink::MemoryUsageReporter.new(io, TEN_MEGS, :format => :verbose).print(output)
210
+ output[3..6].should == str.split("\n")[4..7].map { |o| o.strip }
211
+ end
212
+
213
+ it "should handle actions which do not complete properly" do
214
+ threshold = 10
215
+
216
+ str = <<-STR
217
+ Feb 01 01:58:29 ey04-s00297 rails[4413]: Processing Users#show (for 92.84.151.171 at 2009-02-01 01:58:29) [GET]
218
+ Feb 01 01:58:30 ey04-s00297 rails[4413]: Memory usage: 0 | PID: 4413
219
+ Feb 01 01:58:29 ey04-s00297 rails[4413]: Processing MediaController#show (for 92.84.151.171 at 2009-02-01 01:58:29) [GET]
220
+ Feb 01 01:58:29 ey04-s00297 rails[4413]: Processing MediaController#show (for 92.84.151.171 at 2009-02-01 01:58:29) [GET]
221
+ Feb 01 01:58:30 ey04-s00297 rails[4413]: Memory usage: #{TEN_MEGS + 1} | PID: 4413
222
+ Feb 01 01:58:30 ey04-s00297 rails[4413]: Completed in 984ms (View: 840, DB: 4) | 200 OK
223
+ Feb 01 01:58:29 ey04-s00297 rails[4413]: Processing ActorController#show (for 92.84.151.171 at 2009-02-01 01:58:29) [GET]
224
+ Feb 01 01:58:30 ey04-s00297 rails[4413]: Memory usage: #{(TEN_MEGS * 2) + 2} | PID: 4413
225
+ Feb 01 01:58:30 ey04-s00297 rails[4413]: Completed in 984ms (View: 840, DB: 4) | 200 OK
226
+ STR
227
+
228
+ io = StringIO.new(str)
229
+ output = PsuedoOutput.new
230
+ Oink::MemoryUsageReporter.new(io, TEN_MEGS, :format => :verbose).print(output)
231
+ output[3..5].should == str.split("\n")[6..8].map { |o| o.strip }
232
+ end
233
+ end
234
+
235
+ describe "multiple io streams" do
236
+ it "should accept multiple files" do
237
+
238
+ str1 = <<-STR
239
+ Feb 01 01:58:29 ey04-s00297 rails[4413]: Processing Users#show (for 92.84.151.171 at 2009-02-01 01:58:29) [GET]
240
+ Feb 01 01:58:30 ey04-s00297 rails[4413]: Memory usage: 0 | PID: 4413
241
+ Feb 01 01:58:30 ey04-s00297 rails[4413]: Completed in 984ms (View: 840, DB: 4) | 200 OK
242
+ Feb 01 01:58:29 ey04-s00297 rails[4413]: Processing MediaController#show (for 92.84.151.171 at 2009-02-01 01:58:29) [GET]
243
+ Feb 01 01:58:30 ey04-s00297 rails[4413]: Memory usage: #{TEN_MEGS + 1} | PID: 4413
244
+ Feb 01 01:58:30 ey04-s00297 rails[4413]: Completed in 984ms (View: 840, DB: 4) | 200 OK
245
+ STR
246
+
247
+ str2 = <<-STR
248
+ Feb 01 01:58:29 ey04-s00297 rails[4413]: Processing Users#show (for 92.84.151.171 at 2009-02-01 01:58:29) [GET]
249
+ Feb 01 01:58:30 ey04-s00297 rails[4413]: Memory usage: 0 | PID: 4413
250
+ Feb 01 01:58:30 ey04-s00297 rails[4413]: Completed in 984ms (View: 840, DB: 4) | 200 OK
251
+ Feb 01 01:58:29 ey04-s00297 rails[4413]: Processing MediaController#show (for 92.84.151.171 at 2009-02-01 01:58:29) [GET]
252
+ Feb 01 01:58:30 ey04-s00297 rails[4413]: Memory usage: #{TEN_MEGS + 1} | PID: 4413
253
+ Feb 01 01:58:30 ey04-s00297 rails[4413]: Completed in 984ms (View: 840, DB: 4) | 200 OK
254
+ STR
255
+
256
+ io1 = StringIO.new(str1)
257
+ io2 = StringIO.new(str2)
258
+ output = PsuedoOutput.new
259
+ Oink::MemoryUsageReporter.new([io1, io2], TEN_MEGS).print(output)
260
+ output.should include("2, MediaController#show")
261
+ end
262
+
263
+ end
264
+
265
+ end