measure rpm of stepper motor

PLease would be helpful if you give the code for the encoder and also tell me where will the encoder be attached to measure the speed?It should actually be kind of automation so as to reduce human effort of holding the encoder each time.