a_r_q_logger 0.0.8 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/lib/a_r_q_logger.rb +55 -20
- data/lib/a_r_q_logger/initializer.rb +2 -1
- data/lib/a_r_q_logger/version.rb +1 -1
- data/spec/test_model_spec.rb +39 -2
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e77f5f0ec1b623ab24e5e2b493181138511469f6
|
4
|
+
data.tar.gz: 8592b04bd9f8d2cf749947ece215f3702546e6de
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 567bc7769be381d3090b53047bc8b30c75710cc45980a00cc61f4eeeab2c674da1ea6ab7b5f790712b3b88e303737d54dc58bd21a999ba207e7a4609aff5c407
|
7
|
+
data.tar.gz: d46ef32a7c741784ea67f5d2ef3605c065df1b6befbb5a436d5b741cc9c8496dbddb647960548ecddb6d745467e74dcbd2692e729a2bf4e837c49ada14febd79
|
data/Gemfile.lock
CHANGED
data/lib/a_r_q_logger.rb
CHANGED
@@ -1,20 +1,26 @@
|
|
1
1
|
require 'a_r_q_logger/initializer'
|
2
|
+
require 'monitor'
|
3
|
+
|
4
|
+
# Thread.current.object_id
|
2
5
|
|
3
6
|
module ARQLogger
|
4
|
-
|
5
|
-
|
7
|
+
cattr_accessor :store, :mon
|
8
|
+
|
9
|
+
self.store = {}
|
10
|
+
self.mon = Monitor.new
|
6
11
|
|
7
|
-
|
12
|
+
class << self
|
13
|
+
def sql(event)
|
8
14
|
return unless store
|
9
15
|
|
10
16
|
payload = event.payload
|
11
17
|
if payload[:name] && !ActiveRecord::LogSubscriber::IGNORE_PAYLOAD_NAMES.include?(payload[:name])
|
12
|
-
store.push(event.duration)
|
18
|
+
mon.synchronize { store[key].push(event.duration) }
|
13
19
|
end
|
14
20
|
end
|
15
21
|
|
16
22
|
def instantiate
|
17
|
-
|
23
|
+
mon.synchronize { store[key] && store[key].instantiate }
|
18
24
|
end
|
19
25
|
|
20
26
|
def log(&block)
|
@@ -26,28 +32,57 @@ module ARQLogger
|
|
26
32
|
private
|
27
33
|
|
28
34
|
def start
|
29
|
-
|
30
|
-
|
35
|
+
mon.synchronize do
|
36
|
+
raise CannotNestedStarting if store[key]
|
37
|
+
|
38
|
+
store[key] = Log.new(start_at: Time.now.to_i)
|
39
|
+
end
|
31
40
|
end
|
32
41
|
|
33
42
|
def finish
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
43
|
+
mon.synchronize do
|
44
|
+
store.delete(key).tap do |result|
|
45
|
+
result.finish(Time.now.to_i)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
39
49
|
|
40
|
-
|
41
|
-
|
42
|
-
msec: logged.sum.round(1),
|
43
|
-
instances: instances
|
44
|
-
)
|
50
|
+
def key
|
51
|
+
Thread.current.object_id
|
45
52
|
end
|
46
53
|
end
|
47
54
|
|
48
|
-
class
|
49
|
-
|
50
|
-
|
55
|
+
class Log
|
56
|
+
attr_accessor :start_at, :queries, :duration, :instances
|
57
|
+
|
58
|
+
def initialize(start_at:)
|
59
|
+
@start_at = start_at
|
60
|
+
|
61
|
+
@queries = []
|
62
|
+
@instances = 0
|
63
|
+
end
|
64
|
+
|
65
|
+
def finish(end_at)
|
66
|
+
@duration = end_at - start_at
|
67
|
+
end
|
68
|
+
|
69
|
+
def instantiate
|
70
|
+
@instances += 1
|
71
|
+
end
|
72
|
+
|
73
|
+
def push(query)
|
74
|
+
@queries.push(query)
|
51
75
|
end
|
76
|
+
|
77
|
+
def count
|
78
|
+
@queries.size
|
79
|
+
end
|
80
|
+
|
81
|
+
def queries_time
|
82
|
+
@queries.sum.round(1)
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
class CannotNestedStarting < StandardError
|
52
87
|
end
|
53
88
|
end
|
@@ -6,7 +6,7 @@ module ARQLogger
|
|
6
6
|
alias_method :real_sql, :sql
|
7
7
|
|
8
8
|
def sql(event)
|
9
|
-
ARQLogger.
|
9
|
+
ARQLogger.sql(event)
|
10
10
|
real_sql(event)
|
11
11
|
end
|
12
12
|
end
|
@@ -26,3 +26,4 @@ elsif defined?(::Rails::Railtie)
|
|
26
26
|
initializer('add pass method') { ARQLogger::Initializer.patch }
|
27
27
|
end
|
28
28
|
end
|
29
|
+
|
data/lib/a_r_q_logger/version.rb
CHANGED
data/spec/test_model_spec.rb
CHANGED
@@ -3,13 +3,12 @@ require 'supports/schema'
|
|
3
3
|
require 'models/test_child_model'
|
4
4
|
require 'models/test_model'
|
5
5
|
require 'tempfile'
|
6
|
+
require 'securerandom'
|
6
7
|
|
7
8
|
ActiveRecord::Base.logger = Logger.new(Tempfile.new(''))
|
8
9
|
|
9
|
-
|
10
10
|
require 'a_r_q_logger'
|
11
11
|
|
12
|
-
|
13
12
|
RSpec.describe TestModel, type: :model do
|
14
13
|
it do
|
15
14
|
TestModel.create!
|
@@ -28,6 +27,44 @@ RSpec.describe TestModel, type: :model do
|
|
28
27
|
}.count).to eq(2)
|
29
28
|
end
|
30
29
|
|
30
|
+
context 'in multi thread' do
|
31
|
+
before do
|
32
|
+
TestModel.destroy_all
|
33
|
+
end
|
34
|
+
|
35
|
+
after do
|
36
|
+
TestModel.destroy_all
|
37
|
+
end
|
38
|
+
|
39
|
+
it do
|
40
|
+
threads = []
|
41
|
+
|
42
|
+
wrap = ARQLogger.log {
|
43
|
+
10.times do
|
44
|
+
threads.push(Thread.start do
|
45
|
+
ActiveRecord::Base.connection_pool.with_connection do
|
46
|
+
sleep rand(0..2) / 10
|
47
|
+
re = ARQLogger.log {
|
48
|
+
TestModel.create!
|
49
|
+
sleep rand(0..2) / 10
|
50
|
+
TestModel.create!
|
51
|
+
sleep rand(0..2) / 10
|
52
|
+
TestModel.create!
|
53
|
+
}
|
54
|
+
expect(re.count).to eq(3)
|
55
|
+
expect(re.instances).to eq(3)
|
56
|
+
end
|
57
|
+
end)
|
58
|
+
end
|
59
|
+
}
|
60
|
+
|
61
|
+
threads.each { |t| t.join }
|
62
|
+
|
63
|
+
expect(wrap.count).to eq(0)
|
64
|
+
expect(TestModel.count).to eq(30)
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
31
68
|
context 'with associations' do
|
32
69
|
before :all do
|
33
70
|
TestModel.delete_all
|