Database Reference
In-Depth Information
This code fetches all sales for a given employee and then returns the count of the
returned array.
Fetching the full sale object just to see how many sales exist for a given employee
is probably wasteful. This might be another opportunity to boost performance!
Let's measure the problem before attempting to fix it.
Open
EmployeeDetailViewControllerTests.swift
and find
testCountSales
.
func
testCountSales() {
self
.
measureMetrics
([
XCTPerformanceMetric_WallClockTime
],
automaticallyStartMeasuring:
false
, forBlock: {
let
employee =
self
.
getEmployee
()
let
employeeDetails =
EmployeeDetailViewController
()
self
.
startMeasuring
()
let
sales =
employeeDetails.
salesCountForEmployee
(employee)
self
.
stopMeasuring
()
})
}
Like the previous example, this function is using
measureMetrics
to see how long a
single function takes to run. The test gets an employee from a convenience
method, creates an
EmployeeDetailViewController
, begins measuring and then calls
the method in question.
Run this test. From Xcode's menu bar, select
Product
and then
Test
, or press
⌘
U.
This will build the app and run the test.
Once the test has finished running, you'll see a time next to this test method, as
before.
The performance is not too bad there is room for improvement