Cost Optimizations in Runtime Testing and Diagnosis