PageRenderTime 17ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/testability-explorer/src/test/java/com/google/test/metric/PerformanceTest.java

http://testability-explorer.googlecode.com/
Java | 77 lines | 52 code | 10 blank | 15 comment | 0 complexity | 38ab57b76aecdb9856c27331c4a530a8 MD5 | raw file
Possible License(s): Apache-2.0
  1. /*
  2. * Copyright 2008 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. import java.io.File;
  18. import java.io.FileNotFoundException;
  19. import java.io.PrintStream;
  20. import java.util.Properties;
  21. public class PerformanceTest {
  22. public static void main(String[] args) throws Exception {
  23. new PerformanceTest().testStringAnalysisPerformance();
  24. }
  25. Runtime runtime = Runtime.getRuntime();
  26. JavaClassRepository repo = new JavaClassRepository();
  27. MetricComputer computer = new MetricComputer(repo, null,
  28. new RegExpWhiteList(), 1);
  29. File reportDir = new File("target/reports/perf");
  30. public void testStringAnalysisPerformance() throws Exception {
  31. System.gc();
  32. long startMem = usedMemory();
  33. long startTime = System.currentTimeMillis();
  34. ClassCost runPerformance = runPerformance(String.class);
  35. long endTime = System.currentTimeMillis();
  36. long endMem = usedMemory();
  37. System.gc();
  38. runPerformance.hashCode();
  39. runPerformance = null;
  40. System.gc();
  41. long end2Mem = usedMemory();
  42. double time = (endTime - startTime) / 1000d;
  43. double memTotal = (endMem - startMem) / 1024d / 1024d;
  44. double memCost = (endMem - end2Mem) / 1024d / 1024d;
  45. System.out.println("Execution time: " + time + " sec");
  46. System.out.println("Total Memory: " + memTotal + " MB");
  47. System.out.println("ClassCost Memory: " + memCost + " MB");
  48. reportDir.mkdirs();
  49. writeResult(time, "time.prop");
  50. writeResult(memTotal, "memTotal.prop");
  51. writeResult(memCost, "memCost.prop");
  52. }
  53. private void writeResult(double result, String file)
  54. throws FileNotFoundException {
  55. Properties timeProp = new Properties();
  56. timeProp.setProperty("YVALUE", ""+result);
  57. PrintStream os = new PrintStream(new File(reportDir, file));
  58. timeProp.list(os);
  59. os.close();
  60. }
  61. private long usedMemory() {
  62. return runtime.totalMemory() - runtime.freeMemory();
  63. }
  64. private ClassCost runPerformance(Class<String> clazz) {
  65. return computer.compute(clazz.getName());
  66. }
  67. }