elasticsearch_pretty_profile 0.1.0

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 828dfd70028d77d0aa934f95ce63d839e33c9a1f926e6a466288ed5fa3c77ad0
4
+ data.tar.gz: 5141785f16e391460af6b8272155e9b01e638fe1c3e7f667f32204e8e2f87a0e
5
+ SHA512:
6
+ metadata.gz: 00db44dfb68c9f7ed0fd7904b84758ce623d992a320cfea95926b7a3f2a39acdfcd87bb2d8b7789703859e38cefdd90980d7a94ea4dc87848775f9080c16d59c
7
+ data.tar.gz: 2e414d73a7fb05879e768bce20c3c62c65a17c28f45571535d00872e6036231aecfdf6d3990e37b6cfd89cecec7c992297b45773942041aa33798c6ace36aa39
data/.gitignore ADDED
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /doc/
5
+ /pkg/
6
+ /spec/examples.txt
7
+ /tmp/
8
+ /Gemfile.lock
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --require spec_helper
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in elasticsearch_profile_pp.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,97 @@
1
+ # Elasticsearch Pretty Profile
2
+
3
+ Pretty print an elasticsearch profile.
4
+
5
+ Iterate quickly from an irb/pry session. Use your own code to run Elasticsearch
6
+ queries and pretty print the full profile or only the slow operations.
7
+
8
+ Or when setting up Kibana is too troublesome.
9
+
10
+
11
+ ## Install
12
+
13
+ Add this line to your application's Gemfile:
14
+
15
+ ```ruby
16
+ gem "elasticsearch_pretty_profile"
17
+ ```
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install elasticsearch_pretty_profile
22
+
23
+
24
+ ## Usage
25
+
26
+ ```ruby
27
+ require "elasticsearch"
28
+ client = Elasticsearch::Client.new
29
+ result = client.search(body: {profile: true})
30
+
31
+ require "elasticsearch_pretty_profile/espp"
32
+ espp result, over_ms: 50
33
+ # or
34
+ require "elasticsearch_pretty_profile"
35
+ ElasticsearchPrettyProfile.pp result, breakdown: true
36
+ ```
37
+
38
+ Options:
39
+ - `over_ms` - Filter output to operations slower than threshold. Default `1`.
40
+ - `breakdown` - Include breakdown of operations. Default `true`.
41
+
42
+ Example output:
43
+ ```
44
+ [ObNbzJLfQWaafS83U5b9lg][index][2]
45
+ 108ms BooleanQuery #ConstantScore(vehicle.state:UNBUILT) (106ms next_doc)
46
+ 397ms CancellableCollector search_cancelled
47
+ 365ms MultiCollector search_multi
48
+ 271ms MultiBucketCollector: [[term_agg_body_type, term_agg_feature, collapsed_total]] aggregation
49
+
50
+ 69ms GlobalOrdinalsStringTermsAggregator term_agg_body_type (55ms collect)
51
+ 256ms GlobalOrdinalsStringTermsAggregator term_agg_feature (118ms build_aggregation 137ms collect)
52
+ 181ms CardinalityAggregator uniq_by (178ms collect)
53
+
54
+ ```
55
+
56
+ The first line is the shard ID which consists of the node ID, index name, and
57
+ shard number.
58
+
59
+ The next lines consist of searches (queries and collectors), then a blank line,
60
+ and finally aggregations. Indentation denotes it is a child of the previous
61
+ operation.
62
+
63
+ The lines follow this pattern:
64
+ 1. the time taken in milliseconds by the query, collector, or aggregation;
65
+ 2. the type of the query or aggregation, or the name of the collector;
66
+ 3. the description of the query or aggregation, or the reason of the collector;
67
+ 4. the breakdown of the operation in parentheses at the end;
68
+
69
+ The breakdown is a list of sub-operation names with the associated time in
70
+ milliseconds preceding it.
71
+
72
+ More shards will be separated by a blank line and follow the same pattern as
73
+ above.
74
+
75
+
76
+ ## Development
77
+
78
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run
79
+ `rake spec` to run the tests. You can also run `bin/console` for an interactive
80
+ prompt that will allow you to experiment.
81
+
82
+ To install this gem onto your local machine, run `bundle exec rake install`.
83
+
84
+
85
+ ## Release
86
+
87
+ To release a new version, update the version number in `version.rb`, and then
88
+ run `bundle exec rake release`, which will create a git tag for the version,
89
+ push git commits and tags, and push the `.gem` file to [rubygems.org].
90
+
91
+ [rubygems.org]: https://rubygems.org
92
+
93
+
94
+ ## Contributing
95
+
96
+ Bug reports and pull requests are welcome on GitHub at
97
+ https://github.com/pchambino/elasticsearch_pretty_profile.
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require "bundler/setup"
2
+ require "bundler/gem_tasks"
3
+ require "rspec/core/rake_task"
4
+ require "standard/rake"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: [:spec, :standard]
data/bin/console ADDED
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "elasticsearch_pretty_profile"
5
+ require "pry"
6
+
7
+ Pry.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,29 @@
1
+ require_relative "lib/elasticsearch_pretty_profile/version"
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "elasticsearch_pretty_profile"
5
+ spec.version = ElasticsearchPrettyProfile::VERSION
6
+ spec.authors = ["Pedro Chambino"]
7
+ spec.email = ["pchambino@gmail.com"]
8
+
9
+ spec.summary = "Pretty print an elasticsearch profile"
10
+ spec.description = <<~TXT
11
+ Iterate quickly from an irb/pry session. Use your own code to run Elasticsearch
12
+ queries and pretty print the full profile or only the slow operations.
13
+
14
+ Or when setting up Kibana is too troublesome.
15
+ TXT
16
+ spec.homepage = "https://github.com/pchambino/elasticsearch_pretty_profile"
17
+
18
+ spec.metadata["allowed_push_host"] = "https://rubygems.org"
19
+
20
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.start_with? "spec" }
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.7", "< 3.1")
24
+
25
+ spec.add_development_dependency "pry", "~> 0.14"
26
+ spec.add_development_dependency "rake", "~> 13.0"
27
+ spec.add_development_dependency "rspec", "~> 3.10"
28
+ spec.add_development_dependency "standard", "~> 0.12"
29
+ end
@@ -0,0 +1,78 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ElasticsearchPrettyProfile
4
+ autoload :VERSION, "elasticsearch_profile_pp/version"
5
+
6
+ def self.pp(result, over_ms: 1, breakdown: true)
7
+ PrettyProfile.new(result, over_ms: over_ms, breakdown: breakdown)
8
+ .pretty_print
9
+ end
10
+
11
+ class PrettyProfile
12
+ def initialize(result, over_ms:, breakdown:)
13
+ @result = result
14
+ @over_ms = over_ms
15
+ @breakdown = breakdown
16
+ end
17
+
18
+ def pretty_print
19
+ profile = @result["profile"]
20
+ return unless profile
21
+
22
+ profile["shards"]&.each do |shard|
23
+ puts shard["id"]
24
+
25
+ shard["searches"]&.each do |search|
26
+ search["query"]&.each { |op| pp_operation op }
27
+ search["collector"]&.each { |op| pp_operation op }
28
+
29
+ rewrite_time = ns_to_ms search["rewrite_time"]
30
+ puts " #{rewrite_time}ms rewrite" unless rewrite_time < @over_ms
31
+ puts
32
+ end
33
+
34
+ shard["aggregations"]&.each { |op| pp_operation op }
35
+ puts
36
+ end
37
+
38
+ nil
39
+ end
40
+
41
+ private
42
+
43
+ def ns_to_ms(nanos)
44
+ return 0 unless nanos
45
+
46
+ nanos / 1000 / 1000
47
+ end
48
+
49
+ def pp_operation(op, indent = 0)
50
+ time = ns_to_ms op["time_in_nanos"]
51
+ return if time < @over_ms
52
+
53
+ puts [
54
+ " " * indent,
55
+ "#{time}ms",
56
+ op["type"] || op["name"],
57
+ op["description"] || op["reason"],
58
+ pp_breakdown(op["breakdown"])
59
+ ].compact.join " "
60
+
61
+ op["children"]&.each { |child| pp_operation child, indent + 1 }
62
+ end
63
+
64
+ def pp_breakdown(bd)
65
+ return unless @breakdown
66
+
67
+ operations = bd&.map do |name, value|
68
+ time = ns_to_ms(value)
69
+ next if time < @over_ms
70
+
71
+ "#{time}ms #{name}"
72
+ end&.compact
73
+
74
+ "(#{operations.join " "})" if operations&.any?
75
+ end
76
+ end
77
+ private_constant :PrettyProfile
78
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "elasticsearch_pretty_profile"
4
+
5
+ module Kernel
6
+ def espp(...)
7
+ ElasticsearchPrettyProfile.pp(...)
8
+ end
9
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ElasticsearchPrettyProfile
4
+ VERSION = "0.1.0"
5
+ end
metadata ADDED
@@ -0,0 +1,117 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: elasticsearch_pretty_profile
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Pedro Chambino
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-03-14 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: pry
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.14'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.14'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '13.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '13.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.10'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.10'
55
+ - !ruby/object:Gem::Dependency
56
+ name: standard
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0.12'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0.12'
69
+ description: |
70
+ Iterate quickly from an irb/pry session. Use your own code to run Elasticsearch
71
+ queries and pretty print the full profile or only the slow operations.
72
+
73
+ Or when setting up Kibana is too troublesome.
74
+ email:
75
+ - pchambino@gmail.com
76
+ executables: []
77
+ extensions: []
78
+ extra_rdoc_files: []
79
+ files:
80
+ - ".gitignore"
81
+ - ".rspec"
82
+ - Gemfile
83
+ - README.md
84
+ - Rakefile
85
+ - bin/console
86
+ - bin/setup
87
+ - elasticsearch_pretty_profile.gemspec
88
+ - lib/elasticsearch_pretty_profile.rb
89
+ - lib/elasticsearch_pretty_profile/espp.rb
90
+ - lib/elasticsearch_pretty_profile/version.rb
91
+ homepage: https://github.com/pchambino/elasticsearch_pretty_profile
92
+ licenses: []
93
+ metadata:
94
+ allowed_push_host: https://rubygems.org
95
+ post_install_message:
96
+ rdoc_options: []
97
+ require_paths:
98
+ - lib
99
+ required_ruby_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '2.7'
104
+ - - "<"
105
+ - !ruby/object:Gem::Version
106
+ version: '3.1'
107
+ required_rubygems_version: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - ">="
110
+ - !ruby/object:Gem::Version
111
+ version: '0'
112
+ requirements: []
113
+ rubygems_version: 3.1.4
114
+ signing_key:
115
+ specification_version: 4
116
+ summary: Pretty print an elasticsearch profile
117
+ test_files: []