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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 73dcd0a5bfb16dd7b0889be277962c454ba6f794
4
- data.tar.gz: 70378c5839a001193cfd8e6a3569932f46828101
3
+ metadata.gz: e77f5f0ec1b623ab24e5e2b493181138511469f6
4
+ data.tar.gz: 8592b04bd9f8d2cf749947ece215f3702546e6de
5
5
  SHA512:
6
- metadata.gz: 99d6b045d48b0174cf4e608b12fee971ab1a748a50c692681d4d30bd54bbdc989b43eb85156f5992acfb0e889f94c06e6d03cc9cf649e7b7c8614493c433010b
7
- data.tar.gz: 76259a3913e244d752888d755eb98b10e57e62501d24c2499199d9b7bdce860f818a43533f282f3be3b26daf7becd2c8eb796161f6bc34e3d47ee7e7d9dd479a
6
+ metadata.gz: 567bc7769be381d3090b53047bc8b30c75710cc45980a00cc61f4eeeab2c674da1ea6ab7b5f790712b3b88e303737d54dc58bd21a999ba207e7a4609aff5c407
7
+ data.tar.gz: d46ef32a7c741784ea67f5d2ef3605c065df1b6befbb5a436d5b741cc9c8496dbddb647960548ecddb6d745467e74dcbd2692e729a2bf4e837c49ada14febd79
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- a_r_q_logger (0.0.6)
4
+ a_r_q_logger (0.0.8)
5
5
  activerecord (>= 4.2)
6
6
 
7
7
  GEM
@@ -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
- class << self
5
- attr_accessor :store, :instantiating
7
+ cattr_accessor :store, :mon
8
+
9
+ self.store = {}
10
+ self.mon = Monitor.new
6
11
 
7
- def pass(event)
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
- self.instantiating += 1 if instantiating
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
- self.store = []
30
- self.instantiating = 0
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
- logged = store
35
- instances = instantiating
36
-
37
- self.store = nil
38
- self.instantiating = nil
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
- Result.new(
41
- count: logged.size,
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 Result < Struct.new(:count, :msec, :instances)
49
- def initialize(count:, msec:, instances:)
50
- super(count, msec, instances)
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.pass(event)
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
+
@@ -1,3 +1,3 @@
1
1
  module ARQLogger
2
- VERSION = "0.0.8"
2
+ VERSION = "1.0.0"
3
3
  end
@@ -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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: a_r_q_logger
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.8
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - mmmpa