vuf 1.0.0 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,93 @@
1
+ module Vuf
2
+ class Profiler
3
+ def initialize
4
+ @mutex = Mutex.new
5
+ @delay =
6
+ @stats = {:counter => {}, :time => {}, :avtime => {}}
7
+ @timers = {}
8
+ @enable = false
9
+ end
10
+
11
+ def enable ; @enable = true ; end
12
+ def disable ; @enable = false ; end
13
+
14
+ def count(label)
15
+ @mutex.synchronize {
16
+ @stats[:counter][label] ||= 0
17
+ @stats[:counter][label] += 1
18
+ }
19
+ end
20
+ def start(label)
21
+ @mutex.synchronize { @timers[[Thread.current,label]] = Time.new }
22
+ end
23
+
24
+ def stop(label)
25
+ new_time = Time.new
26
+ @mutex.synchronize {
27
+ time = @timers.delete([Thread.current,label])
28
+ if time
29
+ @stats[:time][label] ||= []
30
+ @stats[:time][label] << new_time - time
31
+ end
32
+ }
33
+ end
34
+
35
+ def result
36
+ counter_stat = [] ; time_stat = []
37
+ error = nil
38
+ @mutex.synchronize {
39
+ begin
40
+ @stats[:counter].each{|k,v| counter_stat << "#{k} => #{v.to_s.rjust(10,' ')}" }
41
+ @stats[:time].each{ |k,v|
42
+ # retrieve previous av_time and number of mesures (nbdata)
43
+ av_time,nbdata = @stats[:avtime][k]
44
+ # Init values if not already done
45
+ av_time ||= 0 ; nbdata ||= 0
46
+ # retrieve cumultime from previous average time
47
+ cumul_time = av_time * nbdata
48
+ unless v.nil?
49
+ v.each{|time| cumul_time += time}
50
+ # increase number of mesures with number of new mesure
51
+ nbdata += v.size
52
+ end
53
+ av_time = cumul_time / nbdata
54
+ time_stat << "#{k.to_s.rjust(20,' ')} => #{av_time.round(8).to_s.rjust(12,' ')} s/op " +
55
+ "[#{nbdata.to_s.rjust(12,' ')} op| #{(nbdata*av_time).round(2).to_s.rjust(8,' ')} s]"
56
+ @stats[:avtime][k] = [av_time,nbdata]
57
+ @stats[:time][k] = nil
58
+ }
59
+ rescue => e
60
+ Logger.error {"Error in statistic #{e.message}\n#{e.backtrace.join("\n")}"}
61
+ error = e
62
+ end
63
+ }
64
+ raise error if error
65
+ [counter_stat, time_stat].flatten.join("\n - ")
66
+ end
67
+
68
+ def finalize
69
+ return if @stat_thread.nil? || !@stat_thread.alive?
70
+ @running = false
71
+ @stat_thread.wakeup
72
+ @stat_thread.join
73
+ @stat_thread=nil
74
+ end
75
+
76
+ def run(delay=10)
77
+ return if @stat_thread
78
+
79
+ @stat_thread = Thread.new do
80
+ enable ; GC::Profiler.enable ; @running = true
81
+ while @running
82
+ start('total') ; sleep(delay) ; stop('total')
83
+ gc_result = [] ; GC::Profiler.result.each_line{|l| gc_result << l}
84
+ Logger.info "Profiling Result : \n - #{result}\n#{gc_result.shift}#{gc_result.first}#{gc_result.last}"
85
+ end
86
+ GC::Profiler.disable ; disable
87
+ end
88
+ # Ensure that finaliz was call at exit
89
+ Kernel.at_exit { finalize }
90
+ end
91
+
92
+ end
93
+ end
data/lib/vuf/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Vuf
2
- VERSION = "1.0.0"
2
+ VERSION = "1.0.1"
3
3
  end
@@ -0,0 +1,15 @@
1
+ require_relative '../../test_helper'
2
+
3
+ describe Vuf::Profiler do
4
+ subject { Vuf::Profiler.new }
5
+
6
+ it "must parse options without errors" do
7
+ subject.run(0.1)
8
+ 10000.times do
9
+ subject.start('test')
10
+ sleep(rand(10)/1000)
11
+ subject.stop('test')
12
+ end
13
+ subject.finalize
14
+ end
15
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: vuf
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -59,10 +59,12 @@ files:
59
59
  - lib/vuf/batch.rb
60
60
  - lib/vuf/logger.rb
61
61
  - lib/vuf/pool.rb
62
+ - lib/vuf/profiler.rb
62
63
  - lib/vuf/version.rb
63
64
  - lib/vuf/working_pool.rb
64
65
  - test/lib/vuf/batch_test.rb
65
66
  - test/lib/vuf/pool_test.rb
67
+ - test/lib/vuf/profiler_test.rb
66
68
  - test/lib/vuf/wp_test.rb
67
69
  - test/test_helper.rb
68
70
  - test/tmp/.gitignore
@@ -96,6 +98,7 @@ summary: This gem provide usefulle patterns like workingpool, batch executor, ob
96
98
  test_files:
97
99
  - test/lib/vuf/batch_test.rb
98
100
  - test/lib/vuf/pool_test.rb
101
+ - test/lib/vuf/profiler_test.rb
99
102
  - test/lib/vuf/wp_test.rb
100
103
  - test/test_helper.rb
101
104
  - test/tmp/.gitignore