It seems possible that Shakespeare wrote some of his monologues as separate poems when the thoughts occurred to him, then saved them to insert in his plays in convenient places. This could have been true of Hamlet's "To be or not to be," Jacques' "All the world's a stage," and others such as the one about sleep to which your question refers. Shakespeare valued sleep and wrote about it in a number of places. Here is his sonnet XXVII:
Weary with toil, I haste me to my bed,
The dear repose for limbs with travel tired;
But then begins a journey in my head,
To work my mind, when body's work's expired:
For then my thoughts, from far where I abide,
Intend a zealous pilgrimage to thee,
And keep my drooping eyelids open wide,
Looking on darkness which the blind do see
Save that my soul's imaginary sight
Presents thy shadow to my sightless view,
Which, like a jewel hung in ghastly night,
Makes black night beauteous and her old face new.
Lo! thus, by day my limbs, by night my mind,
For thee and for myself no quiet find.
Macbeth wanted to murder King Duncan in order to take his place--but the King had two sons, both of whom were also sleeping under Macbeth's roof on that fatal night. The older son, Malcolm, was the heir apparent. Macbeth should have killed both Malcolm and Donalbain that same night. Shakespeare knew this. But he hoped his audience would accept the way the two sons escaped, making it possible for Malcolm to flee to England and raise an army.
Macbeth returns carrying two daggers, suggesting that he had intended to murder Duncan's two sons but something had forced him to flee to his bed chamber. He tells his wife:
Methought I heard a voice cry 'Sleep no more!
Macbeth does murder sleep', the innocent sleep,
Sleep that knits up the ravell'd sleeve of care,
The death of each day's life, sore labour's bath,
Balm of hurt minds, great nature's second course,
Chief nourisher in life's feast,--
Whether the voice was real or imaginary, Macbeth felt he had to go into hiding or be caught with bloody daggers and bloody hands. A few lines later he says:
Still it cried 'Sleep no more!' to all the house:
'Glamis hath murder'd sleep, and therefore Cawdor
Shall sleep no more; Macbeth shall sleep no more.'
The voice is crying, or shouting, and threatening to expose him "to all the house." Otherwise, it would appear, he would have stolen into Malcolm's chamber and murdered him in his bed, and then stolen into Donalbain's chamber and done the same thing. The triple crime would, of course, have to be blamed on someone else; but if Macbeth subsequently murdered Duncan's grooms, the perpetrator of the three homicides could remain a mystery. It could have been anybody!
Shakespeare inserted his beautiful lines about the innocent sleep as a distraction to keep his audience from wondering whether he did indeed intend to kill the two sons. The conclusion of his monologue foretells his terrible punishment for murdering the King while the old man was enjoying his innocent sleep. Now Macbeth will have to lie awake every night reliving and regretting his crime, as well as listening fearfully to every sound inside and outside his room. His insomnia will cause him to experience hallucinations and become mentally incompetent to rule the kingdom he usurped. He will commence an orgy of killings, hoping to save himself from assassination by eliminating any probable enemies.