Performance of the CMS Level-1 trigger during commissioning with cosmic ray muons and LHC beams