Put a voltmeter on the battery while trying to start. It is a good load test for the battery. Voltage should not drop below about 10.5 volts.
As said above, clean all battery connections first and fully charge the battery. Just because the bike will start and run sometimes doesn't mean the battery is good.
You don't say what year the bike is. If the battery is more than 5 years old, it may be time to replace it.