benchify 0.1.1 → 0.2.2

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: 9ab4739b8e35a289ef0bc93d42a07e0de279bbc6811c2a25158ec72755719011
4
- data.tar.gz: 2ecbe780019eb29c010a0ea14634a53ae6744fe2c2a1e4efadca368e320c65b3
3
+ metadata.gz: 939a25dfbd759e26c076183b242de3cddd7eaa43cf3d365ade56ddab627156ac
4
+ data.tar.gz: 64169340ba3ff5bf3c6d85f9d1f4084a6e2f970465fd355bc3871d3f0baf7ee3
5
5
  SHA512:
6
- metadata.gz: f44aaf4bbcd2921215a2857bfe85f5dba5942ee5c02de193b1e15dcc792f4ecf0882a99db1f60bfb01665975ac6bd7fd9488213d80aeb863b07fb3a1cadfe998
7
- data.tar.gz: a5d0c9d184bfbba77077cf75fdfcecdfcd6893422dbda8f598f75812c9040d971ae63d87fee3d6934b577833ce8fae259b34d3ded489d83234eb7f49b67cbdf5
6
+ metadata.gz: 285d8eeff1baa8911a1973edc0b4fc3c897e16d686f5f87a6f5aa7a263b14e5a27ca15550d350f7298b413614a6d2f61c1036095ca9a5b198bc6ded409f64507
7
+ data.tar.gz: 33787391b420b5bdbd71d18945fc91264741f1972d871077aa2a5a66361b66826723bba21b0384bdd5db4e682a25de61743bb91aeb08441f1a892b7641726d26
data/README.md CHANGED
@@ -1,58 +1,128 @@
1
+ **For 0.2.0 you must call #finish to calculate results**
2
+ **execution time is saved with #call, and calculated later with #finish**
3
+
4
+ documentation will be added ...
5
+
1
6
  # Benchify
2
7
 
