If you want to optimize, you will want to have 50% of the food bucket the same turn the granary is produced. Then any food you'll produce will be store in the granary. If you have less than 50% of the bucket, you will lose one turn of food production which won't be stored. If you have more than 50% of the bucket, the granary will not be full for the next growth.
If you are size N and 1-pop whip the granary you need to whip when you have :
Code:
(food need for the growth at size N-1) / 2 - (food/turn you produce after whipping)
If you are size N and 2-pop whip the granary, you need to whip when you have :
Code:
(food need for the growth at size N-2) / 2 - (food/turn you produce after whipping)
It's always better to optimize, but an approximation I use is to whip around 25% for a 2-pop whip and 35% for a 1-pop whip.