Skip to content

Instantly share code, notes, and snippets.

@matsuda
Last active December 11, 2018 01:20
Show Gist options
  • Save matsuda/b7b62a341c6ef254e95f02ecf6a99c7c to your computer and use it in GitHub Desktop.
Save matsuda/b7b62a341c6ef254e95f02ecf6a99c7c to your computer and use it in GitHub Desktop.
A Fastlane's action for xcprofiler customized to display total time. https://github.com/giginet/xcprofiler
# -Xfrontend -debug-time-function-bodies -Xfrontend -warn-long-function-bodies=100
lane :profile_build_time do |options|
gym(
...
)
options.merge!(product_name: 'Sample', display_limit: 30)
xcprofiler(options)
output_path = Actions.lane_context[SharedValues::XCPROFILER_OUTPUT_PATH]
...
end
module Fastlane
module Actions
module SharedValues
XCPROFILER_REPORT = :XCPROFILER_REPORT
XCPROFILER_OUTPUT_PATH = :XCPROFILER_OUTPUT_PATH
end
class XcprofilerAction < Action
def self.run(params)
# fastlane will take care of reading in the parameter and fetching the environment variable:
product_name = params[:product_name]
UI.error('product_name is required') unless product_name
UI.message "Parameter product name: #{params[:product_name]}"
UI.message "Parameter order: #{params[:order]}"
UI.message "Parameter output path: #{params[:output_path]}"
# Actions.lane_context[SharedValues::XCPROFILER_CUSTOM_VALUE] = "my_val"
dir = File.dirname(File.expand_path(__FILE__))
require File.join(dir, '..', 'xcprofiler_ext')
output_path = params[:output_path]
##############################
# http://secret-garden.hatenablog.com/entry/2015/05/26/203000
##############################
# 標準出力先を指定
$stdout = StringIO.new
profiler = Xcprofiler::Profiler.by_product_name(product_name)
options = { order: params[:order].to_sym }
options[:limit] = params[:limit] if params[:limit]
options[:display_limit] = params[:display_limit] if params[:display_limit]
profiler.reporters = [
Xcprofiler::StandardOutputReporter.new(options),
]
profiler.report!
# 出力された値を取得
results = $stdout.string
File.open(output_path, "w") do |f|
f.write(results)
end
Actions.lane_context[SharedValues::XCPROFILER_OUTPUT_PATH] = output_path
Actions.lane_context[SharedValues::XCPROFILER_REPORT] = results
ensure
# 出力先を戻す
$stdout = STDOUT
end
#####################################################
# @!group Documentation
#####################################################
def self.description
"xcprofiler"
end
def self.details
end
def self.available_options
# Define all options your action supports.
# Below a few examples
[
FastlaneCore::ConfigItem.new(key: :limit,
env_name: "FL_XCPROFILER_LIMIT",
description: "Limit for report",
type: Integer,
optional: true),
FastlaneCore::ConfigItem.new(key: :display_limit,
env_name: "FL_XCPROFILER_DISPLAY_LIMIT",
description: "Limit for display",
type: Integer,
default_value: 30),
FastlaneCore::ConfigItem.new(key: :order,
env_name: "FL_XCPROFILER_ORDER",
description: "Sort order (default,time,file)",
is_string: true,
default_value: :time),
FastlaneCore::ConfigItem.new(key: :product_name,
env_name: "FL_XCPROFILER_PRODUCT_NAME",
description: "product name",
is_string: true),
FastlaneCore::ConfigItem.new(key: :output_path,
env_name: "FL_XCPROFILER_OUTPUT_PATH",
description: "output path for results",
is_string: true,
default_value: 'profile.log')
]
end
def self.output
# Define the shared values you are going to provide
# Example
[
['XCPROFILER_REPORT', 'A reported text'],
['XCPROFILER_OUTPUT_PATH', 'A repoted file'],
]
end
def self.return_value
# If your method provides a return value, you can describe here what it does
end
def self.authors
"matsuda"
end
def self.is_supported?(platform)
# you can do things like
#
# true
#
# platform == :ios
#
# [:ios, :mac].include?(platform)
#
platform == :ios
end
end
end
end
require 'xcprofiler'
module ::Xcprofiler
class StandardOutputReporter < AbstractReporter
def table_for(executions)
time = 0
Terminal::Table.new do |t|
t << ['File', 'Line', 'Method name', 'Time(ms)']
t << :separator
i = 0
display_max = display_limit
executions.each do |execution|
i += 1
time += execution.time
if display_max && i <= display_max
t << [execution.filename, execution.line, truncate(execution.method_name, truncate_at), execution.time]
end
end
t << :separator
t << ["Total", "-", "-", time.truncate(3)]
end
end
private
def display_limit
options[:display_limit]
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment