リファクタリング＆コッホ曲線追加
add CpuStat

diff --git a/lib/stylet/base.rb b/lib/stylet/base.rb
index 4ae1b1a..0201ae2 100644
--- a/lib/stylet/base.rb
+++ b/lib/stylet/base.rb
@@ -20,7 +20,8 @@
 # 汎用ライブラリ
 require_relative 'palette'
 require_relative 'fps_adjust'
-require_relative 'check_fps'
+require_relative 'fps_stat'
+require_relative 'cpu_stat'
 require_relative 'logger'
 require_relative 'rect'
 require_relative 'collision_support'
diff --git a/lib/stylet/check_fps.rb b/lib/stylet/check_fps.rb
deleted file mode 100644
index 6d36cde..0000000
--- a/lib/stylet/check_fps.rb
+++ /dev/null
@@ -1,49 +0,0 @@
-# -*- coding: utf-8 -*-
-module Stylet
-  #
-  # FPSの計測
-  #
-  # Example:
-  #
-  #   obj = CheckFPS.new
-  #   loop do
-  #     obj.update
-  #     obj.fps # => 60
-  #     screen.flip
-  #   end
-  #
-  class CheckFPS
-    MSECOND = 1000.0
-
-    attr_reader :fps
-
-    # blockにはミリ秒単位で現在の時間を返すブロックを指定する
-    def initialize(&block)
-      @block = block || -> { Time.now.to_f * MSECOND }
-      @old_time = @block.call
-      @fps = 0
-      @counter = 0
-    end
-
-    # 毎フレーム呼ぶことでフレーム数を調べられる
-    def update
-      @counter += 1
-      v = @block.call
-      t = v - @old_time
-      if t > MSECOND
-        @old_time = v
-        @fps = @counter
-        @counter = 0
-      end
-    end
-  end
-end
-
-if $0 == __FILE__
-  obj = Stylet::CheckFPS.new
-  sleep(0.5)
-  obj.update
-  sleep(0.5)
-  obj.update
-  p obj.fps
-end
diff --git a/lib/stylet/core.rb b/lib/stylet/core.rb
index 677c413..8558fa7 100644
--- a/lib/stylet/core.rb
+++ b/lib/stylet/core.rb
@@ -93,7 +93,7 @@ def next_frame(&block)
       after_draw            # @screen.flip
       before_draw           # @console_current_line = 0
       background_clear
-      before_update         # vputs(system_line)
+      before_update         # vputs(system_infos)

       # ここからユーザーの処理
       # ということは next_frame の中のブロックから呼ぶ必要はない？
diff --git a/lib/stylet/cpu_stat.rb b/lib/stylet/cpu_stat.rb
new file mode 100644
index 0000000..b85ae40
--- /dev/null
+++ b/lib/stylet/cpu_stat.rb
@@ -0,0 +1,66 @@
+# -*- coding: utf-8 -*-
+#
+# CPU率の求め方
+#
+#   flip   余力    CPU
+#  -------------------
+#  16 ms   100 %    0%
+#  15 ms    90 %   10%
+#   1 ms    10 %   90%
+#   0 ms     0 %  100%
+#
+# free = (     ms) / 16 * 100
+# cpu  = (16 - ms) / 16 * 100
+#
+# 1秒単位で測定すれば / 16 は不要になる
+
+require "benchmark"
+require "active_support/core_ext/benchmark"
+
+module Stylet
+  #
+  # CPU率の測定
+  #
+  #   * 余力とCPUは反比例の関係
+  #   * なので実際はCPUではなく余力を測定することでCPU率を得る
+  #   *1フレーム毎に計算すると誤差が多いため1秒毎に計算する
+  #
+  # Example:
+  #
+  #   stat = CpuStat.new
+  #   loop do
+  #     stat.benchmark { screen.flip }       # 余力測定
+  #     stat.cpu_rate # => 80.0
+  #   end
+  #
+  class CpuStat
+    attr_reader :free_ms, :cpu_ms
+
+    def initialize
+      @free_ms = 1000.0
+      @cpu_ms = 0.0
+
+      @old_time = Time.now.to_f
+      @total_ms = 0
+    end
+
+    def benchmark
+      @total_ms += Benchmark.ms { yield }
+      now = Time.now.to_f
+      if now >= @old_time + 1.0
+        @old_time = now
+        @free_ms = @total_ms
+        @cpu_ms = 1000.0 - @free_ms
+        @total_ms = 0
+      end
+    end
+
+    def free_rate
+      @free_ms * 100.0 / 1000.0
+    end
+
+    def cpu_rate
+      @cpu_ms * 100.0 / 1000.0
+    end
+  end
+end
diff --git a/lib/stylet/delegators.rb b/lib/stylet/delegators.rb
index 6d5a3d2..57dc450 100644
--- a/lib/stylet/delegators.rb
+++ b/lib/stylet/delegators.rb
@@ -28,7 +28,8 @@ module Delegators
             :screen,
             :font_width,

-            :check_fps,
+            :fps_stat,
+            :cpu_stat,

             :vec2,
             :rect2,