3
- **Benchmark Source-Code:**
8
+ [![Gem Version](https://badge.fury.io/rb/benchify.svg)](https://badge.fury.io/rb/benchify)
9
+
10
+ Benchmark measures for different languages. Compare scripts created in Ruby, Python, Bash... or check different methods execution time in Ruby source code.
11
+ Benchify count only a real execution time, in seconds. You can set a default count-type, but anyway you'll be able to access values in seconds, milliseconds, minutes and hours. You can also get a time when operation started/ended. Purpose of this is to get an easy way to count anything you do in terminal, including shell commands like `sudo apt update`.
12
+
13
+
14
+ # How to install
15
+
16
+ Install from [rubygems](https://rubygems.org/gems/benchify):
4
17
 
5
18
  ```ruby
6
- # initialize new object
7
- # accept type (:symbol) and message ('string')
19
+ gem install benchify
8
20
 
9
- benchmark = Benchify.new :min, 'processed time in minutes:'
10
- # or
11
- benchmark = Benchmark.new :sec
21
+ # or add to Gemfile, followed by bundle install
22
+ gem 'benchify', '~> 0.1.1'
23
+ ```
24
+
25
+ Install from source:
12
26
 
27
+ ```ruby
28
+ git clone https://github.com/decentralizuj/benchify
29
+ cd benchify
30
+
31
+ # no dependecies needed for production, only bundler and rake for development
32
+ bundle install
33
+ ```
34
+
35
+
36
+ # How to use
13
37
 
14
- # measure is alias for call
38
+ **Benchmark Ruby Source-Code:**
15
39
 
40
+ Initialize new object with type and message (both are optional). Default type is `:sec`, and message is `processed time`. Here's a list of available symbols for type, anything else would lead to counting in seconds.
41
+
42
+ ```ruby
43
+ hours => :h, :hour, :hours
44
+ minutes => :m, :min, :minute, :minutes
45
+ milliseconds => :ms, :milli, :milliseconds
46
+
47
+ benchmark = Benchify.new(:ms)
48
+ ```
49
+
50
+
51
+ You can override default message constant, if you want to
52
+
53
+ ```ruby
54
+ Benchify::MESSAGE = 'Your Message Here'
55
+
56
+ # or you can add a message with new object
57
+
58
+ benchmark = Benchify.new( :ms, 'Your Message Here' )
59
+ ```
60
+
61
+
62
+ Now you can call method `#call` or it's alias `#measure`
63
+
64
+ ```ruby
16
65
  benchmark.call { YOUR CODE HERE }
66
+
17
67
  # or
68
+
18
69
  benchmark.measure do
19
70
  # YOUR CODE HERE
20
71
  end
72
+
73
+ # you can also add a message as parameter
74
+
75
+ benchmark.measure('counting seconds:') do
76
+ # YOUR CODE HERE
77
+ end
21
78
  ```
22
79
 
80
+
23
81
  **Benchmark Terminal:**
24
82
 
25
- Make sure your script has environment defined ( shebang )
83
+ Make sure your script has shebang (environment defined)
26
84
 
27
85
  ` #!/usr/bin/env ruby `
28
86
 
29
87
  ```ruby
88
+ # if script is not executable, you'll be asked for permission (and sudo password)
89
+
30
90
  benchify script_1.rb script_2.py script_3.sh
31
91
  ```
32
92
 
33
- Or use `-e` to benchmark shell command
93
+ Use `-e` to benchmark shell commands (use this if you don't have shebang in script)
34
94
 
35
95
  ```ruby
36
- benchify -e 'free -m | head -n 2' 'some other command'
96
+ benchify -e 'sudo apt update' 'gem install moriarty' 'python3 somefile.py'
37
97
  ```
38
98
 
39
- # About
99
+
100
+ # Example
101
+
102
+ If you don't like name `Benchify`, you can also use `Benchmark`
40
103
 
41
104
  ```ruby
42
- benchmark = Benchmark.new :ms, ' counted milliseconds:'
105
+ benchmark = Benchmark.new :sec, ' counted seconds:'
43
106
 
44
107
  benchmark.call { ... }
45
108
  benchmark.measure('change_message:') { ... }
46
109
 
47
110
  Attributes:
48
111
 
112
+ # after initialization
49
113
  benchmark.message => 'processed time:'
50
114
  benchmark.type => :sec
51
- benchmark.start => Time.now when start
52
- benchmark.end => Time.now when end
53
- benchmark.total => Total seconds - float
54
- benchmark.time => counted time by type (:sec)
115
+
116
+ # after code finished
117
+
118
+ benchmark.start => Time.now (when started)
119
+ benchmark.end => Time.now (when ended)
120
+ benchmark.total => Total seconds (float)
121
+ benchmark.time => counted time by type (in_seconds)
55
122
  benchmark.result => message + time + type (processed time: 22.124 sec)
123
+
124
+ # available time attributes (after code finished)
125
+
56
126
  benchmark.in_seconds => access time in seconds
57
127
  benchmark.in_minutes => access time in minutes
58
128
  benchmark.in_hours => access time in hours
data/benchify.gemspec CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = 'benchify'
5
- s.version = '0.1.1'
5
+ s.version = '0.2.2'
6
6
  s.summary = 'Benchmark measures for Code and/or Terminal'
7
7
  s.description = <<~DESC
8
8
  Benchmark Ruby methods from source code, or any script from terminal.
@@ -16,7 +16,7 @@ Gem::Specification.new do |s|
16
16
  s.metadata['source_code_uri'] = 'https://github.com/decentralizuj/benchify'
17
17
  s.metadata['bug_tracker_uri'] = 'https://github.com/decentralizuj/benchify/issues'
18
18
 
19
- s.files = ['bin/benchify', 'lib/benchify.rb', 'LICENSE', 'README.md', 'benchify.gemspec']
19
+ s.files = ['bin/benchify', 'lib/benchify.rb', 'lib/benchify/benchmark.rb', 'lib/benchify/result.rb', 'LICENSE', 'README.md', 'benchify.gemspec']
20
20
  s.bindir = 'bin'
21
21
  s.executables = ['benchify']
22
22
  s.require_paths = ['lib']
data/bin/benchify CHANGED
@@ -2,11 +2,13 @@
2
2
 
3
3
  require_relative '../lib/benchify'
4
4
 
5
- if ARGV.empty?
5
+ if ARGV.empty? or ARGV.include? '-h' or ARGV.include? '--help'
6
6
 
7
7
  puts
8
8
  puts " $ benchify script_1.rb script_2.py script_3.sh"
9
9
  puts
10
+ puts " $ benchify -e 'sudo apt update' 'sudo apt upgrade'"
11
+ puts
10
12
 
11
13
  else
12
14
 
@@ -18,7 +20,9 @@ require_relative '../lib/benchify'
18
20
  if test == :file
19
21
 
20
22
  if File.executable?(script)
21
- benchmark.measure { system "./#{script}" }
23
+ benchmark.call { system "./#{script}" }
24
+ benchmark.finish
25
+ puts
22
26
  puts benchmark.result
23
27
  else
24
28
  puts "[!] NOT EXECUTABLE!"
@@ -30,13 +34,17 @@ require_relative '../lib/benchify'
30
34
  else
31
35
  system "sudo chmod +x #{script}"
32
36
  benchmark.measure { system "./#{script}" }
37
+ benchmark.finish
38
+ puts
33
39
  puts benchmark.result
34
40
  end
35
41
  end
36
42
 
37
43
  else
38
44
  next if script == '-e'
39
- benchmark.call { system "#{script}" }
45
+ benchmark.measure { system "#{script}" }
46
+ benchmark.finish
47
+ puts
40
48
  puts benchmark.result
41
49
  end
42
50
  end
data/lib/benchify.rb CHANGED
@@ -1,74 +1,5 @@
1
- class Benchify
1
+ require_relative 'benchify/benchmark'
2
2
 
3
- MESSAGE = 'processing time:'
3
+ class Benchify < Benchmark
4
4
 
5
- attr_reader :time, :message, :type, :total, :data, :start, :end
6
- attr_reader :in_seconds, :in_hours, :in_minutes, :in_milliseconds
7
-
8
- alias result data
9
-
10
- alias in_ms in_milliseconds
11
- alias in_milli in_milliseconds
12
-
13
- alias in_h in_hours
14
-
15
- alias in_sec in_seconds
16
-
17
- alias in_min in_minutes
18
-
19
-
20
- def initialize( type = :sec, message = nil )
21
-
22
- case type
23
- when :m, :min, :minute, :minutes then :min
24
- when :ms, :milli, :milliseconds then :ms
25
- when :h, :hour, :hours then :h
26
- else :sec
27
- end
28
-
29
- @type = type
30
- @message = message || MESSAGE
31
- end
32
-
33
-
34
- def call( message = nil, &operation )
35
-
36
- msg = message || @message
37
- @start = Time.now
38
-
39
- yield operation
40
-
41
- @end = Time.now
42
- @total = (@end - @start).to_f
43
-
44
- calculate_time @type
45
- @data = "#{msg} #{@time} #{@type}"
46
- end
47
-
48
- alias measure call
49
-
50
-
51
- private
52
-
53
-
54
- def calculate_time( type )
55
- @in_seconds = @total.round(4)
56
- @in_hours = (@total / 60).round(2)
57
- @in_minutes = (@total / 360).round(2)
58
- @in_milliseconds = (@total * 1000).round(5)
59
- return_by_type type
60
- end
61
-
62
-
63
- def return_by_type( type )
64
- case type
65
- when :sec then time = @in_seconds
66
- when :min then time = @in_minutes
67
- when :ms then time = @in_milliseconds
68
- when :h then time = @in_hours
69
- end
70
- return @time = time.to_s
71
- end
72
5
  end
73
-
74
- class Benchmark < Benchify; end
@@ -0,0 +1,51 @@
1
+ require_relative 'result'
2
+
3
+ class Benchmark
4
+
5
+ MESSAGE = 'processing time:'
6
+
7
+ attr_reader :type, :msg
8
+ attr_reader :data, :min, :max, :middle, :result, :results
9
+
10
+ alias message msg
11
+ alias time middle
12
+
13
+ def initialize( type = :sec, message = nil )
14
+ case type
15
+ when :m, :min, :minute, :minutes then :min
16
+ when :ms, :milli, :milliseconds then :ms
17
+ when :h, :hour, :hours then :h
18
+ else :sec
19
+ end
20
+ @data = []
21
+ @type = type
22
+ @msg = message || MESSAGE
23
+ end
24
+
25
+ def call( message = nil, &operation )
26
+ @msg = message unless message.nil?
27
+ start = Time.now
28
+ yield operation
29
+ ends = Time.now
30
+ @data << [ start, ends ]
31
+ end
32
+
33
+ alias measure call
34
+
35
+ def finish
36
+ @results = Result.new @type, @msg
37
+ @data.each do |st, en|
38
+ @results.add st, en
39
+ end
40
+ @results.get
41
+ @min = @results.min
42
+ @max = @results.max
43
+ @middle = @results.middle
44
+ @result = @results.output
45
+ end
46
+
47
+ alias end finish
48
+
49
+ # TO-DO
50
+ # save output
51
+ end
@@ -0,0 +1,45 @@
1
+ class Result
2
+
3
+ attr_reader :type, :msg, :counter, :output
4
+ attr_reader :min, :max, :middle, :all
5
+
6
+ alias message msg
7
+
8
+ def initialize( type, message )
9
+ @type = type.to_sym
10
+ @msg = message
11
+ @all = []
12
+ end
13
+
14
+ def add( start, ends )
15
+ time = ends.to_f - start.to_f
16
+ @all << time
17
+ end
18
+
19
+ def get
20
+ @counter = @all.size
21
+ @min = get_by_type @all.min
22
+ @max = get_by_type @all.max
23
+ @middle = get_by_type ( @max - ( (@max - @min) / 2 ) )
24
+ @output = create_result_message
25
+ end
26
+
27
+ def create_result_message
28
+ msg = @msg.to_s
29
+ msg += " => "
30
+ msg += "min: #{@min} #{@type} | max: #{@max} #{@type} | middle: #{@middle} #{@type}"
31
+ end
32
+
33
+ private
34
+
35
+ def get_by_type( time )
36
+ case @type
37
+ when :min then x = (time / 360).round(2)
38
+ when :ms then x = (time * 1000).round(5)
39
+ when :h then x = (time / 60).round(2)
40
+ else x = time.round(4)
41
+ end
42
+ return x
43
+ end
44
+
45
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: benchify
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - decentralizuj
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-04-16 00:00:00.000000000 Z
11
+ date: 2021-04-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -52,6 +52,8 @@ files:
52
52
  - benchify.gemspec
53
53
  - bin/benchify
54
54
  - lib/benchify.rb
55
+ - lib/benchify/benchmark.rb
56
+ - lib/benchify/result.rb
55
57
  homepage: https://github.com/decentralizuj/benchify
56
58
  licenses:
57
59
  - MIT