Log Energy Instruments

As far as life is concerned, the power of the mobile phone is an important lifeline for us. Generally speaking, when the electricity is less than 20%, our hearts are always so tight. As a developer, we should be saving the user’s mobile phone, allowing users a limited battery can be used longer, we developed APP, for users, developers of our scheme is the best of both worlds. Therefore, the APP power consumption should also be the point of performance optimization.


Or raywenderlich Catstagram APP as an example of the analysis. The case is a list with pictures.

Log Energy Instruments
case screenshot

value of the note is in my development environment Energy need to run on a real machine equipment, the development environment is Xcode 8.3.2, iPhone 6 (10.3.1).

Using Energy Impact

Energy Impact is a tool that comes with Xcode to view device cost profiles.

Log Energy Instruments,
, Energy, Impact diagrams

as shown above, click the Energy Impact column on the left of the Xcode to see the level of power consumption on the device running APP.

Log Energy Instruments,
, I indicators

CPU, Location, GPU and Network on the left, and the Background five index, the 5 index is the major energy consumers, in the table on the right if it was gray filled, then it means that at that moment, the index is active. For example, as shown in the diagram, CPU and Network have always been filled with gray, which means that CPU and Network have been active. The top has blue and red column charts, and red is the Overhead indicator, indicating that in addition to this APP, other power consumption of the system, blue is Cost, which means that the amount of electricity consumed by the APP. For more information about Energy Impact, you can refer to Apple’s official document, https://developer.apple.com/library/content/documentation/Performance/Conceptual/EnergyGuide-iOS/MonitorEnergyWithXcode.html. This place is no longer burdensome.

Log Energy Instruments
high energy consumption

APP operation for a period of time, after moving several times list, APP energy consumption becomes very high, can be seen from the figure, APP in the static state of high power consumption is certainly not normal, Energy Impact can only be seen if there is a problem, but can not point out where there may be problems. So this time to wielding the Instruments weapon.

Energy of Instruments

Command + I runs Instruments and selects the Energy Log template.

Log Energy Instruments
selects the Energy Log template
Log Energy Instruments,
, Energy, Log indicators

look at the left, Energy, Log’s indicators are Energy, CPU, Network, and so on.

Click the start button to record APP running

Log Energy Instruments
APP operation

It can be seen from the figure of the APP consumption of energy, but there is a problem, the problem is that we already know the APP consumption of energy, but how to know where to modify the code? At this point, we need the Time Profiler tool.

Log Energy Instruments
add Time Profiler tool

As shown in the figure above, we have added the Time Profiler tool to record the code running of APP over a certain period of time.

After everything is available, we re start recording the operation of the APP.

Operation of Log Energy Instruments
Log Energy Instruments
Timer Profiler

Energy Log combines the use of Timer Profiler to avoid interference with hiding our system library contents and displaying our code calls.

Log Energy Instruments

finds the CatPhotoTableViewCell (with, yRotation:, CGFloat) method of the panImage in terms of the weight ratio of the code execution time. By tracing the code, we found the viewDidLoad () method of the CatFeedViewController.swift file and found the place where panImage (with, yRotation:, CGFloat) methods were frequently invoked

