The Porter scene is not primarily intended for comic relief. Shakespeare wanted to force Macbeth to come out of hiding and be present when Macduff discovers the King's dead body. Macbeth was planning to pretend to have been sound asleep with his wife when the alarm is rung. The prolonged knocking is what forces Macbeth to come down in his nightgown to see why no one is responding. The Porter is obviously drunk. This explains why he takes so long to open the gate for Macduff. But then the Porter has to explain why everybody is drunk. They were all carousing "till the second cock." Then Shakespeare has to explain how the household staff got so much liquor, so he inserts a line earlier that informs the audience that the King "Sent forth great largess to your offices" (2.1).
The Porter would be played by a talented comedian who would keep the audience laughing uproariously. This diversion would distract them and keep them from wondering about the many unanswered questions, such as:
Why were Macduff and Lennox forced to spend the night in some hovel outside the castle? Macduff is a very important thane.
Why didn't Duncan appoint someone like Banquo to wake him in the morning, someone he knew would be accommodated inside the castle? For that matter, why didn't Duncan simply ask Lady Macbeth to see that he was awakened?
Why is Macduff so cordial with the Porter when he is finally admitted? (The answer is probably that Macduff is extremely worried by the long delay in being admitted and wants to question the Porter rather than jumping down his throat and frightening him out of his wits. Macduff, standing outside in the rain, wind, and cold, can't help feeling there is something bad happening inside. He might actually be supposing that everybody in the castle has been murdered.)
Why did Macbeth and Lady Macbeth allow their entire household staff and presumably their household guards to get so drunk when they are hosting the King himself and so many important noblemen?
The three things achieved by the introductory lines of the Porter scene are:
They explain the prolonged knocking, which is their most important function.
The provide comic relief.
The comic relief makes the audience overlook obvious inconsistencies, such as "Why didn't some of the other guests put in an appearance?" and "How could everybody in that huge castle be drunk?" and "Why would the Macbeths permit such debauchery in wartime?"
They also allow the audience to see Macduff for the first time.
The Porter scene ends when Macduff sees Macbeth approaching and says, "Our knocking has awaked him. Here he comes." This is the whole point and purpose of the knocking and the Porter scene.
It has been suggested that Shakespeare did not write the Porter scene himself. This is quite probable, but he certainly wanted such a scene inserted.