Class: MGit::Timer
- Inherits:
-
Object
- Object
- MGit::Timer
- Defined in:
- lib/m-git/foundation/timer.rb
Overview
计时器,用于统计指令执行耗时
Constant Summary collapse
- @@time_stamp =
{}
- @@duration =
{}
- @@lock =
Mutex.new
Class Method Summary collapse
-
.mutex_exec(use_lock) ⇒ Object
多线程执行保护.
-
.show_time_consuming_repos(threshold: 5) ⇒ Object
显示最耗时仓库.
-
.start(repo_name, use_lock: false) ⇒ Object
开始计时.
-
.stop(repo_name, use_lock: false) ⇒ Object
停止计时.
Class Method Details
.mutex_exec(use_lock) ⇒ Object
多线程执行保护
61 62 63 64 65 66 67 68 69 |
# File 'lib/m-git/foundation/timer.rb', line 61 def mutex_exec(use_lock) if use_lock @@lock.lock yield if block_given? @@lock.unlock else yield if block_given? end end |
.show_time_consuming_repos(threshold: 5) ⇒ Object
显示最耗时仓库
47 48 49 50 51 52 53 54 55 |
# File 'lib/m-git/foundation/timer.rb', line 47 def show_time_consuming_repos(threshold:5) repos = [] @@duration.sort_by { |repo_name,seconds| seconds }.reverse.first(5).each { |info| repo_name = info.first seconds = info.last repos.push("[#{seconds.round(2)}s]#{repo_name}") if seconds > threshold } Output.puts_remind_block(repos, "以上为最耗时且耗时超过#{threshold}s的仓库,请自行关注影响速度的原因。") if repos.length > 0 end |
.start(repo_name, use_lock: false) ⇒ Object
开始计时
19 20 21 22 23 24 25 |
# File 'lib/m-git/foundation/timer.rb', line 19 def start(repo_name, use_lock:false) return if repo_name.nil? mutex_exec(use_lock) { @@time_stamp[repo_name] = Time.new if @@time_stamp[repo_name].nil? @@duration[repo_name] = 0 if @@duration[repo_name].nil? } end |
.stop(repo_name, use_lock: false) ⇒ Object
停止计时
33 34 35 36 37 38 39 40 41 |
# File 'lib/m-git/foundation/timer.rb', line 33 def stop(repo_name, use_lock:false) return if repo_name.nil? mutex_exec(use_lock) { if !@@time_stamp[repo_name].nil? && !@@duration[repo_name].nil? @@duration[repo_name] += Time.new.to_f - @@time_stamp[repo_name].to_f @@time_stamp[repo_name] = nil end } end |