MotionManager.startDeviceMotionUpdates (to:.Main, withHandler:{deviceMotion, error in guard let deviceMotion deviceMotion else {return} = self.lastY = deviceMotion.rotationRate.y let xRotationRate = CGFloat (deviceMotion.rotationRate.x) let yRotationRate = CGFloat (deviceMotion.rotationRate.y) let zRotationRate CGFloat (deviceMotion.rotationRate.z) = print (Y / and / (yRotationRate) x (xRotationRate) and z/ (zRotationRate) ") if ABS (yRotationRate) > (ABS (xRotationRate) + ABS (zRotationRate)) {for cell in self.tableView.visibleCells as! [CatPhotoTableViewCell] (with: yRotationRate) {cell.panImage} }

The key of this code is self.lastY = deviceMotion.rotationRate.y this statement, no matter how much deviceMotion.rotationRate.y changes are behind the implementation of the code should be normal when the variation range of deviceMotion.rotationRate.y more than the number just behind the implementation of the code, so the optimization is as follows

MotionManager.startDeviceMotionUpdates (to:.Main, withHandler:{deviceMotion, error in guard let deviceMotion = deviceMotion else guard {return} ABS (self.lastY - deviceMotion.rotationRate.y) > 0.1 else {return} let xRotationRate = CGFloat (deviceMotion.rotationRate.x) let yRotationRate = CGFloat (deviceMotion.rotationRate.y) let zRotationRate CGFloat (deviceMotion.rotationRate.z) = print (Y / (yRotationRate) and X / (xRotationRate) and z/ (zRotationRate)) "if ABS (yRotationRate) > (ABS (xRotationRate) + ABS (zRotationRate)) {for cell in self.tableView.visibleCells as [CatPhotoTableViewCell] cell.panImage with: yRo ({! TationRate)}

The logic for modifying self.lastY = deviceMotion.rotationRate.y is guard ABS (self.lastY – deviceMotion.rotationRate.y) > 0.1 else {return}
. When the deviceMotion.rotationRate.y changes over 0.1, the following code logic is executed. Modify the code after verification, modify the effect.

Log Energy Instruments
validation changes

After using Energy Impact for validation, it was found that energy consumption was still very high and could not be lowered. Then go on using Instruments to find the reason.

Log Energy Instruments
uses Time Profiler

It also takes a lot of CPU time to find sendLogs’s CatFeedViewController (), and then use Xcode to view the code. Through code tracing, the init () method of CatFeedViewController is found.

Init (super.init) {(nibName: nil, bundle: Nil) navigationItem.title = "Catstagram" tableView.autoresizingMask = UIViewAutoresizing.flexibleWidth; tableView.delegate = self tableView.dataSource = self let timer = Timer (timeInterval: 1, target: self, selector: #selector (CatFeedViewController.sendLogs), userInfo: nil, repeats: true RunLoop.main.add (timer) forMode:,.CommonModes)}

An amazing code was found in this init () method, with a timer initiating a sendlog network request every 1 s. Don’t doubt it. It must be the pit daddy’s code that consumes a lot of electricity. The normal send log operation should be to send the last log at the time the APP startup completes, or send a log in the APP’s callback method to the applicationWillResignActive. Our modification scheme is to send log at the APP callback method into the applicationWillResignActive. Open the AppDelegate.swift file, add the following code, and delete the sendlog timer in the init () method of the CatFeedViewController.

Func applicationWillResignActive (_ application: UIApplication) {(rootVC.sendLogs)}

The next step is to validate the modification, using the Energy Impact tool to verify that the Energy Impact tool meets the requirements for validating the energy consumption profile of APP.

Log Energy Instruments
low energy consumption

now the energy consumption of APP is low, and the Network bin is not always active.

Log Energy Instruments
temporary high energy consumption

Move the APP back to the background, and then into the foreground, triggering APP’s sendlog operation. At this point, APP’s energy consumption is high, but then declines to lower levels of energy consumption. This is the normal performance of APP.


In APP performance tuning, energy optimization determines how long your user can use your APP in the same amount of power consumption. The general steps to optimize the energy consumption following
1, using Energy Impact APP
, 2 of energy consumption to see if there is high energy consumption, the use of the Instruments Energy Log template detailed verification, and with the operation details of the Time Profiler template capture code.
3, according to the code running details, determine the potential problem points, and then modify the code
4, verify the modification effect, if it is invalid, then repeat the 2-4 steps

Reference resources

This is of course raywenderlich Practical Instruments course content reference notes,
1, Demo 2, Energy
download https://files.betamax.raywenderlich.com/attachments/videos/789/9560e62e-96d3-47e5-b604-5d20c72bf9ee.zip Log address
3, the course of Energy
https://developer.apple.com/library/content/documentation/Performance/Conceptual/EnergyGuide-iOS/ MonitorEnergyWithXcode.html Impact official documents