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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b94654f7e00d9563e9ef60b2564e075709dadaf8
4
- data.tar.gz: 074462f2581b586eb9d9a0b3a1d71120045543b0
3
+ metadata.gz: a0b06029c982d8f0e5935128e7db6bb16526a55a
4
+ data.tar.gz: 3b82d033647c5c4da28953802d751ea56f821446
5
5
  SHA512:
6
- metadata.gz: a2e1c95ffeed6251984b87fd9ffbf45e593c1b062cbb2610c9b9cc4a0925fda262e4ddcdb8d4023df160c8c769f81538fa959cebf98dafaa5b0bab6181f69e09
7
- data.tar.gz: 81f8f2e61b78c7a015d57a372bf4dec89b61e9d3e3352a923759f7dd33e1961eea473eff302ee0bca9ca0bcfc376055f383e9a29900d876753bf40154db8c5dd
6
+ metadata.gz: 199df42d125c17554d633e51b8aaef95dafa1d114ac6650d28eafebea695d1577c2e47493a8cb9bccb4221a3ed0731cd3d89119d475286a8f60084e0148086b3
7
+ data.tar.gz: 3abfaf96cb25d9c6e946b7e4d51dddbf7117160ee5438ead5190f5d78a80d742c1d317f7233b1b21b76b7f7b37b138b21e0427aea2705b86f545dbf30abcab25
data/lib/sampling_prof.rb CHANGED
@@ -1,9 +1,13 @@
1
- require File.join(File.dirname(__FILE__), 'sampling_prof.jar')
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
- attr_accessor :output_file
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.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-13 00:00:00 Z
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