wvanbergen-request-log-analyzer 0.2.2 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (67) hide show
  1. data/{README → README.textile} +29 -36
  2. data/Rakefile +3 -70
  3. data/TODO +43 -8
  4. data/bin/request-log-analyzer +32 -99
  5. data/lib/base/summarizer.rb +14 -0
  6. data/lib/bashcolorizer.rb +1 -1
  7. data/lib/command_line/arguments.rb +15 -2
  8. data/lib/command_line/flag.rb +12 -0
  9. data/lib/rails_analyzer/summarizer.rb +12 -4
  10. data/lib/rails_analyzer/virtual_mongrel.rb +91 -0
  11. data/lib/request_log_analyzer/aggregator/base.rb +34 -0
  12. data/lib/request_log_analyzer/aggregator/database.rb +86 -0
  13. data/lib/request_log_analyzer/aggregator/echo.rb +10 -0
  14. data/lib/request_log_analyzer/aggregator/summarizer.rb +53 -0
  15. data/lib/request_log_analyzer/controller.rb +90 -0
  16. data/lib/request_log_analyzer/file_format/merb.rb +30 -0
  17. data/lib/request_log_analyzer/file_format/rails.rb +84 -0
  18. data/lib/request_log_analyzer/file_format.rb +91 -0
  19. data/lib/request_log_analyzer/log_parser.rb +122 -0
  20. data/lib/request_log_analyzer/request.rb +72 -0
  21. data/lib/request_log_analyzer.rb +5 -0
  22. data/output/blockers.rb +2 -4
  23. data/output/errors.rb +1 -2
  24. data/output/hourly_spread.rb +3 -3
  25. data/output/mean_db_time.rb +1 -2
  26. data/output/mean_rendering_time.rb +2 -3
  27. data/output/mean_time.rb +2 -3
  28. data/output/most_requested.rb +1 -2
  29. data/output/timespan.rb +10 -8
  30. data/output/total_db_time.rb +2 -3
  31. data/output/total_time.rb +2 -3
  32. data/output/usage.rb +3 -2
  33. data/spec/controller_spec.rb +33 -0
  34. data/spec/database_inserter_spec.rb +81 -0
  35. data/{test/log_fragments/merb_1.log → spec/fixtures/merb.log} +0 -0
  36. data/spec/fixtures/multiple_files_1.log +5 -0
  37. data/spec/fixtures/multiple_files_2.log +2 -0
  38. data/{test/log_fragments/fragment_1.log → spec/fixtures/rails_1x.log} +5 -5
  39. data/{test/log_fragments/fragment_3.log → spec/fixtures/rails_22.log} +2 -2
  40. data/spec/fixtures/rails_22_cached.log +10 -0
  41. data/spec/fixtures/rails_unordered.log +24 -0
  42. data/{test/log_fragments/fragment_2.log → spec/fixtures/syslog_1x.log} +0 -0
  43. data/spec/fixtures/test_file_format.log +11 -0
  44. data/spec/fixtures/test_language_combined.log +14 -0
  45. data/spec/fixtures/test_order.log +16 -0
  46. data/spec/line_definition_spec.rb +34 -0
  47. data/spec/log_parser_spec.rb +92 -0
  48. data/spec/merb_format_spec.rb +58 -0
  49. data/spec/rails_format_spec.rb +95 -0
  50. data/spec/request_spec.rb +76 -0
  51. data/spec/spec_helper.rb +49 -0
  52. data/spec/summarizer_spec.rb +109 -0
  53. data/tasks/github-gem.rake +177 -0
  54. data/tasks/request_log_analyzer.rake +10 -0
  55. data/tasks/rspec.rake +6 -0
  56. data/test/base_summarizer_test.rb +30 -0
  57. metadata +46 -22
  58. data/bin/request-log-database +0 -81
  59. data/lib/base/log_parser.rb +0 -78
  60. data/lib/base/record_inserter.rb +0 -139
  61. data/lib/merb_analyzer/log_parser.rb +0 -26
  62. data/lib/rails_analyzer/log_parser.rb +0 -35
  63. data/lib/rails_analyzer/record_inserter.rb +0 -39
  64. data/test/merb_log_parser_test.rb +0 -39
  65. data/test/rails_log_parser_test.rb +0 -95
  66. data/test/record_inserter_test.rb +0 -45
  67. data/test/tasks.rake +0 -8
