query_diet 0.2.3 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +2 -0
- data/VERSION +1 -1
- data/lib/query_diet/logger.rb +14 -13
- data/query_diet.gemspec +3 -3
- data/spec/logger_spec.rb +28 -7
- data/spec/spec_helper.rb +1 -0
- metadata +25 -25
data/README.rdoc
CHANGED
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.3.0
|
data/lib/query_diet/logger.rb
CHANGED
@@ -1,26 +1,29 @@
|
|
1
1
|
module QueryDiet
|
2
2
|
class Logger
|
3
3
|
class << self
|
4
|
-
|
5
|
-
attr_accessor :count, :time, :bad_count, :bad_time
|
4
|
+
attr_accessor :queries, :bad_count, :bad_time
|
6
5
|
|
7
6
|
def reset
|
8
|
-
self.
|
9
|
-
self.time = 0
|
7
|
+
self.queries = []
|
10
8
|
end
|
11
9
|
|
12
|
-
def log(query
|
10
|
+
def log(query)
|
13
11
|
result = nil
|
14
|
-
|
15
|
-
result =
|
16
|
-
end
|
17
|
-
if log_query?(query)
|
18
|
-
self.time += (seconds * 1000).to_i
|
19
|
-
self.count += 1
|
12
|
+
time = Benchmark.realtime do
|
13
|
+
result = yield
|
20
14
|
end
|
15
|
+
queries << [query, time] if log_query?(query)
|
21
16
|
result
|
22
17
|
end
|
23
18
|
|
19
|
+
def time
|
20
|
+
(queries.sum(&:last) * 1000).to_i
|
21
|
+
end
|
22
|
+
|
23
|
+
def count
|
24
|
+
queries.size
|
25
|
+
end
|
26
|
+
|
24
27
|
def bad?
|
25
28
|
count >= bad_count or time >= bad_time
|
26
29
|
end
|
@@ -30,12 +33,10 @@ module QueryDiet
|
|
30
33
|
def log_query?(query)
|
31
34
|
query =~ /^(select|create|update|delete|insert)\b/i
|
32
35
|
end
|
33
|
-
|
34
36
|
end
|
35
37
|
|
36
38
|
reset
|
37
39
|
self.bad_count = 8
|
38
40
|
self.bad_time = 5000
|
39
|
-
|
40
41
|
end
|
41
42
|
end
|
data/query_diet.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = "query_diet"
|
8
|
-
s.version = "0.
|
8
|
+
s.version = "0.3.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Henning Koch"]
|
12
|
-
s.date = "2012-
|
12
|
+
s.date = "2012-09-18"
|
13
13
|
s.description = "Rails database query counter that stays out of your way"
|
14
14
|
s.email = "github@makandra.de"
|
15
15
|
s.extra_rdoc_files = [
|
@@ -55,7 +55,7 @@ Gem::Specification.new do |s|
|
|
55
55
|
]
|
56
56
|
s.homepage = "http://github.com/makandra/query_diet"
|
57
57
|
s.require_paths = ["lib"]
|
58
|
-
s.rubygems_version = "1.3.9.
|
58
|
+
s.rubygems_version = "1.3.9.5"
|
59
59
|
s.summary = "Rails database query counter that stays out of your way"
|
60
60
|
|
61
61
|
if s.respond_to? :specification_version then
|
data/spec/logger_spec.rb
CHANGED
@@ -1,29 +1,50 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe QueryDiet::Logger do
|
4
|
-
|
5
|
-
before :each do
|
4
|
+
before do
|
6
5
|
QueryDiet::Logger.reset
|
7
6
|
end
|
8
7
|
|
9
|
-
describe
|
8
|
+
describe "#reset" do
|
9
|
+
it "should reset count/time/queries" do
|
10
|
+
QueryDiet::Logger.count.should == 0
|
11
|
+
QueryDiet::Logger.time.should == 0
|
12
|
+
QueryDiet::Logger.queries.should == []
|
13
|
+
|
14
|
+
Benchmark.should_receive(:realtime).and_return(5)
|
15
|
+
Movie.create
|
16
|
+
|
17
|
+
QueryDiet::Logger.count.should > 0
|
18
|
+
QueryDiet::Logger.time.should > 0
|
19
|
+
QueryDiet::Logger.queries.size.should > 0
|
20
|
+
end
|
21
|
+
end
|
10
22
|
|
23
|
+
describe "#count" do
|
11
24
|
it "should return the number of queries since the last reset" do
|
12
25
|
Movie.create
|
13
26
|
Movie.create
|
14
27
|
QueryDiet::Logger.count.should == 2
|
15
28
|
end
|
16
|
-
|
17
29
|
end
|
18
30
|
|
19
|
-
describe
|
20
|
-
|
31
|
+
describe "#time" do
|
21
32
|
it "should return the number of miliseconds spent running database queries since the last reset" do
|
22
33
|
Benchmark.should_receive(:realtime).and_return(5)
|
23
34
|
Movie.create
|
24
35
|
QueryDiet::Logger.time.should == 5000
|
25
36
|
end
|
26
|
-
|
27
37
|
end
|
28
38
|
|
39
|
+
describe "#queries" do
|
40
|
+
it "should return the queries since last reset" do
|
41
|
+
Benchmark.should_receive(:realtime).and_return(5.1234)
|
42
|
+
Movie.create
|
43
|
+
QueryDiet::Logger.queries.size.should == 1
|
44
|
+
query = QueryDiet::Logger.queries.first
|
45
|
+
query.size.should == 2
|
46
|
+
query[0].should include("INSERT INTO \"movies\"")
|
47
|
+
query[1].should == 5.1234
|
48
|
+
end
|
49
|
+
end
|
29
50
|
end
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: query_diet
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 19
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
- 2
|
9
8
|
- 3
|
10
|
-
|
9
|
+
- 0
|
10
|
+
version: 0.3.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Henning Koch
|
@@ -15,13 +15,14 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2012-
|
18
|
+
date: 2012-09-18 00:00:00 +02:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
22
|
-
type: :runtime
|
23
22
|
prerelease: false
|
24
|
-
|
23
|
+
name: rails
|
24
|
+
type: :runtime
|
25
|
+
version_requirements: &id001 !ruby/object:Gem::Requirement
|
25
26
|
none: false
|
26
27
|
requirements:
|
27
28
|
- - ~>
|
@@ -31,12 +32,12 @@ dependencies:
|
|
31
32
|
- 2
|
32
33
|
- 3
|
33
34
|
version: "2.3"
|
34
|
-
|
35
|
-
version_requirements: *id001
|
35
|
+
requirement: *id001
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
|
-
type: :runtime
|
38
37
|
prerelease: false
|
39
|
-
|
38
|
+
name: rspec
|
39
|
+
type: :runtime
|
40
|
+
version_requirements: &id002 !ruby/object:Gem::Requirement
|
40
41
|
none: false
|
41
42
|
requirements:
|
42
43
|
- - ~>
|
@@ -46,12 +47,12 @@ dependencies:
|
|
46
47
|
- 1
|
47
48
|
- 3
|
48
49
|
version: "1.3"
|
49
|
-
|
50
|
-
version_requirements: *id002
|
50
|
+
requirement: *id002
|
51
51
|
- !ruby/object:Gem::Dependency
|
52
|
-
type: :runtime
|
53
52
|
prerelease: false
|
54
|
-
|
53
|
+
name: rspec-rails
|
54
|
+
type: :runtime
|
55
|
+
version_requirements: &id003 !ruby/object:Gem::Requirement
|
55
56
|
none: false
|
56
57
|
requirements:
|
57
58
|
- - ~>
|
@@ -61,12 +62,12 @@ dependencies:
|
|
61
62
|
- 1
|
62
63
|
- 3
|
63
64
|
version: "1.3"
|
64
|
-
|
65
|
-
version_requirements: *id003
|
65
|
+
requirement: *id003
|
66
66
|
- !ruby/object:Gem::Dependency
|
67
|
-
type: :runtime
|
68
67
|
prerelease: false
|
69
|
-
|
68
|
+
name: sqlite3
|
69
|
+
type: :runtime
|
70
|
+
version_requirements: &id004 !ruby/object:Gem::Requirement
|
70
71
|
none: false
|
71
72
|
requirements:
|
72
73
|
- - ">="
|
@@ -75,12 +76,12 @@ dependencies:
|
|
75
76
|
segments:
|
76
77
|
- 0
|
77
78
|
version: "0"
|
78
|
-
|
79
|
-
version_requirements: *id004
|
79
|
+
requirement: *id004
|
80
80
|
- !ruby/object:Gem::Dependency
|
81
|
-
type: :runtime
|
82
81
|
prerelease: false
|
83
|
-
|
82
|
+
name: jeweler
|
83
|
+
type: :runtime
|
84
|
+
version_requirements: &id005 !ruby/object:Gem::Requirement
|
84
85
|
none: false
|
85
86
|
requirements:
|
86
87
|
- - ">="
|
@@ -89,8 +90,7 @@ dependencies:
|
|
89
90
|
segments:
|
90
91
|
- 0
|
91
92
|
version: "0"
|
92
|
-
|
93
|
-
version_requirements: *id005
|
93
|
+
requirement: *id005
|
94
94
|
description: Rails database query counter that stays out of your way
|
95
95
|
email: github@makandra.de
|
96
96
|
executables: []
|
@@ -166,7 +166,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
166
166
|
requirements: []
|
167
167
|
|
168
168
|
rubyforge_project:
|
169
|
-
rubygems_version: 1.3.9.
|
169
|
+
rubygems_version: 1.3.9.5
|
170
170
|
signing_key:
|
171
171
|
specification_version: 3
|
172
172
|
summary: Rails database query counter that stays out of your way
|