This essentially depends on how you prefer to measure your performance. I will just give a few simple examples to start. Let me know if you're looking for something more.
If you just want to achieve maximum $ return, then you should always use maximum margin, so long as your expected return (%) is higher than your cost to borrow.
For example, suppose you can use margin to double your investment, and the cost to borrow is 7%. If you're investing in some security that expects to return 10%, then your annual return on an account opened with $100 is:
(2 * $100 * 10% - $100 * 7%) / $100 = 13%
So, you see the expected return, amount of leverage, and cost to borrow will all factor in to your return.
Suppose you want to also account for the additional risk you're incurring. Then you could use the Sharpe Ratio. For example, suppose the same security has volatility of 20%, and the risk free rate is 5%. Then the Sharpe Ratio without leverage is:
(10% - 5%) / 20% = 0.25
The Sharpe Ratio using maximum margin is then:
(13% - 5%) / (2 * 20%) = 0.2,
where the 13% comes from the above formula. So on a risk-adjusted basis, it's better not to utilize margin in this particular example.