smpl_prflr 0.0.2 → 0.0.5

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 18ed8c169f4c2ffd164823d3e225608710e9ab8edc5d0786b308bdcca7b4eeeb
4
- data.tar.gz: fae15566f5558dd29727557f0ad831b11178ae0766c3ebd18a9afd01bb5addd6
3
+ metadata.gz: 97d77e302aa8f3e2851abaf19e1c236ca4a3445bc6d1c45607ad2a6668589d04
4
+ data.tar.gz: 90dafcbd65657ad4b04df834f1c7dfc85c26db70ff0d13e94d33cf366bc5cf8b
5
5
  SHA512:
6
- metadata.gz: '025598f88748306974a29be60ac8dadd0b3edf11bfb58b384cb308bbd98f38e96d8b9bae88ef59b5fc8da3b8f4e3e964c5b295e96ab75ff9abd189af0defdf85'
7
- data.tar.gz: 35f74d523d0dc957887a59632116a454ec084623eeb421b4276f21762cc2b68904d134b3a719df1faed2e8c31cfbbf7f28a210ffc68c683267271528ccb880b0
6
+ metadata.gz: 3e809adbeb7e3876dd53afda140c721ec2673e62fc67697d2f002700a5ddee1338c9085d1fff93e05b7b96fc33f71b24b86d9d1e7d8e854a0492af65752845a0
7
+ data.tar.gz: 39701976729f4e937b7c8aae119197e93d50480f80bfb2ab0128d4ccbdd560ecfff0e9fdee130e6bea67fb3dbfd7a86f363a092cedecc90e82b138cd75f0ae99
data/.rubocop.yml CHANGED
@@ -5,7 +5,6 @@ AllCops:
5
5
  - Rakefile
6
6
  - smpl_prflr.gemspec
