PageRenderTime 15ms CodeModel.GetById 1ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 0ms

/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 */
16package com.google.test.metric;
17
18
19public class CostModel {
20
21  /**
22   * Increase the weight we give on expensive methods. The ClassCost weighted
23   * average will be skewed towards expensive-to-test methods' costs within the
24   * class.
25   */
26  public static final double WEIGHT_TO_EMPHASIZE_EXPENSIVE_METHODS = 1.5;
27  private static final int DEFAULT_CYCLOMATIC_MULTIPLIER = 1;
28  private static final int DEFAULT_GLOBAL_MULTIPLIER = 10;
29  private static final int DEFAULT_CONSTRUCTOR_MULTIPLIER = 1;
30
31  private final double cyclomaticMultiplier;
32  private final double globalMultiplier;
33  private final double constructorMultiplier;
34
35  public double weightToEmphasizeExpensiveMethods = WEIGHT_TO_EMPHASIZE_EXPENSIVE_METHODS;
36
37  /* For testing only */
38  public CostModel() {
39    this(DEFAULT_CYCLOMATIC_MULTIPLIER, DEFAULT_GLOBAL_MULTIPLIER, DEFAULT_CONSTRUCTOR_MULTIPLIER);
40    weightToEmphasizeExpensiveMethods = 0;
41  }
42
43  public CostModel(double cyclomaticMultiplier, double globalMultiplier, double constructorMultiplier) {
44    this.cyclomaticMultiplier = cyclomaticMultiplier;
45    this.globalMultiplier = globalMultiplier;
46    this.constructorMultiplier = constructorMultiplier;
47  }
48
49  public int computeOverall(Cost cost) {
50    int sum = 0;
51    sum += cyclomaticMultiplier * cost.getCyclomaticComplexityCost();
52    sum += globalMultiplier * cost.getGlobalCost();
53    for (int count : cost.getLoDDistribution()) {
54      sum += count;
55    }
56    return sum;
57  }
58
59  public int computeClass(ClassCost classCost) {
60    WeightedAverage average = createWeighedAverage();
61    for (MethodCost methodCost : classCost.getMethods()) {
62      double overallCost;
63      if (methodCost.isConstructor()) {
64        overallCost = computeOverall(methodCost.getTotalCost()) * constructorMultiplier;
65      } else {
66        Cost constructorCost = methodCost.getConstructorDependentCost();
67        Cost nonConstructorCost = methodCost.getNonConstructorCost();
68        overallCost = computeOverall(nonConstructorCost)
69                           + constructorMultiplier * computeOverall(constructorCost);
70      }
71      average.addValue((long) overallCost);
72    }
73    return (int) average.getAverage();
74  }
75
76  public WeightedAverage createWeighedAverage() {
77    return new WeightedAverage(weightToEmphasizeExpensiveMethods);
78  }
79}