iOS 13 - Test Your App Performance and Memory Metrics
From developer.apple.com

iOS 13 - Test Your App Performance and Memory Metrics

Introduction

Performance tests are not that common, but they are different in a lot of terms from standard unit tests because even if they fail, it doesn’t necessarily mean we have bugs.

Sometimes, a small change in one of the metrics can point to a bigger problem that can be identified only by performance tests, and not by regular QA process or even by your users.

In XCode 11, Apple added several new metrics to the plate and built the tools for performance testing based on a new protocol - XCTMetric Protocol.

Meet The XCTMetric Protocol

So, now we have a combination - a new protocol called XCTMeric, with a set of classes that represent new metrics to profile your functions, and a built-in UI tools in XCode that is really helpful in setting the baseline and show you the current status of your code.So what metrics do we have, that conforms to this protocol?

XCTClockMetric - to measure the time taken for your code to run.

XCTCPUMetric - to measure the number of CPU cycles, number of instructions retired, and CPU time that your code consumed.

XCTStorageMetric - to measure utilization of storage for your code.

XCTMemoryMetric - to measure the physical memory that your code consumed.

All of the metrics are relevant both for UI Tests and Unit Tests.

How do I use them?

Well, that’s easy - you just call the measure() method, and pass the metrics you want to examine, and that’s it!

func testPerforamnce() {
let obj = ProcessingImageService()
let image = UIImage(named: "myImage")
measure(metrics: [XCTClockMetric(), // to measure time
                          XCTCPUMetric(), // to measure cpu cycles
                          XCTStorageMetric(), // to measure storage consuming
                          XCTMemoryMetric()]) { // to measeure RAM consuming
                            
            obj.processImage(image: image) // this is the heavy process
        }
    }


After your first run, you will see a message saying “No baseline average for <name of metric>”

Tapping on that will open a “Performance Result” window. 

Let’s see what we have on that window:

Metric popup menu: If you used several metrics, you can select other metrics from here instead of exit and enter again.

Result: This is important - this is the actual result of your test. It could be the expected result, or better or worse.

Average: This is the average result of all your test runs in this session. This can help decide the expected result you want to get.

Baseline: This is the expected result.

MAX_XTDDEV: Since performance result can’t always be the same, you can deviate from your baseline in order not to fail in the test.

At the bottom, you have an edit button, when you tap it, you can change the baseline and your MAX_XTDDEV.

Best Practices 

  1. You don’t have to run performance tests each time. But when you do, try to run them on a device, and the same device as always, especially when it related to clock time metrics.
  2. Use Instruments to profile and analyze your code. It’s a waste of time not using this essential tool.
  3. Don’t ignore failures of those tests, by just setting a new baseline. It’s very easy to do that because our code just works with no bugs. As I mentioned in the introduction, it can point on a serious issue that is cooking in your code.










Yotam Golomb

???? 9K+ Followers | Father, Leader, Builder | Empathic Leadership Enthusiast | R&D Leadership | Agile Mindset Implementation & Scrum Team Building

5 年

Great Article !

要查看或添加评论,请登录

Avi Tsadok的更多文章

  • Boost Your Loop Performance By 87%

    Boost Your Loop Performance By 87%

    Today’s devices are incredibly powerful, often leading us to overlook the importance of efficiency and optimizations…

    2 条评论
  • Explore the Dynamic Island — ActivityKit Tutorial

    Explore the Dynamic Island — ActivityKit Tutorial

    Apple wowed us with the iPhone 14 Pro presentation in a way no one expected. The team from Cupertino took a hardware…

    1 条评论
  • Where Is My?Logic?

    Where Is My?Logic?

    “Describe to me your app architecture”. “Oh, it’s easy.

    2 条评论
  • Swift Result Builders – A Giant Leap Forward to produce a beautiful code

    Swift Result Builders – A Giant Leap Forward to produce a beautiful code

    The most noticeable thing about SwiftUI is that it looks incredible. It takes a relatively big chunk of code and…

    6 条评论
  • Swift 5.7: Unwrapping Optionals Gets Improvement

    Swift 5.7: Unwrapping Optionals Gets Improvement

    Introduction Unwrapping optional variables in Swift is one of the most common development tasks iOS developers perform…

    4 条评论
  • Creating a Custom Core Data Store

    Creating a Custom Core Data Store

    This tutorial is a special one. Its primary goal is to unleash how Core Data works under the hood, not to solve you a…

  • How Apple Screwed Up with SwiftUI and Core Data

    How Apple Screwed Up with SwiftUI and Core Data

    Introduction A few months ago, I decided that my third iOS development book will focus on Core Data, which is Apple’s…

    2 条评论
  • My Personal Takes from Using Combine

    My Personal Takes from Using Combine

    Confession I have a confession I want to begin with — before adopting any new technology or design pattern, I need to…

  • Reorder Items in SwiftUI LazyVStack

    Reorder Items in SwiftUI LazyVStack

    Introduction Those of you who are familiar with UIKit iOS 11 Drag and Drop API probably know that it dramatically…

  • UIKit Animations Are Messy. Here's Why SwiftUI May Fix It.

    UIKit Animations Are Messy. Here's Why SwiftUI May Fix It.

    Not only do iOS animations feel and look great, but they are also very easy to create.So how come I have the nerve to…

社区洞察

其他会员也浏览了