Skip to content

Commit d47619a

Browse files
authored
Add time to query counter (#22)
1 parent 0943558 commit d47619a

File tree

5 files changed

+12
-5
lines changed

5 files changed

+12
-5
lines changed

.rubocop_todo.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
# Offense count: 2
1010
Metrics/AbcSize:
11-
Max: 19
11+
Max: 20
1212

1313
# Offense count: 3
1414
# Configuration parameters: CountComments, ExcludedMethods.

CHANGELOG.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
66

77
## [Unreleased]
88

9+
## [0.5.0] - 2020-07-23
10+
### Changed
11+
- Add time information to query counter
12+
913
## [0.4.0] - 2020-07-20
1014
### Changed
1115
- Upgrade the Rails dependency to allow for Rails 6.1
@@ -23,7 +27,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2327
### Added
2428
- First versions as a public ruby gem.
2529

26-
[Unreleased]: https://github.com/gusto/ar-query-matchers/compare/v0.4.0...HEAD
30+
[Unreleased]: https://github.com/gusto/ar-query-matchers/compare/v0.5.0...HEAD
31+
[0.5.0]: https://github.com/gusto/ar-query-matchers/releases/tag/v0.5.0
2732
[0.4.0]: https://github.com/gusto/ar-query-matchers/releases/tag/v0.4.0
2833
[0.3.0]: https://github.com/gusto/ar-query-matchers/releases/tag/v0.3.0
2934
[0.2.0]: https://github.com/gusto/ar-query-matchers/releases/tag/v0.2.0

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.4.0
1+
0.5.0

lib/ar_query_matchers.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
require 'ar_query_matchers/queries/create_counter'
44
require 'ar_query_matchers/queries/load_counter'
55
require 'ar_query_matchers/queries/update_counter'
6+
require 'bigdecimal'
67

78
module ArQueryMatchers
89
module ArQueryMatchers

lib/ar_query_matchers/queries/query_counter.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ def initialize(query_filter)
6262
# @param [block] block to instrument
6363
# @return [QueryStats] stats about all the SQL queries executed during the block
6464
def instrument(&block)
65-
queries = Hash.new { |h, k| h[k] = { count: 0, lines: [] } }
65+
queries = Hash.new { |h, k| h[k] = { count: 0, lines: [], time: BigDecimal(0) } }
6666
ActiveSupport::Notifications.subscribed(to_proc(queries), 'sql.active_record', &block)
6767
QueryStats.new(queries)
6868
end
@@ -75,7 +75,7 @@ def instrument(&block)
7575
private_constant :MARGINALIA_SQL_COMMENT_PATTERN
7676

7777
def to_proc(queries)
78-
lambda do |_name, _start, _finish, _message_id, payload|
78+
lambda do |_name, start, finish, _message_id, payload|
7979
return if payload[:cached]
8080

8181
# Given a `sql.active_record` event, figure out which model is being
@@ -87,6 +87,7 @@ def to_proc(queries)
8787
comment = payload[:sql].match(MARGINALIA_SQL_COMMENT_PATTERN)
8888
queries[model_name][:lines] << comment[:line] if comment
8989
queries[model_name][:count] += 1
90+
queries[model_name][:time] += (finish - start).round(6) # Round to microseconds
9091
end
9192
end
9293
end

0 commit comments

Comments
 (0)