sampling_prof 0.1.1 → 0.1.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 +4 -4
- data/lib/sampling_prof.rb +6 -2
- data/lib/sampling_prof/internal.rb +82 -0
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a0b06029c982d8f0e5935128e7db6bb16526a55a
|
4
|
+
data.tar.gz: 3b82d033647c5c4da28953802d751ea56f821446
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 199df42d125c17554d633e51b8aaef95dafa1d114ac6650d28eafebea695d1577c2e47493a8cb9bccb4221a3ed0731cd3d89119d475286a8f60084e0148086b3
|
7
|
+
data.tar.gz: 3abfaf96cb25d9c6e946b7e4d51dddbf7117160ee5438ead5190f5d78a80d742c1d317f7233b1b21b76b7f7b37b138b21e0427aea2705b86f545dbf30abcab25
|
data/lib/sampling_prof.rb
CHANGED
@@ -1,9 +1,13 @@
|
|
1
|
-
|
1
|
+
if RUBY_PLATFORM =~ /java/
|
2
|
+
require 'sampling_prof.jar'
|
3
|
+
else
|
4
|
+
require 'sampling_prof/internal'
|
5
|
+
end
|
2
6
|
|
3
7
|
class SamplingProf
|
4
8
|
DEFAULT_OUTPUT_FILE = 'profile.txt'
|
5
9
|
|
6
|
-
|
10
|
+
attr_writer :output_file
|
7
11
|
|
8
12
|
def output_file
|
9
13
|
@output_file ||= DEFAULT_OUTPUT_FILE
|
@@ -0,0 +1,82 @@
|
|
1
|
+
|
2
|
+
class SamplingProf
|
3
|
+
class Sample < Struct.new(:self, :total)
|
4
|
+
end
|
5
|
+
|
6
|
+
class Sampling
|
7
|
+
def initialize(target)
|
8
|
+
@target = target
|
9
|
+
@samples = Hash.new{|h,k| h[k] = [0, 0] }
|
10
|
+
@call_graph = Hash.new{|h,k| h[k] = 0}
|
11
|
+
@nodes = {}
|
12
|
+
end
|
13
|
+
|
14
|
+
def result
|
15
|
+
[@nodes.to_a, @samples.to_a, @call_graph.to_a]
|
16
|
+
end
|
17
|
+
|
18
|
+
def snapshot
|
19
|
+
locations = @target.backtrace_locations
|
20
|
+
from = -1
|
21
|
+
paths = []
|
22
|
+
calls = []
|
23
|
+
locations.reverse.each_with_index do |loc, i|
|
24
|
+
node_id = node_id(loc.path)
|
25
|
+
if i == 0
|
26
|
+
@samples[node_id][0] += 1
|
27
|
+
end
|
28
|
+
|
29
|
+
path = [from, node_id]
|
30
|
+
if !paths.include?(path)
|
31
|
+
paths << path
|
32
|
+
@call_graph[path] += 1
|
33
|
+
end
|
34
|
+
if !calls.include?(node_id)
|
35
|
+
calls << node_id
|
36
|
+
@samples[node_id][1] += 1
|
37
|
+
end
|
38
|
+
from = node_id
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def node_id(path)
|
43
|
+
@nodes[path] ||= @nodes.size
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def initialize(period)
|
48
|
+
@period = period
|
49
|
+
@running = false
|
50
|
+
@sampling_thread = nil
|
51
|
+
end
|
52
|
+
|
53
|
+
def __start__(&block)
|
54
|
+
unless @running
|
55
|
+
@running = true
|
56
|
+
target = Thread.current
|
57
|
+
@sampling_thread = Thread.start do
|
58
|
+
sampling = Sampling.new(target)
|
59
|
+
loop do
|
60
|
+
break unless @running
|
61
|
+
sampling.snapshot
|
62
|
+
sleep @period
|
63
|
+
end
|
64
|
+
block.call(sampling.result)
|
65
|
+
end
|
66
|
+
true
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
def stop
|
71
|
+
if @running
|
72
|
+
@running = false
|
73
|
+
@sampling_thread.join
|
74
|
+
@sampling_thread = nil
|
75
|
+
true
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
def profiling?
|
80
|
+
!!@sampling_thread
|
81
|
+
end
|
82
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sampling_prof
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Xiao Li
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2014-02-
|
12
|
+
date: 2014-02-15 00:00:00 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rake-compiler
|
@@ -39,6 +39,7 @@ files:
|
|
39
39
|
- README.md
|
40
40
|
- lib/sampling_prof.jar
|
41
41
|
- lib/sampling_prof.rb
|
42
|
+
- lib/sampling_prof/internal.rb
|
42
43
|
homepage: https://github.com/xli/sampling_prof
|
43
44
|
licenses:
|
44
45
|
- MIT
|