#-- # Copyright (c) 2005, Michael Schuerig, michael@schuerig.de # # == License # This library is free software; you can redistribute it and/or # modify it under the terms of the GNU Lesser General Public # License as published by the Free Software Foundation; either # version 2.1 of the License, or (at your option) any later version. # See http://www.gnu.org/copyleft/lesser.html #++ require 'csv' require 'yaml' require 'boilerplate/tools/stopwatch' # Just drop this controller into app/controllers. # Then you can access it as http://127.0.0.1/stopwatch/ # # Supports snapshotting and export as CSV and YAML. # class StopwatchController < ActionController::Base NUMBER_FORMAT = '%10.3f' def index template =<<-END Stopwatches

Stopwatches

<%= link_to '[Reset all]', :action => :reset_all %> <%= link_to '[CSV]', :action => :export_csv %> <%= link_to '[YAML]', :action => :export_yaml %> [Take a Snapshot] <% Stopwatch.each do |sw| -%> <% end -%>
Stopwatch execs time/exec [ms] total time [ms]
<%=h sw.name %> <%= sw.executions %> <%= Admin::StopwatchController::NUMBER_FORMAT % sw.millis_per_execution %> <%= Admin::StopwatchController::NUMBER_FORMAT % sw.total_millis %> <%= link_to '[Reset]', :action => :reset, :id => CGI::escape(sw.name) %>

Snapshots

<% Stopwatch.each_snapshot do |name, snap| -%> <% snap.each do |sw| -%> <% end -%>
<%=h name %> <%= link_to '[Delete]', :action => :delete_snapshot, :id => CGI::escape(name) %>
Stopwatch execs time/exec [ms] total time [ms]
<%=h sw.name %> <%= sw.executions %> <%= Admin::StopwatchController::NUMBER_FORMAT % sw.millis_per_execution %> <%= Admin::StopwatchController::NUMBER_FORMAT % sw.total_millis %>
<% end -%> END render :inline => template end def take_snapshot Stopwatch.take_snapshot(CGI::unescape(params[:id])) redirect_to :action => 'index' end def delete_snapshot Stopwatch.delete_snapshot(CGI::unescape(params[:id])) redirect_to :action => 'index' end def reset_all Stopwatch.reset_all redirect_to :action => 'index' end def reset Stopwatch.reset(CGI::unescape(params[:id])) redirect_to :action => 'index' end def export_yaml send_data data.to_yaml, :type => 'text/x-yaml', :filename => 'stopwatch.yaml' end def export_csv out = '' CSV::Writer.generate(out) do |csv| csv << [ 'snapshot', 'stopwatch', 'executions', 'total_time' ] data.each do |snapshot| snapshot['stopwatches'].each do |stopwatch| csv << [ snapshot['snapshot'], stopwatch['name'], stopwatch['executions'], stopwatch['total_time'] ] end end end send_data out, :type => 'text/comma-separated-values', :filename => 'stopwatch.csv' end protected def data data = [ Stopwatch.export ] Stopwatch.each_snapshot do |name, snap| data << Stopwatch.export(name, snap) end data end end