Hey everybody has gone this is Caleb with Deb's Lopes dot com.
And in this video we're going to continue our talk about unit testing except we're going to talk about
something called performance testing now you know as a developer that your apps performance is almost
like the number one most important thing if your app does not perform well if it's slow and buggy and
it just it's a terrible experience.
So performance testing basically forces your app to perform at a certain level and if it is performing
slower than usual the test will fail which is very cool so maybe you have code that's worked but you
do work to you know maybe refactor it and it ends up being slower.
You know maybe it's worse than before you started and performance tests basically make sure that your
code is working as efficiently or more efficiently than before.
So let's pull up on our project here and let's write one.
Now what I'm going to do is I'm going to go into our download client which doesn't really do anything
it doesn't really serve a purpose in this app except for showing how to use that mock you or all session
but just pretending that we're using some type of client model for handling downloading.
I'm going to write a very bad function K that you would never actually write.
So I'm going to call it phunk terrible function.
You would never write literally you would never write this.
It's a weird function.
I don't really like how it works but let's just say let's total value equals 1.
And then maybe 1 2 3 4 5 6 7 1 billion.
So that's going to be our total value and then our current value is going to be equal to zero.
For i in 1 up until total value current value equals I.
So basically what this function is going to do is it's going to you know set our current value to be
0 up sorry.
There we go.
There should be three dots.
What it's going to do is it's basically going to cycle through and every single time it's going to go
you know one two three four times all the way up to 1 billion.
And that's going to set the value of current value to be equal to that.
Now I'm using the for loop just because it shows a very simple you know example of inefficient code.
This for loop takes forever.
And so we're going to work on making it more efficient.
But we now have a terrible function Let's go read a test to test its performance.
So go into download client tests and what we're going to do is we're going to go ahead and basically
go down here and we're going to type phunk test.
Terrible function performance.
OK.
And basically our system under test is a download client we already have one so we can type system under
test and we can call our function here.
Terrible function.
You would never write.
And that's going to run and of course the tests will pass because we're not actually testing anything.
But if we want to test performance here's what we've got to do.
We have to call a particular block of code that is a part of the test called measure K and measure what
it's going to do.
Like you saw it measures the performance of a block of code.
Call this method from within a test method to measure the performance of a block of code by default.
This method measures the number of seconds the block of code takes to execute.
So you can override that.
You can set up all kinds of custom parameters but basically what it's going to do is it's going to run
your code 10 times and then take note of how long it takes each time to run and it's going to vary you
know by a little bit each time then it's going to average them and the average result is what will be
returned.
But there's a couple of more steps that we need to do after that.
So let's go ahead and select this cut it and then paste it into the measured block and we're going to
go ahead and just run this test and I'm going to show you how it works so click run on little diamond
there it's going to build our project and run our test and then we're going to get a result.
Kainat it looks a bit different than most other tests you've seen.
So we are launching our test here.
All it all looks good and it's starting OK.
So check it out.
It's done it's succeeded but it's telling us no baseline average for time.
But if I click on that you'll see that it took point 0 6 1 seconds on average for this function to run.
OK.
Now that's not great but that's how long it took.
Now it's telling us we have no baseline average and that means that we don't have a baseline to compare
this to.
How fast should this run let's say that we have a standard that all functions need to run around.
Point zero five seconds.
So what we're going to do to set that baseline is we're going to go over here into the report navigator.
We're going to click on our latest test and then right here in the log or we can go ahead and click
on the time that our performance test took.
And you'll see there's a set baselines button here as well but if you just click on the time we can
go ahead and set a baseline and of course says no baseline because we have not set one.
So if we click Set baseline it's going to take the average of our most recent test and set that to be
the baseline.
But of course if you want you can click the edit button here and you can change it to whatever you want.
So I'm going to do point 0 5 and then you can set a maximum standard deviation and that basically means
your values K can go above point 0 5 by 10 percent or below by 10 percent.
I'm going to change that to 5 percent so we're a little less tolerant of error or I guess poor performance
and click safe.
So now we have a baseline of point zero five seconds we have a maximum standard deviation of 5 percent.
So now if we go back into our test and we click run it's going to use that baseline basically as a way
to determine whether or not our test is faster or slower so check it out.
Our average time for that test was point zero five nine seconds.
That means that it is 17 percent worse than our baseline.
OK.
Now I thought we said 5 percent maximum standard deviation.
Yes but if you go back into your test here you click on the time you'll see that seventeen point four
percent worse is only 2 percent of a standard deviation.
So it's not that big of a deal.
Now if I were to go back in here how am I going to make this better.
Let's actually do something that will make this test fail for sure.
I'm going to go ahead and add another zero here.
And that's actually going to increase the compute time by quite a bit.
So now that we have a bigger value and a larger range to cycle through click run on this test then it
will take longer for the test to actually operate.
But we should see that it will come back significantly worse and the test will fail.
So it's running it's running to do and 1000 66 percent worse that's very very bad.
So this is how you could identify major performance issues maybe your code runs maybe your app works
but it's just taking forever.
So what you can do is you can put in these performance tests and they basically let you see hey we know
something we just introduced made our app very very slow and very inefficient.
And of course the test fails.
Normally you won't introduce a bug that's 1000 percent worse than what you had before.
But anyway.
So let's go ahead and let's make this maybe not a not a trillion.
We'll go back to a billion here that is a billion.
Right.
One two three four.
Oh no it's 10 million.
Whoopsies.
OK.
So we have 10 million let's make it $18 million.
OK let's just say that our code that we wrote was a little bit worse but not terrible.
So we're going to go ahead and run our test.
It should take much less time than before.
And we should see that it is a little bit worse than our baseline but passable.
So it's going to test.
OK so a 107 percent worse.
I was mistaken.
So that obviously is much less efficient.
Let's go down to maybe one or $12 million.
Okay here we go.
Going to run all of our test again and we'll see how we did our tests are running.
Here we go 40 percent worse so check it out.
It's going to run.
Here we go.
Testing and test complete all tests pass 40 percent worse.
Okay.
When you're dealing with standard deviation because that's not yet 5 percent as far as the standard
deviation goes.
Let's let's try making this even more efficient Let's take office 0 at the end make it super fast.
This is also going to be useful to tell us hey the code you just wrote is way more efficient than what
you had before it's way faster.
That would be a good thing right.
So it's testing and it comes back 88 percent better so hey.
Wow.
Like let's keep that going.
That's obviously a better test.
And if we go into the log here when it's all done you can check your performance test here.
You can click on it and it's going to say hey that's amazing that's 87 percent better than the code
you wrote before.
So that could be your new standard and you could set the baseline that way knowing hey I had it at one
point where it was way way faster.
This is basically how performance testing works in a nutshell.
You pass in the code you want to measure and then depending on the speed that comes back you can set
a baseline and then from then on you can either know hey this works as it was as efficient or you can
work on making it better.
Obviously not making it worse.
So this is how performance testing works in unit testing in swift and Iowa.
Let's head over to the next video where we're going to continue talking about unit testing.