1
00:00:05,980 --> 00:00:09,100
Hey everybody has gone this is Caleb with Deb's Lopes dot com.

2
00:00:09,110 --> 00:00:13,670
And in this video we're going to continue our talk about unit testing except we're going to talk about

3
00:00:13,670 --> 00:00:20,480
something called performance testing now you know as a developer that your apps performance is almost

4
00:00:20,480 --> 00:00:26,150
like the number one most important thing if your app does not perform well if it's slow and buggy and

5
00:00:26,630 --> 00:00:28,520
it just it's a terrible experience.

6
00:00:28,520 --> 00:00:35,690
So performance testing basically forces your app to perform at a certain level and if it is performing

7
00:00:35,720 --> 00:00:41,660
slower than usual the test will fail which is very cool so maybe you have code that's worked but you

8
00:00:41,660 --> 00:00:45,380
do work to you know maybe refactor it and it ends up being slower.

9
00:00:45,410 --> 00:00:51,500
You know maybe it's worse than before you started and performance tests basically make sure that your

10
00:00:51,500 --> 00:00:55,220
code is working as efficiently or more efficiently than before.

11
00:00:55,220 --> 00:00:58,270
So let's pull up on our project here and let's write one.

12
00:00:58,460 --> 00:01:02,960
Now what I'm going to do is I'm going to go into our download client which doesn't really do anything

13
00:01:02,960 --> 00:01:07,820
it doesn't really serve a purpose in this app except for showing how to use that mock you or all session

14
00:01:08,270 --> 00:01:13,950
but just pretending that we're using some type of client model for handling downloading.

15
00:01:14,060 --> 00:01:18,460
I'm going to write a very bad function K that you would never actually write.

16
00:01:18,470 --> 00:01:21,760
So I'm going to call it phunk terrible function.

17
00:01:21,770 --> 00:01:27,490
You would never write literally you would never write this.

18
00:01:27,520 --> 00:01:28,480
It's a weird function.

19
00:01:28,490 --> 00:01:34,300
I don't really like how it works but let's just say let's total value equals 1.

20
00:01:34,370 --> 00:01:38,990
And then maybe 1 2 3 4 5 6 7 1 billion.

21
00:01:38,990 --> 00:01:44,920
So that's going to be our total value and then our current value is going to be equal to zero.

22
00:01:44,990 --> 00:01:52,760
For i in 1 up until total value current value equals I.

23
00:01:52,790 --> 00:02:01,070
So basically what this function is going to do is it's going to you know set our current value to be

24
00:02:01,150 --> 00:02:01,970
0 up sorry.

25
00:02:03,290 --> 00:02:04,280
There we go.

26
00:02:05,290 --> 00:02:07,090
There should be three dots.

27
00:02:07,390 --> 00:02:11,100
What it's going to do is it's basically going to cycle through and every single time it's going to go

28
00:02:11,110 --> 00:02:13,950
you know one two three four times all the way up to 1 billion.

29
00:02:14,110 --> 00:02:16,960
And that's going to set the value of current value to be equal to that.

30
00:02:16,960 --> 00:02:24,380
Now I'm using the for loop just because it shows a very simple you know example of inefficient code.

31
00:02:24,400 --> 00:02:26,740
This for loop takes forever.

32
00:02:27,340 --> 00:02:30,030
And so we're going to work on making it more efficient.

33
00:02:30,030 --> 00:02:34,010
But we now have a terrible function Let's go read a test to test its performance.

34
00:02:34,090 --> 00:02:40,540
So go into download client tests and what we're going to do is we're going to go ahead and basically

35
00:02:42,100 --> 00:02:46,840
go down here and we're going to type phunk test.

36
00:02:46,960 --> 00:02:50,950
Terrible function performance.

37
00:02:51,860 --> 00:02:52,490
OK.

38
00:02:52,810 --> 00:02:58,570
And basically our system under test is a download client we already have one so we can type system under

39
00:02:58,570 --> 00:03:00,990
test and we can call our function here.

40
00:03:01,000 --> 00:03:01,910
Terrible function.

41
00:03:01,930 --> 00:03:03,650
You would never write.

42
00:03:04,390 --> 00:03:09,730
And that's going to run and of course the tests will pass because we're not actually testing anything.

43
00:03:09,730 --> 00:03:12,930
But if we want to test performance here's what we've got to do.

44
00:03:13,000 --> 00:03:20,980
We have to call a particular block of code that is a part of the test called measure K and measure what

45
00:03:20,980 --> 00:03:21,620
it's going to do.

46
00:03:21,640 --> 00:03:24,860
Like you saw it measures the performance of a block of code.

47
00:03:24,880 --> 00:03:30,190
Call this method from within a test method to measure the performance of a block of code by default.

48
00:03:30,400 --> 00:03:34,780
This method measures the number of seconds the block of code takes to execute.

49
00:03:34,810 --> 00:03:36,420
So you can override that.

50
00:03:36,430 --> 00:03:40,930
You can set up all kinds of custom parameters but basically what it's going to do is it's going to run