diff --git a/lib/stylet/draw.rb b/lib/stylet/draw.rb
index 112279f..6e1144d 100644
--- a/lib/stylet/draw.rb
+++ b/lib/stylet/draw.rb
@@ -5,14 +5,16 @@

 module Stylet
   module Draw
-    attr_reader :count, :check_fps, :sdl_event, :rect, :screen, :screen_active
+    attr_reader :count, :sdl_event, :rect, :screen, :screen_active
+    attr_reader :fps_stat, :cpu_stat
     attr_accessor :title

     def run_initializers
       super
       init_on(:draw) do
         @count = 0
-        @check_fps = CheckFPS.new
+        @fps_stat = FpsStat.new
+        @cpu_stat = CpuStat.new
         @screen_active = true

         screen_open
@@ -37,11 +39,12 @@ def run_initializers
       end
     end

-    # ハードウェアがダブルバッファ対応の場合、flipで自動的にVSYNCを待って切り替えるため
-    # ハードウェアのフレーム数(60FPS)以上にはならないことに注意
+    # ハードウェアがダブルバッファ対応の場合flipで自動的にVSYNCを待って切り替えるため
+    # ハードウェアのフレーム数(60FPS)以上にはならない
     def after_draw
       super
-      @screen.flip
+      @fps_stat.update
+      @cpu_stat.benchmark { @screen.flip }
       @count += 1
     end

@@ -135,19 +138,28 @@ def save_bmp(fname)
       @screen.save_bmp(fname)
     end

-    def system_line
+    def system_infos
+      list = [
+        @count,
+        "FPS:#{@fps_stat.fps}",
+        "CPU:#{format("%4.2f", @cpu_stat.cpu_rate)}",
+      ]
       if Stylet.production
-        "#{@count} #{@check_fps.fps}"
       else
-        "#{@count} #{@check_fps.fps} FPS SE:#{SDL::Mixer.playing_channels}/#{SE.allocated_channels} M#{SDL::Mixer.play_music? ? 1 : 0} #{@rect.w}x#{@rect.h} #{app_state}"
+        list += [
+          "SE:#{SDL::Mixer.playing_channels}/#{SE.allocated_channels}",
+          "M#{SDL::Mixer.play_music? ? 1 : 0}",
+          "#{@rect.w}x#{@rect.h}",
+          app_state,
+        ]
       end
+      list
     end

     def before_update
       super
       # return if Stylet.production
-      @check_fps.update
-      vputs(system_line)
+      vputs system_infos.compact.join(" ")
     end

     def full_screen_toggle
diff --git a/lib/stylet/fps_stat.rb b/lib/stylet/fps_stat.rb
new file mode 100644
index 0000000..4874ce1
--- /dev/null
+++ b/lib/stylet/fps_stat.rb
@@ -0,0 +1,47 @@
+# -*- coding: utf-8 -*-
+module Stylet
+  #
+  # FPSの計測
+  #
+  # Example:
+  #
+  #   obj = FpsStat.new
+  #   loop do
+  #     obj.update
+  #     obj.fps # => 60
+  #     screen.flip
+  #   end
+  #
+  class FpsStat
+    attr_reader :fps
+
+    # blockにはミリ秒単位で現在の時間を返すブロックを指定する
+    def initialize(&block)
+      @block = block || -> { Time.now.to_f * 1000.0 }
+      @old_time = @block.call
+      @fps = 0
+      @counter = 0
+    end
+
+    # 毎フレーム呼ぶことでフレーム数を調べられる
+    def update
+      @counter += 1
+      v = @block.call
+      t = v - @old_time
+      if t > 1000.0
+        @old_time = v
+        @fps = @counter
+        @counter = 0
+      end
+    end
+  end
+end
+
+if $0 == __FILE__
+  obj = Stylet::FpsStat.new
+  sleep(0.5)
+  obj.update
+  sleep(0.5)
+  obj.update
+  p obj.fps
+end
diff --git a/spec/check_fps_spec.rb b/spec/check_fps_spec.rb
deleted file mode 100644
index 15642ff..0000000
--- a/spec/check_fps_spec.rb
+++ /dev/null
@@ -1,15 +0,0 @@
-# -*- coding: utf-8 -*-
-require "spec_helper"
-
-module Stylet
-  describe CheckFPS do
-    it "0.5秒ずつフレームが切り替わっているということは2フレーム" do
-      obj = CheckFPS.new
-      sleep(0.5)
-      obj.update
-      sleep(0.5)
-      obj.update
-      obj.fps.should == 2
-    end
-  end
-end
diff --git a/spec/fps_stat_spec.rb b/spec/fps_stat_spec.rb
new file mode 100644
index 0000000..89dc930
--- /dev/null
+++ b/spec/fps_stat_spec.rb
@@ -0,0 +1,15 @@
+# -*- coding: utf-8 -*-
+require "spec_helper"
+
+module Stylet
+  describe FpsStat do
+    it "0.5秒ずつフレームが切り替わっているということは2フレーム" do
+      obj = FpsStat.new
+      sleep(0.5)
+      obj.update
+      sleep(0.5)
+      obj.update
+      obj.fps.should == 2
+    end
+  end
+end
FpsAdjust refactoring