@@ -1,81 +0,0 @@
1
- #!/usr/bin/ruby
2
-
3
- require File.dirname(__FILE__) + '/../lib/command_line/arguments'
4
- require File.dirname(__FILE__) + '/../lib/base/log_parser'
5
- require File.dirname(__FILE__) + '/../lib/base/record_inserter'
6
- require File.dirname(__FILE__) + '/../lib/rails_analyzer/log_parser'
7
- require File.dirname(__FILE__) + '/../lib/rails_analyzer/record_inserter'
8
- require File.dirname(__FILE__) + '/../lib/bashcolorizer'
9
- require File.dirname(__FILE__) + '/../lib/ruby-progressbar/progressbar.rb'
10
-
11
-
12
- puts "Rails log parser, by Willem van Bergen and Bart ten Brinke\n\n"
13
-
14
- begin
15
-
16
- $arguments = CommandLine::Arguments.parse do |command_line|
17
- command_line.switch(:guess_database_time, :g)
18
- command_line.switch(:reset_database, :r)
19
- command_line.flag(:database, :alias => :d, :required => false)
20
- command_line.required_files = 1
21
- end
22
-
23
- rescue CommandLine::Error => e
24
- puts "ARGUMENT ERROR: " + e.message
25
- puts
26
- puts "Usage: ruby parsetodb.rb [LOGFILES*] <OPTIONS>"
27
- puts
28
- puts "Options:"
29
- puts " --database, -t: The database file to use"
30
- puts " --reset-database, -r: Resets the database before inserting new records"
31
- puts " --guess-database-time, -g: Guesses the database duration of requests"
32
- puts
33
- puts "Examples:"
34
- puts " ./parsetodb.rb development.log"
35
- puts " ./parsetodb.rb mongrel.0.log mongrel.1.log mongrel.2.log -g -d mongrel.db"
36
- puts
37
-
38
- exit(0)
39
- end
40
-
41
- log_files = $arguments.files
42
- db_file = $arguments[:database] || log_files.first + '.db'
43
-
44
- if $arguments[:reset_database] && File.exist?(db_file)
45
- File.delete(db_file)
46
- puts "Database file cleared."
47
- end
48
-
49
- records_inserted = 0
50
- inserter = RailsAnalyzer::RecordInserter.insert_batch_into(db_file) do |db|
51
- log_files.each do |log_file|
52
-
53
- puts "Processing all log lines from #{log_file}..."
54
- parser = RailsAnalyzer::LogParser.new(log_file)
55
-
56
- pbar = ProgressBar.new(green(log_file), File.size(log_file))
57
- parser.progress { |pos, total| (pos == :finished) ? pbar.finish : pbar.set(pos) }
58
-
59
- parser.each do |request|
60
- db.insert(request)
61
- records_inserted += 1
62
- end
63
- end
64
-
65
- if $arguments[:guess_database_time]
66
- puts "Calculating database times..."
67
- db.calculate_db_durations!
68
- end
69
- end
70
-
71
- started = inserter.count(:started)
72
- completed = inserter.count(:completed)
73
- failed = inserter.count(:failed)
74
-
75
- puts
76
- puts "Inserted #{records_inserted} records from #{log_files.length} files."
77
- puts "Parse warnings: #{inserter.warning_count}. Check the parse_warnings table in the database for details."
78
- puts
79
- puts "Requests started: #{started}"
80
- puts "Requests completed: #{completed}"
81
- puts "Requests failed: #{failed}"
@@ -1,78 +0,0 @@
1
- module Base
2
- # Parse a log file
3
- class LogParser
4
-
5
- LOG_LINES = {}
6
-
7
- # LogParser initializer
8
- # <tt>file</tt> The fileobject this LogParser wil operate on.
9
- def initialize(file, options = {})
10
- @file_name = file
11
- @options = options
12
- @file_size = File.size(@file_name)
13
-
14
- self.initialize_hook(options) if self.respond_to?(:initialize_hook)
15
- end
16
-
17
- def progress(&block)
18
- @progress_handler = block
19
- end
20
-
21
- # Output a warning
22
- # <tt>message</tt> The warning message (object)
23
- def warn(message)
24
- puts " -> " + message.to_s
25
- end
26
-
27
- def convert_value(value, type)
28
- return case type
29
- when :string; value.to_s
30
- when :int; value.to_i
31
- when :sec; value.to_f
32
- when :msec; value.to_f / 1000
33
- when :timestamp; value.to_s # TODO: fix me?
34
- else
35
- warn("Unkwn type encountered: #{type}")
36
- value
37
- end
38
- end
39
-
40
- # Finds a log line and then parses the information in the line.
41
- # Yields a hash containing the information found.
42
- # <tt>*line_types</tt> The log line types to look for (defaults to LOG_LINES.keys).
43
- # Yeilds a Hash when it encounters a chunk of information.
44
- def each(*line_types, &block)
45
- log_lines_hash = self.class::LOG_LINES
46
-
47
-
48
- # parse everything by default
49
- line_types = log_lines_hash.keys if line_types.empty?
50
-
51
- File.open(@file_name) do |file|
52
-
53
- file.each_line do |line|
54
-
55
- @progress_handler.call(file.pos, @file_size) if @progress_handler
56
-
57
- line_types.each do |line_type|
58
- if log_lines_hash[line_type][:teaser] =~ line
59
- if log_lines_hash[line_type][:regexp] =~ line
60
-
61
- captures = $~.captures
62
- request = { :type => line_type, :line => file.lineno }
63
- log_lines_hash[line_type][:params].each_with_index do |paramhash, index|
64
- paramhash.each { |key, type| request[key] = convert_value(captures[index], type) } unless captures[index].nil?
65
- end
66
-
67
- yield(request) if block_given?
68
- else
69
- warn("Unparsable #{line_type} line: " + line[0..79]) unless line_type == :failed
70
- end
71
- end
72
- end
73
- end
74
- @progress_handler.call(:finished, @file_size) if @progress_handler
75
- end
76
- end
77
- end
78
- end
@@ -1,139 +0,0 @@
1
- require 'rubygems'
2
- require 'sqlite3'
3
-
4
- module Base
5
-
6
- # Set of functions that can be used to easily log requests into a SQLite3 Database.
7
- class RecordInserter
8
-
9
- attr_reader :database
10
- attr_reader :current_request
11
- attr_reader :warning_count
12
-
13
- # Initializer
14
- # <tt>db_file</tt> The file which will be used for the SQLite3 Database storage.
15
- def initialize(db_file, options = {})
16
- @database = SQLite3::Database.new(db_file)
17
- @insert_statements = nil
18
- @warning_count = 0
19
- create_tables_if_needed!
20
-
21
- self.initialize_hook(options) if self.respond_to?(:initialize_hook)
22
- end
23
-
24
- # Calculate the database durations of the requests currenty in the database.
25
- # Used if a logfile does contain any database durations.
26
- def calculate_db_durations!
27
- @database.execute('UPDATE "completed_queries" SET "database" = "duration" - "rendering" WHERE "database" IS NULL OR "database" = 0.0')
28
- end
29
-
30
- # Insert a batch of loglines into the database.
31
- # Function prepares insert statements, yeilds and then closes and commits.
32
- def insert_batch(&block)
33
- @database.transaction
34
- prepare_statements!
35
- block.call(self)
36
- close_prepared_statements!
37
- @database.commit
38
- rescue Exception => e
39
- puts e.message
40
- @database.rollback
41
- end
42
-
43
- def insert_warning(line, warning)
44
- @database.execute("INSERT INTO parse_warnings (line, warning) VALUES (:line, :warning)", :line => line, :warning => warning)
45
- @warning_count += 1
46
- end
47
-
48
- # Insert a request into the database.
49
- # def insert(request, close_statements = false)
50
- # raise 'No insert defined for this log file type'
51
- # end
52
-
53
- # Insert a batch of files into the database.
54
- # <tt>db_file</tt> The filename of the database file to use.
55
- # Returns the created database.
56
- def self.insert_batch_into(db_file, options = {}, &block)
57
- db = self.new(db_file)
58
- db.insert_batch(&block)
59
- return db
60
- end
61
-
62
- def count(type)
63
- @database.get_first_value("SELECT COUNT(*) FROM \"#{type}_requests\"").to_i
64
- end
65
-
66
- protected
67
-
68
- # Prepare insert statements.
69
- def prepare_statements!
70
- @insert_statements = {
71
- :started => @database.prepare("
72
- INSERT INTO started_requests ( line, timestamp, ip, method, controller, action)
73
- VALUES (:line, :timestamp, :ip, :method, :controller, :action)"),
74
-
75
- :failed => @database.prepare("
76
- INSERT INTO failed_requests ( line, exception_string, stack_trace, error)
77
- VALUES (:line, :exception_string, :stack_trace, :error)"),
78
-
79
- :completed => @database.prepare("
80
- INSERT INTO completed_requests ( line, url, status, duration, rendering_time, database_time)
81
- VALUES (:line, :url, :status, :duration, :rendering, :db)")
82
- }
83
- end
84
-
85
- # Close all prepared statments
86
- def close_prepared_statements!
87
- @insert_statements.each { |key, stmt| stmt.close }
88
- end
89
-
90
- # Create the needed database tables if they don't exist.
91
- def create_tables_if_needed!
92
-
93
- @database.execute("
94
- CREATE TABLE IF NOT EXISTS started_requests (
95
- id INTEGER PRIMARY KEY AUTOINCREMENT,
96
- line INTEGER NOT NULL,
97
- timestamp DATETIME NOT NULL,
98
- controller VARCHAR(255) NOT NULL,
99
- action VARCHAR(255) NOT NULL,
100
- method VARCHAR(6) NOT NULL,
101
- ip VARCHAR(6) NOT NULL
102
- )
103
- ");
104
-
105
- @database.execute("
106
- CREATE TABLE IF NOT EXISTS failed_requests (
107
- id INTEGER PRIMARY KEY AUTOINCREMENT,
108
- line INTEGER NOT NULL,
109
- started_request_id INTEGER,
110
- error VARCHAR(255),
111
- exception_string VARCHAR(255),
112
- stack_trace TEXT
113
- )
114
- ");
115
-
116
- @database.execute("
117
- CREATE TABLE IF NOT EXISTS completed_requests (
118
- id INTEGER PRIMARY KEY AUTOINCREMENT,
119
- line INTEGER NOT NULL,
120
- started_request_id INTEGER,
121
- url VARCHAR(255) NOT NULL,
122
- hashed_url VARCHAR(255),
123
- status INTEGER NOT NULL,
124
- duration FLOAT,
125
- rendering_time FLOAT,
126
- database_time FLOAT
127
- )
128
- ");
129
-
130
- @database.execute("CREATE TABLE IF NOT EXISTS parse_warnings (
131
- id INTEGER PRIMARY KEY AUTOINCREMENT,
132
- line INTEGER NOT NULL,
133
- warning VARCHAR(255) NOT NULL
134
- )
135
- ");
136
- end
137
-
138
- end
139
- end
@@ -1,26 +0,0 @@
1
- module MerbAnalyzer
2
-
3
- class LogParser < Base::LogParser
4
- LOG_LINES = {
5
- # ~ Started request handling: Fri Aug 29 11:10:23 +0200 2008
6
- :started => {
7
- :teaser => /Started/,
8
- :regexp => /Started request handling\:\ (.+)/,
9
- :params => [{:timestamp => :timestamp}]
10
- },
11
- # ~ Params: {"action"=>"create", "controller"=>"session"}
12
- # ~ Params: {"_method"=>"delete", "authenticity_token"=>"[FILTERED]", "action"=>"d}
13
- :params => {
14
- :teaser => /Params/,
15
- :regexp => /Params\:\ \{(.+)\}/,
16
- :params => [{:raw_hash => :string}]
17
- },
18
- # ~ {:dispatch_time=>0.006117, :after_filters_time=>6.1e-05, :before_filters_time=>0.000712, :action_time=>0.005833}
19
- :completed => {
20
- :teaser => /\{:dispatch_time/,
21
- :regexp => /\{\:dispatch_time=>(.+), (?:\:after_filters_time=>(.+), )?(?:\:before_filters_time=>(.+), )?\:action_time=>(.+)\}/,
22
- :params => [ {:dispatch_time => :sec}, {:after_filters_time => :sec}, {:before_filters_time => :sec}, {:action_time => :sec} ]
23
- }
24
- }
25
- end
26
- end
@@ -1,35 +0,0 @@
1
- module RailsAnalyzer
2
-
3
- class LogParser < Base::LogParser
4
-
5
- # Completed in 0.21665 (4 reqs/sec) | Rendering: 0.00926 (4%) | DB: 0.00000 (0%) | 200 OK [http://demo.nu/employees]
6
- RAILS_21_COMPLETED = /Completed in (\d+\.\d{5}) \(\d+ reqs\/sec\) (?:\| Rendering: (\d+\.\d{5}) \(\d+\%\) )?(?:\| DB: (\d+\.\d{5}) \(\d+\%\) )?\| (\d\d\d).+\[(http.+)\]/
7
-
8
- # Completed in 614ms (View: 120, DB: 31) | 200 OK [http://floorplanner.local/demo]
9
- RAILS_22_COMPLETED = /Completed in (\d+)ms \((?:View: (\d+), )?DB: (\d+)\) \| (\d\d\d).+\[(http.+)\]/
10
-
11
-
12
- LOG_LINES = {
13
- # Processing EmployeeController#index (for 123.123.123.123 at 2008-07-13 06:00:00) [GET]
14
- :started => {
15
- :teaser => /Processing/,
16
- :regexp => /Processing ((?:\w+::)?\w+)#(\w+)(?: to (\w+))? \(for (\d+\.\d+\.\d+\.\d+) at (\d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d)\) \[([A-Z]+)\]/,
17
- :params => [{:controller => :string}, {:action => :string}, {:format => :string}, {:ip => :string}, {:timestamp => :timestamp}, {:method => :string}]
18
- },
19
- # RuntimeError (Cannot destroy employee): /app/models/employee.rb:198:in `before_destroy'
20
- :failed => {
21
- :teaser => /Error/,
22
- :regexp => /(\w+)(?:Error|Invalid) \((.*)\)\:(.*)/,
23
- :params => [{:error => :string}, {:exception_string => :string}, {:stack_trace => :string}]
24
- },
25
-
26
- :completed => {
27
- :teaser => /Completed in /,
28
- :regexp => Regexp.new("(?:#{RAILS_21_COMPLETED}|#{RAILS_22_COMPLETED})"),
29
- :params => [{:duration => :sec}, {:rendering => :sec}, {:db => :sec}, {:status => :int}, {:url => :string}, # 2.1 variant
30
- {:duration => :msec}, {:rendering => :msec}, {:db => :msec}, {:status => :int}, {:url => :string}] # 2.2 variant
31
-
32
- }
33
- }
34
- end
35
- end
@@ -1,39 +0,0 @@
1
- require 'rubygems'
2
- require 'sqlite3'
3
-
4
- module RailsAnalyzer
5
-
6
- # Set of functions that can be used to easily log requests into a SQLite3 Database.
7
- class RecordInserter < Base::RecordInserter
8
-
9
- # Insert a request into the database.
10
- # <tt>request</tt> The request to insert.
11
- # <tt>close_statements</tt> Close prepared statements (default false)
12
- def insert(request, close_statements = false)
13
- unless @insert_statements
14
- prepare_statements!
15
- close_statements = true
16
- end
17
-
18
- if request[:type] && @insert_statements.has_key?(request[:type])
19
- if request[:type] == :started
20
- insert_warning(request[:line], "Unclosed request encountered on line #{request[:line]} (request started on line #{@current_request})") unless @current_request.nil?
21
- @current_request = request[:line]
22
- elsif [:failed, :completed].include?(request[:type])
23
- @current_request = nil
24
- end
25
-
26
- begin
27
- @insert_statements[request.delete(:type)].execute(request)
28
- rescue SQLite3::Exception => e
29
- insert_warning(request[:line], "Could not save log line to database: " + e.message.to_s)
30
- end
31
- else
32
- insert_warning(request[:line], "Ignored unknown statement type")
33
- end
34
-
35
- close_prepared_statements! if close_statements
36
- end
37
-
38
- end
39
- end
@@ -1,39 +0,0 @@
1
- require 'test/unit'
2
-
3
- require "#{File.dirname(__FILE__)}/../lib/base/log_parser"
4
- require "#{File.dirname(__FILE__)}/../lib/merb_analyzer/log_parser"
5
-
6
- class MerbLogParserTest < Test::Unit::TestCase
7
-
8
- def fragment_file(number)
9
- "#{File.dirname(__FILE__)}/log_fragments/merb_#{number}.log"
10
- end
11
-
12
- def test_parse_started_merb_fragment
13
- requests = []
14
- parser = MerbAnalyzer::LogParser.new(fragment_file(1)).each(:started) do |request|
15
- requests << request
16
- end
17
- assert_equal requests[0][:timestamp], "Fri Aug 29 11:10:23 +0200 2008"
18
- end
19
-
20
- def test_parse_completed_merb_fragment
21
- requests = []
22
- parser = MerbAnalyzer::LogParser.new(fragment_file(1)).each(:completed) do |request|
23
- requests << request
24
- end
25
-
26
- assert_equal requests[0][:action_time], 0.241652
27
- end
28
-
29
- def test_parse_params_merb_fragment
30
- requests = []
31
- parser = MerbAnalyzer::LogParser.new(fragment_file(1)).each(:params) do |request|
32
- requests << request
33
- end
34
-
35
- assert_match '"controller"=>"session"', requests[0][:raw_hash]
36
- assert_match '"action"=>"destroy"', requests[0][:raw_hash]
37
- end
38
-
39
- end
@@ -1,95 +0,0 @@
1
- require 'test/unit'
2
-
3
- require "#{File.dirname(__FILE__)}/../lib/base/log_parser"
4
- require "#{File.dirname(__FILE__)}/../lib/rails_analyzer/log_parser"
5
-
6
- class RailsLogParserTest < Test::Unit::TestCase
7
-
8
- def fragment_file(number)
9
- "#{File.dirname(__FILE__)}/log_fragments/fragment_#{number}.log"
10
- end
11
-
12
-
13
- def test_rails_22_log_format
14
- count = 0
15
- parser = RailsAnalyzer::LogParser.new(fragment_file(3)).each(:completed) do |request|
16
- count += 1
17
- assert_equal 0.614, request[:duration] # should be 0.614
18
- end
19
- assert_equal 1, count
20
- end
21
-
22
- def test_progress_messages
23
- log_file = fragment_file(1)
24
-
25
- finished_encountered = false
26
- file_size = File.size(log_file)
27
-
28
- previous_pos = -1
29
- parser = RailsAnalyzer::LogParser.new(log_file)
30
- parser.progress do |pos, total|
31
- assert_equal file_size, total
32
- if pos == :finished
33
- finished_encountered = true
34
- else
35
- assert pos <= total
36
- assert pos > previous_pos
37
- previous_pos = pos
38
- end
39
- end
40
-
41
- # now parse the file
42
- parser.each(:started) { }
43
-
44
- assert finished_encountered, "A finished event should have been fired"
45
- end
46
-
47
- def test_parse_mongrel_log_fragment
48
- count = 0
49
- parser = RailsAnalyzer::LogParser.new(fragment_file(1)).each(:started) { count += 1 }
50
- assert_equal 4, count
51
-
52
- count = 0
53
- parser = RailsAnalyzer::LogParser.new(fragment_file(1)).each(:completed) { count += 1 }
54
- assert_equal 4, count
55
-
56
- count = 0
57
- parser = RailsAnalyzer::LogParser.new(fragment_file(1)).each(:started, :completed) { count += 1 }
58
- assert_equal 8, count
59
- end
60
-
61
- def test_parse_syslog_fragment
62
- count = 0
63
- parser = RailsAnalyzer::LogParser.new(fragment_file(2)).each(:started) { count += 1 }
64
- assert_equal 1, count
65
-
66
- count = 0
67
- parser = RailsAnalyzer::LogParser.new(fragment_file(2)).each(:completed) { count += 1 }
68
- assert_equal 1, count
69
-
70
- count = 0
71
- parser = RailsAnalyzer::LogParser.new(fragment_file(2)).each(:started, :completed) { count += 1 }
72
- assert_equal 2, count
73
- end
74
-
75
- def test_parse_syslog_fragment_content
76
- # this test only works because there is only one requests in the fragment
77
- parser = RailsAnalyzer::LogParser.new(fragment_file(2)).each(:started) do |request|
78
- assert_equal "EmployeeController", request[:controller]
79
- assert_equal "index", request[:action]
80
- assert_equal "GET", request[:method]
81
- assert_equal '10.1.1.33', request[:ip]
82
- assert_equal '2008-07-13 06:25:58', request[:timestamp]
83
- end
84
-
85
- parser = RailsAnalyzer::LogParser.new(fragment_file(2)).each(:completed) do |request|
86
- assert_equal "http://example.com/employee.xml", request[:url]
87
- assert_equal 200, request[:status]
88
- assert_equal 0.21665, request[:duration]
89
- assert_equal 0.00926, request[:rendering]
90
- assert_equal 0.0, request[:db]
91
- end
92
-
93
- end
94
-
95
- end
@@ -1,45 +0,0 @@
1
- require 'test/unit'
2
-
3
- require "#{File.dirname(__FILE__)}/../lib/base/log_parser"
4
- require "#{File.dirname(__FILE__)}/../lib/base/record_inserter"
5
-
6
- require "#{File.dirname(__FILE__)}/../lib/rails_analyzer/log_parser"
7
- require "#{File.dirname(__FILE__)}/../lib/rails_analyzer/record_inserter"
8
-
9
- class RecordInserterTest < Test::Unit::TestCase
10
-
11
- def fragment_file(number)
12
- "#{File.dirname(__FILE__)}/log_fragments/fragment_#{number}.log"
13
- end
14
-
15
- def setup
16
- File.delete('_tmp.db') if File.exist?('_tmp.db')
17
- end
18
-
19
- def teardown
20
- File.delete('_tmp.db') if File.exist?('_tmp.db')
21
- end
22
-
23
- def test_insert_log_fragment
24
-
25
- db = RailsAnalyzer::RecordInserter.insert_batch_into('_tmp.db') do |batch|
26
- RailsAnalyzer::LogParser.new(fragment_file(1)).each { |request| batch.insert(request) }
27
- end
28
-
29
- assert_equal 4, db.database.get_first_value("SELECT COUNT(*) FROM started_requests").to_i
30
- assert_equal 4, db.database.get_first_value("SELECT COUNT(*) FROM completed_requests").to_i
31
- end
32
-
33
- def test_insert_multiple_fragments
34
- RailsAnalyzer::RecordInserter.insert_batch_into('_tmp.db') do |batch|
35
- RailsAnalyzer::LogParser.new(fragment_file(1)).each { |request| batch.insert(request) }
36
- end
37
-
38
- db = RailsAnalyzer::RecordInserter.insert_batch_into('_tmp.db') do |batch|
39
- RailsAnalyzer::LogParser.new(fragment_file(2)).each { |request| batch.insert(request) }
40
- end
41
- assert_equal 5, db.database.get_first_value("SELECT COUNT(*) FROM started_requests").to_i
42
- assert_equal 5, db.database.get_first_value("SELECT COUNT(*) FROM completed_requests").to_i
43
- end
44
-
45
- end
data/test/tasks.rake DELETED
@@ -1,8 +0,0 @@
1
- require 'rake/testtask'
2
-
3
- desc 'Unit test request-log-analyzer.'
4
- Rake::TestTask.new(:test) do |t|
5
- t.pattern = 'test/**/*_test.rb'
6
- t.verbose = true
7
- t.libs << 'test'
8
- end