rails-pg-extras 5.1.0 → 5.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +41 -41
- data/lib/rails-pg-extras.rb +9 -8
- data/lib/rails_pg_extras/version.rb +1 -1
- data/spec/smoke_spec.rb +1 -1
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4bd83f21b85de2b816dc334b72ad1bd0dfcb0f13ea6946fec42ebb93f47ba876
|
4
|
+
data.tar.gz: 4e9e5b00cc7e07cf3baa1bc3e224fe8ab50510afa909b47e753873fb1e7a11e7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1a30823d47f9e0cab719b7ee6e750da28c751f4dc20d21756f7f3a633c07dd6390397e09502300cfcf9185184a9e00af83de40f2fd9b12a6319a6ade31ccb26f
|
7
|
+
data.tar.gz: f01d58920638949122f26a74857cdab232168b274aee2d28d180eed09ea60a48ace036aa3ba772dc8ce41893d6e4a4397255167bc5ccf699a181c7205438ed93
|
data/README.md
CHANGED
@@ -142,47 +142,47 @@ end
|
|
142
142
|
|
143
143
|
### `measure_queries`
|
144
144
|
|
145
|
-
This method displays query types executed when running a provided Ruby snippet, with their avg., min., max., and total duration. It also outputs info about the snippet execution duration and the portion spent running SQL queries (`total_duration`/`sql_duration`). It can help debug N+1 issues and review the impact of configuring eager loading:
|
146
|
-
|
147
|
-
```ruby
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
145
|
+
This method displays query types executed when running a provided Ruby snippet, with their avg., min., max., and total duration in miliseconds. It also outputs info about the snippet execution duration and the portion spent running SQL queries (`total_duration`/`sql_duration`). It can help debug N+1 issues and review the impact of configuring eager loading:
|
146
|
+
|
147
|
+
```ruby
|
148
|
+
|
149
|
+
RailsPgExtras.measure_queries { User.limit(10).map(&:team) }
|
150
|
+
|
151
|
+
{:count=>11,
|
152
|
+
:queries=>
|
153
|
+
{"SELECT \"users\".* FROM \"users\" LIMIT $1"=>
|
154
|
+
{:count=>1,
|
155
|
+
:total_duration=>3.452000004472211,
|
156
|
+
:min_duration=>3.452000004472211,
|
157
|
+
:max_duration=>3.452000004472211,
|
158
|
+
:avg_duration=>3.452000004472211},
|
159
|
+
"SELECT \"teams\".* FROM \"teams\" WHERE \"teams\".\"id\" = $1 LIMIT $2"=>
|
160
|
+
{:count=>10,
|
161
|
+
:total_duration=>14.487000000372063,
|
162
|
+
:min_duration=>0.778000001446344,
|
163
|
+
:max_duration=>1.985000002605375,
|
164
|
+
:avg_duration=>1.4487000000372063}},
|
165
|
+
:total_duration=>24.812000003294088,
|
166
|
+
:sql_duration=>17.939000004844274}
|
167
|
+
|
168
|
+
RailsPgExtras.measure_queries { User.limit(10).includes(:team).map(&:team) }
|
169
|
+
|
170
|
+
{:count=>2,
|
171
|
+
:queries=>
|
172
|
+
{"SELECT \"users\".* FROM \"users\" LIMIT $1"=>
|
173
|
+
{:count=>1,
|
174
|
+
:total_duration=>3.7079999965499155,
|
175
|
+
:min_duration=>3.7079999965499155,
|
176
|
+
:max_duration=>3.7079999965499155,
|
177
|
+
:avg_duration=>3.7079999965499155},
|
178
|
+
"SELECT \"teams\".* FROM \"teams\" WHERE \"teams\".\"id\" IN ($1, $2, $3, $4, $5, $6, $7, $8)"=>
|
179
|
+
{:count=>1,
|
180
|
+
:total_duration=>2.422000005026348,
|
181
|
+
:min_duration=>2.422000005026348,
|
182
|
+
:max_duration=>2.422000005026348,
|
183
|
+
:avg_duration=>2.422000005026348}},
|
184
|
+
:total_duration=>9.905999999318738,
|
185
|
+
:sql_duration=>6.1300000015762635}
|
186
186
|
|
187
187
|
```
|
188
188
|
|
data/lib/rails-pg-extras.rb
CHANGED
@@ -66,8 +66,7 @@ module RailsPgExtras
|
|
66
66
|
starting = Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
67
67
|
block.call
|
68
68
|
ending = Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
69
|
-
|
70
|
-
elapsed
|
69
|
+
(ending - starting) * 1000
|
71
70
|
end
|
72
71
|
|
73
72
|
def self.measure_queries(&block)
|
@@ -85,16 +84,16 @@ module RailsPgExtras
|
|
85
84
|
key = payload[:sql]
|
86
85
|
queries[key] ||= { count: 0, total_duration: 0, min_duration: nil, max_duration: nil }
|
87
86
|
queries[key][:count] += 1
|
88
|
-
duration = finish - start
|
87
|
+
duration = (finish - start) * 1000
|
89
88
|
queries[key][:total_duration] += duration
|
90
89
|
sql_duration += duration
|
91
90
|
|
92
91
|
if queries[key][:min_duration] == nil || queries[key][:min_duration] > duration
|
93
|
-
queries[key][:min_duration] = duration
|
92
|
+
queries[key][:min_duration] = duration.round(2)
|
94
93
|
end
|
95
94
|
|
96
95
|
if queries[key][:max_duration] == nil || queries[key][:max_duration] < duration
|
97
|
-
queries[key][:max_duration] = duration
|
96
|
+
queries[key][:max_duration] = duration.round(2)
|
98
97
|
end
|
99
98
|
end
|
100
99
|
end
|
@@ -104,7 +103,8 @@ module RailsPgExtras
|
|
104
103
|
end
|
105
104
|
|
106
105
|
queries = queries.reduce({}) do |agg, val|
|
107
|
-
val[1][:avg_duration] = val[1][:total_duration] / val[1][:count]
|
106
|
+
val[1][:avg_duration] = (val[1][:total_duration] / val[1][:count]).round(2)
|
107
|
+
val[1][:total_duration] = val[1][:total_duration].round(2)
|
108
108
|
agg.merge(val[0] => val[1])
|
109
109
|
end
|
110
110
|
|
@@ -112,11 +112,12 @@ module RailsPgExtras
|
|
112
112
|
{
|
113
113
|
count: queries.reduce(0) { |agg, val| agg + val[1].fetch(:count) },
|
114
114
|
queries: queries,
|
115
|
-
total_duration: total_duration,
|
116
|
-
sql_duration: sql_duration
|
115
|
+
total_duration: total_duration.round(2),
|
116
|
+
sql_duration: sql_duration.round(2)
|
117
117
|
}
|
118
118
|
end
|
119
119
|
|
120
|
+
|
120
121
|
def self.index_info(args: {}, in_format: :display_table)
|
121
122
|
data = RailsPgExtras::IndexInfo.call(args[:table_name])
|
122
123
|
|
data/spec/smoke_spec.rb
CHANGED
@@ -30,7 +30,7 @@ describe RailsPgExtras do
|
|
30
30
|
end
|
31
31
|
|
32
32
|
it "collecting queries data works" do
|
33
|
-
output = RailsPgExtras.
|
33
|
+
output = RailsPgExtras.measure_queries { RailsPgExtras.diagnose(in_format: :hash) }
|
34
34
|
expect(output.fetch(:count)).to eq 10
|
35
35
|
end
|
36
36
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rails-pg-extras
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 5.1
|
4
|
+
version: 5.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- pawurb
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-
|
11
|
+
date: 2023-03-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: ruby-pg-extras
|
@@ -16,14 +16,14 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - '='
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: 5.1
|
19
|
+
version: 5.2.1
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - '='
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: 5.1
|
26
|
+
version: 5.2.1
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: rails
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|