fiddlesticks 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/LICENSE +21 -0
- data/README.md +49 -0
- data/lib/fiddlesticks.rb +1 -46
- data/lib/fiddlesticks/core.rb +65 -0
- data/lib/fiddlesticks/memory.rb +56 -0
- metadata +10 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 40b1caf285de1c64e0b825ef56e639243df97315
|
4
|
+
data.tar.gz: 6a9b3bbaaaaa9535fc061aed902a5465416e1799
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7041642fbaea7929c055fdd6f197ec2d4490f3138a47189e189f01a1706ebeb4a261b2e46472c0bf666d19dca593766a69787c4cc3bf0276b1bac166519c85d6
|
7
|
+
data.tar.gz: ba974eff589d9037db4c35a4f4f46b71541e50ee717c6be76d226c844463956a2b5d667d3ad1a170458ed9fa094f4fb3d0a48af20a7eea73866ea5f737841946
|
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2017 Björn Grunde
|
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 in all
|
13
|
+
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 NONINFRINGEMENT. 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.
|
data/README.md
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
# fiddlesticks
|
2
|
+
|
3
|
+
Simple wrapper to measure optimization or to find bottlenecks in your codebase.
|
4
|
+
|
5
|
+
As for now:
|
6
|
+
Works on Mac.
|
7
|
+
Not yet tested in Linux distributions.
|
8
|
+
Not yet implemented support for Windows.
|
9
|
+
|
10
|
+
Install
|
11
|
+
```
|
12
|
+
gem install fiddlesticks
|
13
|
+
```
|
14
|
+
Use
|
15
|
+
```
|
16
|
+
optimize = Fiddlesticks::Core.new
|
17
|
+
|
18
|
+
optimize.measure do
|
19
|
+
100000.times do
|
20
|
+
"SOME STRING".downcase
|
21
|
+
end
|
22
|
+
end
|
23
|
+
```
|
24
|
+
The ***measure*** method will accept a block and print a table with usefull information that may help when optimizing your code.
|
25
|
+
|
26
|
+
```
|
27
|
+
+--------------+---------+-----------+--------------+-------------+--------------+
|
28
|
+
| Ruby Version | GC | GC Sweeps | Total Memory | Memory Used | Time |
|
29
|
+
+--------------+---------+-----------+--------------+-------------+--------------+
|
30
|
+
| 2.4.0 | enabled | 32 | 16.00 GB | 1936.08 MB | 1.37 Seconds |
|
31
|
+
+--------------+---------+-----------+--------------+-------------+--------------+
|
32
|
+
|
33
|
+
```
|
34
|
+
|
35
|
+
If you want to print out memory in different formats user ***configure*** method. It accepts a Hash with the keys:
|
36
|
+
1. total_memory: [Symbol] format: ':kb', ':mb', ':gb', standard ':gb'
|
37
|
+
2. used_memory: [Symbol] format: ':kb', ':mb', ':gb', standard ':mb'
|
38
|
+
3. gc_enabled: [Boolean] format: 'true, 'false', standard 'true'
|
39
|
+
|
40
|
+
```
|
41
|
+
optimize = Fiddlesticks::Core.new
|
42
|
+
|
43
|
+
optimize.configure({total_memory: :mb, used_memory: :kb, gc_enabled: false })
|
44
|
+
|
45
|
+
optimize.measure do
|
46
|
+
#run a block of code
|
47
|
+
end
|
48
|
+
```
|
49
|
+
Will support Mac, Linux and Windows once the project is completed.
|
data/lib/fiddlesticks.rb
CHANGED
@@ -1,46 +1 @@
|
|
1
|
-
require
|
2
|
-
require "color-console"
|
3
|
-
require "fiddlesticks/os"
|
4
|
-
|
5
|
-
class Fiddlesticks
|
6
|
-
|
7
|
-
def measure(&block)
|
8
|
-
no_gc = (ARGV[0] == "--no-gc")
|
9
|
-
no_gc ? GC.disable : GC.start
|
10
|
-
|
11
|
-
memory_before = `ps -o rss= -p #{Process.pid}`.to_i/1024
|
12
|
-
gc_stat_before = GC.stat
|
13
|
-
|
14
|
-
time = Benchmark.realtime do
|
15
|
-
yield
|
16
|
-
end
|
17
|
-
|
18
|
-
unless no_gc
|
19
|
-
GC.start(full_mark: true, immediate_sweep: true, immediate_mark: false)
|
20
|
-
end
|
21
|
-
|
22
|
-
gc_stat_after = GC.stat
|
23
|
-
memory_after = `ps -o rss= -p #{Process.pid}`.to_i/1024
|
24
|
-
|
25
|
-
if OS.windows?
|
26
|
-
total_memory = "Support coming soon"
|
27
|
-
elsif OS.mac?
|
28
|
-
total_memory = "%d GB" % (`sysctl hw.memsize`.split(" ").last.to_i/1024/1024/1000)
|
29
|
-
elsif OS.linux?
|
30
|
-
total_memory = %x(free).split(" ")[7].to_i/1024
|
31
|
-
else
|
32
|
-
total_memory = "OS Not Found!"
|
33
|
-
end
|
34
|
-
|
35
|
-
memory_used = "%d MB" % (memory_after - memory_before)
|
36
|
-
|
37
|
-
gc = no_gc ? "disabled" : "enabled"
|
38
|
-
gc_count = gc_stat_after[:count] - gc_stat_before[:count]
|
39
|
-
|
40
|
-
|
41
|
-
header = ["Ruby Version", "GC", "GC Sweeps", "Total Memory", "Memory Used", "Time"]
|
42
|
-
data = [ RUBY_VERSION, gc, gc_count, total_memory, memory_used, time.round(2)]
|
43
|
-
|
44
|
-
Console.display_table([header, data], width: 100, col_sep: "|", row_sep: "-")
|
45
|
-
end
|
46
|
-
end
|
1
|
+
require 'fiddlesticks/core.rb'
|
@@ -0,0 +1,65 @@
|
|
1
|
+
require "benchmark"
|
2
|
+
require "color-console"
|
3
|
+
require "fiddlesticks/os"
|
4
|
+
require "fiddlesticks/memory"
|
5
|
+
|
6
|
+
# Fiddlesticks runs a block of code and measures memory usage and execution time.
|
7
|
+
# @author Björn Grunde
|
8
|
+
module Fiddlesticks
|
9
|
+
class Core
|
10
|
+
include OS
|
11
|
+
include Memory
|
12
|
+
|
13
|
+
|
14
|
+
def initialize(total_memory: :gb, used_memory: :mb, gc_enabled: true)
|
15
|
+
@config = {
|
16
|
+
total_memory: total_memory,
|
17
|
+
used_memory: used_memory,
|
18
|
+
gc_enabled: gc_enabled
|
19
|
+
}
|
20
|
+
@valid_keys = @config.keys
|
21
|
+
end
|
22
|
+
|
23
|
+
def configure(opt = {})
|
24
|
+
#@params opt [Hash]
|
25
|
+
#@description Accepts a [Hash] with the keys:
|
26
|
+
# total_memory [Symbol] format type ':kb', ':mb', 'gb' standard: ':gb',
|
27
|
+
# used_memory [Symbol] format type ':kb', ':mb', 'gb' standard: ':mb',
|
28
|
+
# gc_enabled [Boolean] format type 'true', 'false' standard: 'true'
|
29
|
+
opt.each { |k, v| @config[k.to_sym] = v if @valid_keys.include? k.to_sym }
|
30
|
+
end
|
31
|
+
|
32
|
+
def measure(&block)
|
33
|
+
total_memory_type = @config.fetch(:total_memory)
|
34
|
+
used_memory_type = @config.fetch(:used_memory)
|
35
|
+
gc_enabled = @config.fetch(:gc_enabled)
|
36
|
+
|
37
|
+
gc_enabled ? GC.start : GC.disable
|
38
|
+
memory_before = get_memory(used_memory_type)
|
39
|
+
gc_stat_before = GC.stat
|
40
|
+
|
41
|
+
time = Benchmark.realtime do
|
42
|
+
yield
|
43
|
+
end
|
44
|
+
|
45
|
+
if gc_enabled
|
46
|
+
GC.start(full_mark: true, immediate_sweep: true, immediate_mark: false)
|
47
|
+
end
|
48
|
+
|
49
|
+
gc_stat_after = GC.stat
|
50
|
+
memory_after = get_memory(used_memory_type)
|
51
|
+
memory_total = total_memory(total_memory_type)
|
52
|
+
memory_used = calculate_memory(memory_before, memory_after, used_memory_type)
|
53
|
+
|
54
|
+
gc = gc_enabled ? "enabled" : "disabled"
|
55
|
+
|
56
|
+
gc_count = gc_stat_after[:count] - gc_stat_before[:count]
|
57
|
+
|
58
|
+
|
59
|
+
header = ["Ruby Version", "GC", "GC Sweeps", "Total Memory", "Memory Used", "Time"]
|
60
|
+
data = [ RUBY_VERSION, gc, gc_count, memory_total, memory_used, "#{time.round(2)} Seconds"]
|
61
|
+
|
62
|
+
Console.display_table([header, data], width: 100, col_sep: "|", row_sep: "-")
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
module Fiddlesticks
|
2
|
+
module Memory
|
3
|
+
|
4
|
+
def get_memory type
|
5
|
+
type.to_sym
|
6
|
+
memory = `ps -o rss= -p #{Process.pid}`.to_f
|
7
|
+
|
8
|
+
case type
|
9
|
+
when :mb then memory = memory/1024
|
10
|
+
when :gb then memory = memory/1024/1024
|
11
|
+
when :kb then memory
|
12
|
+
end
|
13
|
+
|
14
|
+
return memory if [:kb, :mb, :gb].include?(type)
|
15
|
+
raise ArgumentError.new("Argument is not of type Symbol, expected one of(:kb, :mb, :gb)")
|
16
|
+
end
|
17
|
+
|
18
|
+
def total_memory type
|
19
|
+
type.to_sym
|
20
|
+
os = ""
|
21
|
+
|
22
|
+
if OS.windows?
|
23
|
+
total_memory = ""
|
24
|
+
os = "Windows"
|
25
|
+
elsif OS.mac?
|
26
|
+
total_memory = `sysctl hw.memsize`.split(" ").last.to_i
|
27
|
+
os = "Mac"
|
28
|
+
elsif OS.linux?
|
29
|
+
total_memory = %x(free).split(" ")[7].to_i
|
30
|
+
os = "Linux"
|
31
|
+
else
|
32
|
+
total_memory = ""
|
33
|
+
os = "Unkown OS"
|
34
|
+
end
|
35
|
+
|
36
|
+
case type
|
37
|
+
when :kb then total_memory
|
38
|
+
when :mb then total_memory = total_memory/1024
|
39
|
+
when :gb then total_memory = total_memory/1024/1024/1000
|
40
|
+
end
|
41
|
+
|
42
|
+
return "%.2f #{type.upcase}" % total_memory if [:kb, :mb, :gb].include?(type) && total_memory.is_a?(Integer)
|
43
|
+
|
44
|
+
raise ArgumentError.new("Argument is not of type Symbol, expected one of(:kb, :mb, :gb)")
|
45
|
+
raise NotImplementedError.new("Fiddlesticks either lack support or could not figure out the os: #{os}") if ["Windows", "Unkown OS"].include?(os)
|
46
|
+
end
|
47
|
+
|
48
|
+
def calculate_memory before, after, type
|
49
|
+
before.to_f && after.to_f && type.to_sym
|
50
|
+
|
51
|
+
memory_used = after - before
|
52
|
+
return memory_used = "%g #{type.upcase}" % ("%.4f" % memory_used) unless memory_used == 0
|
53
|
+
"Less than 1 MB"
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: fiddlesticks
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Björn Grunde
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-05-
|
11
|
+
date: 2017-05-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: color-console
|
@@ -29,11 +29,17 @@ description: Fiddlesticks accepts a block of code and willl show the memory used
|
|
29
29
|
email: bjorngrunde@live.se
|
30
30
|
executables: []
|
31
31
|
extensions: []
|
32
|
-
extra_rdoc_files:
|
32
|
+
extra_rdoc_files:
|
33
|
+
- README.md
|
34
|
+
- LICENSE
|
33
35
|
files:
|
36
|
+
- LICENSE
|
37
|
+
- README.md
|
34
38
|
- lib/fiddlesticks.rb
|
39
|
+
- lib/fiddlesticks/core.rb
|
40
|
+
- lib/fiddlesticks/memory.rb
|
35
41
|
- lib/fiddlesticks/os.rb
|
36
|
-
homepage:
|
42
|
+
homepage: https://github.com/bjorngrunde/fiddlesticks
|
37
43
|
licenses:
|
38
44
|
- MIT
|
39
45
|
metadata: {}
|