51
00:03:40,930 --> 00:03:47,320
your code 10 times and then take note of how long it takes each time to run and it's going to vary you

52
00:03:47,320 --> 00:03:52,480
know by a little bit each time then it's going to average them and the average result is what will be

53
00:03:52,480 --> 00:03:53,350
returned.

54
00:03:53,350 --> 00:03:55,740
But there's a couple of more steps that we need to do after that.

55
00:03:55,740 --> 00:04:01,030
So let's go ahead and select this cut it and then paste it into the measured block and we're going to

56
00:04:01,030 --> 00:04:06,660
go ahead and just run this test and I'm going to show you how it works so click run on little diamond

57
00:04:06,670 --> 00:04:11,660
there it's going to build our project and run our test and then we're going to get a result.

58
00:04:11,730 --> 00:04:15,670
Kainat it looks a bit different than most other tests you've seen.

59
00:04:15,820 --> 00:04:18,320
So we are launching our test here.

60
00:04:18,360 --> 00:04:23,690
All it all looks good and it's starting OK.

61
00:04:23,740 --> 00:04:24,500
So check it out.

62
00:04:24,620 --> 00:04:29,500
It's done it's succeeded but it's telling us no baseline average for time.

63
00:04:29,530 --> 00:04:36,680
But if I click on that you'll see that it took point 0 6 1 seconds on average for this function to run.

64
00:04:36,820 --> 00:04:37,380
OK.

65
00:04:37,600 --> 00:04:40,260
Now that's not great but that's how long it took.

66
00:04:40,270 --> 00:04:46,120
Now it's telling us we have no baseline average and that means that we don't have a baseline to compare

67
00:04:46,120 --> 00:04:46,860
this to.

68
00:04:46,930 --> 00:04:53,380
How fast should this run let's say that we have a standard that all functions need to run around.

69
00:04:53,380 --> 00:04:55,070
Point zero five seconds.

70
00:04:55,090 --> 00:05:01,860
So what we're going to do to set that baseline is we're going to go over here into the report navigator.

71
00:05:02,110 --> 00:05:07,330
We're going to click on our latest test and then right here in the log or we can go ahead and click

72
00:05:07,330 --> 00:05:10,430
on the time that our performance test took.

73
00:05:10,560 --> 00:05:14,830
And you'll see there's a set baselines button here as well but if you just click on the time we can

74
00:05:14,830 --> 00:05:19,210
go ahead and set a baseline and of course says no baseline because we have not set one.

75
00:05:19,210 --> 00:05:24,940
So if we click Set baseline it's going to take the average of our most recent test and set that to be

76
00:05:24,940 --> 00:05:25,490
the baseline.

77
00:05:25,510 --> 00:05:30,160
But of course if you want you can click the edit button here and you can change it to whatever you want.

78
00:05:30,160 --> 00:05:36,670
So I'm going to do point 0 5 and then you can set a maximum standard deviation and that basically means

79
00:05:37,840 --> 00:05:44,590
your values K can go above point 0 5 by 10 percent or below by 10 percent.

80
00:05:44,590 --> 00:05:51,040
I'm going to change that to 5 percent so we're a little less tolerant of error or I guess poor performance

81
00:05:51,520 --> 00:05:53,040
and click safe.

82
00:05:53,080 --> 00:05:59,250
So now we have a baseline of point zero five seconds we have a maximum standard deviation of 5 percent.

83
00:05:59,260 --> 00:06:07,240
So now if we go back into our test and we click run it's going to use that baseline basically as a way

84
00:06:07,240 --> 00:06:12,930
to determine whether or not our test is faster or slower so check it out.

85
00:06:13,000 --> 00:06:16,530
Our average time for that test was point zero five nine seconds.

86
00:06:16,570 --> 00:06:19,950
That means that it is 17 percent worse than our baseline.

87
00:06:20,080 --> 00:06:20,350
OK.

88
00:06:20,350 --> 00:06:23,220
Now I thought we said 5 percent maximum standard deviation.

89
00:06:23,440 --> 00:06:29,110
Yes but if you go back into your test here you click on the time you'll see that seventeen point four

90
00:06:29,110 --> 00:06:32,170
percent worse is only 2 percent of a standard deviation.

91
00:06:32,170 --> 00:06:34,110
So it's not that big of a deal.

92
00:06:34,120 --> 00:06:37,980
Now if I were to go back in here how am I going to make this better.

93
00:06:37,990 --> 00:06:41,080
Let's actually do something that will make this test fail for sure.

94
00:06:41,080 --> 00:06:43,530
I'm going to go ahead and add another zero here.

95
00:06:43,690 --> 00:06:47,140
And that's actually going to increase the compute time by quite a bit.

96
00:06:47,140 --> 00:06:53,050
So now that we have a bigger value and a larger range to cycle through click run on this test then it

97
00:06:53,050 --> 00:06:55,550
will take longer for the test to actually operate.

98
00:06:55,660 --> 00:07:01,240
But we should see that it will come back significantly worse and the test will fail.

