/testability-explorer/src/main/java/com/google/test/metric/CostModel.java

http://testability-explorer.googlecode.com/ · Java · 79 lines · 48 code · 10 blank · 21 comment · 4 complexity · 6b43646bb0a3db4a7b159ee95357544d MD5 · raw file

  1. /*
  2. * Copyright 2007 Google Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.google.test.metric;
  17. public class CostModel {
  18. /**
  19. * Increase the weight we give on expensive methods. The ClassCost weighted
  20. * average will be skewed towards expensive-to-test methods' costs within the
  21. * class.
  22. */
  23. public static final double WEIGHT_TO_EMPHASIZE_EXPENSIVE_METHODS = 1.5;
  24. private static final int DEFAULT_CYCLOMATIC_MULTIPLIER = 1;
  25. private static final int DEFAULT_GLOBAL_MULTIPLIER = 10;
  26. private static final int DEFAULT_CONSTRUCTOR_MULTIPLIER = 1;
  27. private final double cyclomaticMultiplier;
  28. private final double globalMultiplier;
  29. private final double constructorMultiplier;
  30. public double weightToEmphasizeExpensiveMethods = WEIGHT_TO_EMPHASIZE_EXPENSIVE_METHODS;
  31. /* For testing only */
  32. public CostModel() {
  33. this(DEFAULT_CYCLOMATIC_MULTIPLIER, DEFAULT_GLOBAL_MULTIPLIER, DEFAULT_CONSTRUCTOR_MULTIPLIER);
  34. weightToEmphasizeExpensiveMethods = 0;
  35. }
  36. public CostModel(double cyclomaticMultiplier, double globalMultiplier, double constructorMultiplier) {
  37. this.cyclomaticMultiplier = cyclomaticMultiplier;
  38. this.globalMultiplier = globalMultiplier;
  39. this.constructorMultiplier = constructorMultiplier;
  40. }
  41. public int computeOverall(Cost cost) {
  42. int sum = 0;
  43. sum += cyclomaticMultiplier * cost.getCyclomaticComplexityCost();
  44. sum += globalMultiplier * cost.getGlobalCost();
  45. for (int count : cost.getLoDDistribution()) {
  46. sum += count;
  47. }
  48. return sum;
  49. }
  50. public int computeClass(ClassCost classCost) {
  51. WeightedAverage average = createWeighedAverage();
  52. for (MethodCost methodCost : classCost.getMethods()) {
  53. double overallCost;
  54. if (methodCost.isConstructor()) {
  55. overallCost = computeOverall(methodCost.getTotalCost()) * constructorMultiplier;
  56. } else {
  57. Cost constructorCost = methodCost.getConstructorDependentCost();
  58. Cost nonConstructorCost = methodCost.getNonConstructorCost();
  59. overallCost = computeOverall(nonConstructorCost)
  60. + constructorMultiplier * computeOverall(constructorCost);
  61. }
  62. average.addValue((long) overallCost);
  63. }
  64. return (int) average.getAverage();
  65. }
  66. public WeightedAverage createWeighedAverage() {
  67. return new WeightedAverage(weightToEmphasizeExpensiveMethods);
  68. }
  69. }