7
7
  - bin/*
8
- - config.ru
9
8
 
10
9
  Style/FrozenStringLiteralComment:
11
10
  Enabled: false
data/Gemfile CHANGED
@@ -1,8 +1,3 @@
1
1
  source 'https://rubygems.org/'
2
2
  ruby '2.7.2'
3
- gemspec
4
-
5
- gem 'rack', '~> 2.2'
6
- gem 'require_all', '~> 2.0'
7
- gem 'activesupport', '~> 6.1.1'
8
- gem 'config', '~> 3.1'
3
+ gemspec
data/README.md CHANGED
@@ -4,20 +4,35 @@
4
4
  ## Dependencies:
5
5
  ```sh
6
6
  ruby '2.7.2'
7
- gem 'redis' '~> 4.6'
8
- gem 'ruby-prof' '~>1.4'
7
+ ...
8
+ gem 'redis' '4.6'
9
+ gem 'ruby-prof' '1.4'
10
+ gem 'rubocop' '1.26'
11
+ gem 'rack' '2.2'
9
12
  ```
10
13
  ## Install:
11
14
  ```sh
12
15
  bundle install smpl_prflr
13
16
 
14
17
  requre 'smpl_prflr'
18
+
15
19
  ```
16
20
  ## How is it works?:
17
21
  ```sh
18
22
  require 'smpl_prflr'
23
+
24
+ include SmplPrflr
25
+
26
+ orinitialize_profiler!
19
27
 
20
- SmplPrflr.new.p do
21
- 'your own code'
22
- end
28
+ p { block_of_your_own_code }
29
+ ```
30
+
31
+ ## As web:
32
+ ```sh
33
+ user@mac$ gem install smpl_prflr
34
+ ...
35
+ user@mac$ smpl_prflr
36
+ ...
37
+ get request: 127.0.0.1:9292/
23
38
  ```
data/bin/smpl_prflr CHANGED
@@ -1,5 +1,20 @@
1
1
  #!/usr/bin/env ruby
2
- #
3
- Dir.chdir(File.join(File.dirname(__FILE__), ".."))
4
2
 
5
- load ::File.expand_path("../../config.ru", __FILE__)
3
+ require 'rack'
4
+ require 'redis'
5
+
6
+ # @author KILYA
7
+ # View profile by redis
8
+ # port: 9292
9
+ # url: 127.0.0.1
10
+ # method GET
11
+ def my_method env
12
+ redis = Redis.new host: "127.0.0.1", port: 6379, db: 15
13
+ result = redis.get(:profile) || "Nothing to profile"
14
+ [200, {}, [result]]
15
+ rescue StandardError
16
+ [500, {}, ["Redis not available"]]
17
+ end
18
+
19
+ Rack::Handler::WEBrick.run method(:my_method), :Port => 9292
20
+
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # The MIT License (MIT)
2
4
  #
3
5
  # Copyright (c) 2019-2022 Kondratev Ilya
@@ -20,8 +22,15 @@
20
22
  # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
23
  # SOFTWARE.
22
24
 
23
- module Services
24
- class ProfileService
25
+ require 'ruby-prof'
26
+ require 'redis'
27
+
28
+ module Profiler
29
+ class Profile
30
+ class ProfilerError < StandardError; end
31
+
32
+ # @author KILYA
33
+ # Initialise redis
25
34
  def initialize
26
35
  @redis = Redis.new(
27
36
  host: "127.0.0.1",
@@ -30,8 +39,21 @@ module Services
30
39
  )
31
40
  end
32
41
 
33
- def load_profile
34
- @redis.get(:profile)
42
+ # @author KILYA
43
+ # main point
44
+ def start
45
+ output = String.new
46
+ result = RubyProf.profile do
47
+ yield
48
+ end
49
+
50
+ printer = RubyProf::FlatPrinter.new(result)
51
+ printer.print(output, min_percent: 0)
52
+ @redis.set(:profile, output)
53
+
54
+ nil
55
+ rescue StandardError => e
56
+ raise ProfilerError.new e.message.to_s
35
57
  end
36
58
  end
37
59
  end
data/lib/smpl_prflr.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # The MIT License (MIT)
2
4
  #
3
5
  # Copyright (c) 2019-2022 Kondratev Ilya
@@ -20,51 +22,33 @@
20
22
  # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
23
  # SOFTWARE.
22
24
 
23
- require 'ruby-prof'
24
- require 'redis'
25
25
  require 'logger'
26
+ require 'profiler/profile'
26
27
 
27
- class SmplPrflr
28
- class ProfilerError < StandardError; end
29
- DEFAULT_HOST = "127.0.0.1".freeze
30
-
28
+ module SmplPrflr
31
29
  # @author KILYA
32
- # @param [String] host
33
- # @param [Integer] port
34
- # @param [Integer] db
35
- # @example self.new(host: "http:/your.path", port: 555, db:1)
36
- def initialize(host: DEFAULT_HOST, port: 6379, db: 15)
30
+ # Init logger
31
+ # Init Profiler
32
+ def initialize_profiler!
37
33
  @logger = Logger.new($stdout)
38
- @redis = Redis.new(
39
- host: host,
40
- port: port,
41
- db: db
42
- )
34
+ @profiler = Profiler::Profile.new
43
35
  end
44
36
 
45
37
  # @author KILYA
46
- # @param nil
47
- #
48
38
  # @example PONG
49
39
  # @return [String] PONG
50
- def self.ping
40
+ def ping
51
41
  "PONG"
52
42
  end
53
43
 
54
44
  # @author KILYA
55
45
  # Profile block of your code
46
+ # @return [nil]
56
47
  def p
57
- profiled = ""
58
- result = RubyProf.profile do
48
+ @profiler.start do
59
49
  yield
60
50
  end
61
-
62
- printer = RubyProf::FlatPrinter.new(result)
63
- printer.print(profiled, min_percent: 0)
64
- @redis.set(:profile, profiled)
65
-
66
- nil
67
- rescue StandardError => e
51
+ rescue Profiler::ProfilerError => e
68
52
  @logger.error(e.message.to_s)
69
53
  end
70
54
  end
data/smpl_prflr.gemspec CHANGED
@@ -7,7 +7,7 @@ Gem::Specification.new do |s|
7
7
  s.rubygems_version = '2.7'
8
8
  s.required_ruby_version = '>=2.2'
9
9
  s.name = 'smpl_prflr'
10
- s.version = '0.0.2'
10
+ s.version = '0.0.5'
11
11
  s.executables << 'smpl_prflr'
12
12
  s.license = 'MIT'
13
13
  s.summary = 'Profiler'
@@ -20,4 +20,5 @@ Gem::Specification.new do |s|
20
20
  s.add_dependency 'rubocop-rake'
21
21
  s.add_dependency 'ruby-prof', '~> 1.4', '>= 1.4.3'
22
22
  s.add_dependency 'redis', '~> 4.6'
23
+ s.add_dependency 'rack', '~> 2.2'
23
24
  end
data/test/test_ping.rb CHANGED
@@ -2,8 +2,9 @@ require 'minitest/autorun'
2
2
  require 'smpl_prflr'
3
3
 
4
4
  class TestPing < Minitest::Test
5
+ include SmplPrflr
6
+
5
7
  def test_reverse_string
6
- ping = SmplPrflr.ping
7
8
  assert_equal("PONG", ping)
8
9
  end
9
10
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: smpl_prflr
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ilya Kondratev
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-04-03 00:00:00.000000000 Z
11
+ date: 2022-04-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rubocop
@@ -72,6 +72,20 @@ dependencies:
72
72
  - - "~>"
73
73
  - !ruby/object:Gem::Version
74
74
  version: '4.6'
75
+ - !ruby/object:Gem::Dependency
76
+ name: rack
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - "~>"
80
+ - !ruby/object:Gem::Version
81
+ version: '2.2'
82
+ type: :runtime
83
+ prerelease: false
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - "~>"
87
+ - !ruby/object:Gem::Version
88
+ version: '2.2'
75
89
  description: SmplPrflr for profiler own code.
76
90
  email: ilyafulleveline@gmail.com
77
91
  executables:
@@ -86,15 +100,8 @@ files:
86
100
  - LICENSE.txt
87
101
  - README.md
88
102
  - Rakefile
89
- - app/controllers/base_controller.rb
90
- - app/controllers/services_controller.rb
91
- - app/services/profile_service.rb
92
103
  - bin/smpl_prflr
93
- - config.ru
94
- - config/app/app.rb
95
- - config/app/router.rb
96
- - config/application.rb
97
- - config/initializers/001_settings.rb
104
+ - lib/profiler/profile.rb
98
105
  - lib/smpl_prflr.rb
99
106
  - smpl_prflr.gemspec
100
107
  - test/test_ping.rb
@@ -1,38 +0,0 @@
1
- # The MIT License (MIT)
2
- #
3
- # Copyright (c) 2019-2022 Kondratev Ilya
4
- #
5
- # Permission is hereby granted, free of charge, to any person obtaining a copy
6
- # of this software and associated documentation files (the "Software"), to deal
7
- # in the Software without restriction, including without limitation the rights
8
- # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- # copies of the Software, and to permit persons to whom the Software is
10
- # furnished to do so, subject to the following conditions:
11
- #
12
- # The above copyright notice and this permission notice shall be included
13
- # in all copies or substantial portions of the Software.
14
- #
15
- # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- # FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
18
- # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- # SOFTWARE.
22
-
23
- class BaseController
24
- def initialize(request)
25
- @request = request
26
- @response = Services::ProfileService.new
27
- end
28
-
29
- def not_found
30
- build_response("not_found", status: 404)
31
- end
32
-
33
- private
34
-
35
- def build_response(body, status: 200)
36
- [status, { "Content-Type" => "text/json" }, [body]]
37
- end
38
- end
@@ -1,33 +0,0 @@
1
- # The MIT License (MIT)
2
- #
3
- # Copyright (c) 2019-2022 Kondratev Ilya
4
- #
5
- # Permission is hereby granted, free of charge, to any person obtaining a copy
6
- # of this software and associated documentation files (the "Software"), to deal
7
- # in the Software without restriction, including without limitation the rights
8
- # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- # copies of the Software, and to permit persons to whom the Software is
10
- # furnished to do so, subject to the following conditions:
11
- #
12
- # The above copyright notice and this permission notice shall be included
13
- # in all copies or substantial portions of the Software.
14
- #
15
- # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- # FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
18
- # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- # SOFTWARE.
22
-
23
- class ServicesController < BaseController
24
- def ping
25
- result = "PONG"
26
- build_response(result)
27
- end
28
-
29
- def profile
30
- result = @response.load_profile
31
- build_response(result)
32
- end
33
- end
data/config/app/app.rb DELETED
@@ -1,32 +0,0 @@
1
- # The MIT License (MIT)
2
- #
3
- # Copyright (c) 2019-2022 Kondratev Ilya
4
- #
5
- # Permission is hereby granted, free of charge, to any person obtaining a copy
6
- # of this software and associated documentation files (the "Software"), to deal
7
- # in the Software without restriction, including without limitation the rights
8
- # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- # copies of the Software, and to permit persons to whom the Software is
10
- # furnished to do so, subject to the following conditions:
11
- #
12
- # The above copyright notice and this permission notice shall be included
13
- # in all copies or substantial portions of the Software.
14
- #
15
- # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- # FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
18
- # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- # SOFTWARE.
22
-
23
- class App
24
- def call(env)
25
- request = Rack::Request.new(env)
26
- serve_request(request)
27
- end
28
-
29
- def serve_request(request)
30
- Router.new(request).route!
31
- end
32
- end
data/config/app/router.rb DELETED
@@ -1,50 +0,0 @@
1
- # The MIT License (MIT)
2
- #
3
- # Copyright (c) 2019-2022 Kondratev Ilya
4
- #
5
- # Permission is hereby granted, free of charge, to any person obtaining a copy
6
- # of this software and associated documentation files (the "Software"), to deal
7
- # in the Software without restriction, including without limitation the rights
8
- # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- # copies of the Software, and to permit persons to whom the Software is
10
- # furnished to do so, subject to the following conditions:
11
- #
12
- # The above copyright notice and this permission notice shall be included
13
- # in all copies or substantial portions of the Software.
14
- #
15
- # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- # FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
18
- # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- # SOFTWARE.
22
-
23
- class Router
24
- def initialize(request)
25
- @request = request
26
- end
27
-
28
- def route!
29
- return controller.not_found unless @request.get?
30
-
31
- case @request.path
32
- when "/ping"
33
- controller.ping
34
- when "/profile"
35
- controller.profile
36
- else
37
- controller.not_found
38
- end
39
- end
40
-
41
- private
42
-
43
- def controller
44
- @controller ||= ServicesController.new(@request)
45
- end
46
-
47
- def params
48
- JSON.parse(@request.body.read)
49
- end
50
- end
@@ -1,15 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- # require gems
4
- require 'bundler'
5
-
6
- Bundler.require(:default, ENV["RACK_ENV"] || "development")
7
-
8
- # require additional gem files
9
- require "active_support/all"
10
-
11
- # initialize application, logger, gems, etc.
12
- require_all "config/initializers"
13
-
14
- # require application
15
- require_all "app"
@@ -1,14 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "config"
4
-
5
- Config.setup do |config|
6
- config.const_name = "Settings"
7
- config.use_env = false
8
- end
9
-
10
- env = ::ActiveSupport::StringInquirer.new(ENV["RACK_ENV"] || "development")
11
- path = Dir.pwd << ("/config")
12
- Config.load_and_set_settings(Config.setting_files(path, env))
13
-
14
- Settings.env = env
data/config.ru DELETED
@@ -1,4 +0,0 @@
1
- require_relative "config/application"
2
- require_all "./config/app"
3
-
4
- run Rack::URLMap.new('/' => Rack::Builder.new { run App.new })