99
00:07:01,240 --> 00:07:09,490
So it's running it's running to do and 1000 66 percent worse that's very very bad.

100
00:07:09,490 --> 00:07:15,190
So this is how you could identify major performance issues maybe your code runs maybe your app works

101
00:07:15,400 --> 00:07:17,410
but it's just taking forever.

102
00:07:17,440 --> 00:07:21,820
So what you can do is you can put in these performance tests and they basically let you see hey we know

103
00:07:21,820 --> 00:07:25,590
something we just introduced made our app very very slow and very inefficient.

104
00:07:25,600 --> 00:07:28,030
And of course the test fails.

105
00:07:28,030 --> 00:07:32,480
Normally you won't introduce a bug that's 1000 percent worse than what you had before.

106
00:07:32,590 --> 00:07:34,540
But anyway.

107
00:07:34,540 --> 00:07:37,930
So let's go ahead and let's make this maybe not a not a trillion.

108
00:07:37,930 --> 00:07:39,810
We'll go back to a billion here that is a billion.

109
00:07:39,810 --> 00:07:40,330
Right.

110
00:07:40,660 --> 00:07:41,640
One two three four.

111
00:07:41,680 --> 00:07:42,490
Oh no it's 10 million.

112
00:07:42,490 --> 00:07:44,070
Whoopsies.

113
00:07:44,340 --> 00:07:44,610
OK.

114
00:07:44,620 --> 00:07:48,110
So we have 10 million let's make it $18 million.

115
00:07:48,280 --> 00:07:54,310
OK let's just say that our code that we wrote was a little bit worse but not terrible.

116
00:07:54,310 --> 00:07:56,110
So we're going to go ahead and run our test.

117
00:07:56,110 --> 00:07:58,440
It should take much less time than before.

118
00:07:58,570 --> 00:08:03,340
And we should see that it is a little bit worse than our baseline but passable.

119
00:08:03,550 --> 00:08:04,870
So it's going to test.

120
00:08:05,060 --> 00:08:07,000
OK so a 107 percent worse.

121
00:08:07,000 --> 00:08:08,000
I was mistaken.

122
00:08:08,260 --> 00:08:10,140
So that obviously is much less efficient.

123
00:08:10,150 --> 00:08:13,380
Let's go down to maybe one or $12 million.

124
00:08:13,600 --> 00:08:14,730
Okay here we go.

125
00:08:14,740 --> 00:08:20,610
Going to run all of our test again and we'll see how we did our tests are running.

126
00:08:20,610 --> 00:08:23,370
Here we go 40 percent worse so check it out.

127
00:08:23,370 --> 00:08:25,030
It's going to run.

128
00:08:25,030 --> 00:08:26,170
Here we go.

129
00:08:27,730 --> 00:08:31,620
Testing and test complete all tests pass 40 percent worse.

130
00:08:31,730 --> 00:08:33,250
Okay.

131
00:08:33,550 --> 00:08:38,320
When you're dealing with standard deviation because that's not yet 5 percent as far as the standard

132
00:08:38,320 --> 00:08:39,470
deviation goes.

133
00:08:39,640 --> 00:08:45,430
Let's let's try making this even more efficient Let's take office 0 at the end make it super fast.

134
00:08:45,430 --> 00:08:49,810
This is also going to be useful to tell us hey the code you just wrote is way more efficient than what

135
00:08:49,810 --> 00:08:51,520
you had before it's way faster.

136
00:08:51,520 --> 00:08:53,100
That would be a good thing right.

137
00:08:53,110 --> 00:08:56,980
So it's testing and it comes back 88 percent better so hey.

138
00:08:57,070 --> 00:08:57,430
Wow.

139
00:08:57,440 --> 00:08:58,910
Like let's keep that going.

140
00:08:58,930 --> 00:09:01,100
That's obviously a better test.

141
00:09:01,120 --> 00:09:08,040
And if we go into the log here when it's all done you can check your performance test here.

142
00:09:08,050 --> 00:09:12,700
You can click on it and it's going to say hey that's amazing that's 87 percent better than the code

143
00:09:12,700 --> 00:09:13,870
you wrote before.

144
00:09:13,870 --> 00:09:18,430
So that could be your new standard and you could set the baseline that way knowing hey I had it at one

145
00:09:18,430 --> 00:09:20,590
point where it was way way faster.

146
00:09:20,620 --> 00:09:23,690
This is basically how performance testing works in a nutshell.

147
00:09:23,710 --> 00:09:28,240
You pass in the code you want to measure and then depending on the speed that comes back you can set

148
00:09:28,240 --> 00:09:34,300
a baseline and then from then on you can either know hey this works as it was as efficient or you can

149
00:09:34,300 --> 00:09:35,530
work on making it better.

150
00:09:35,560 --> 00:09:37,620
Obviously not making it worse.

151
00:09:37,810 --> 00:09:42,070
So this is how performance testing works in unit testing in swift and Iowa.

152
00:09:42,100 --> 00:09:45,430
Let's head over to the next video where we're going to continue talking about unit testing.
