PageRenderTime 33ms CodeModel.GetById 22ms app.highlight 8ms RepoModel.GetById 1ms 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
 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 */
16package com.google.test.metric;
17
18import java.io.File;
19import java.io.FileNotFoundException;
20import java.io.PrintStream;
21import java.util.Properties;
22
23public class PerformanceTest {
24
25  public static void main(String[] args) throws Exception {
26    new PerformanceTest().testStringAnalysisPerformance();
27  }
28
29  Runtime runtime = Runtime.getRuntime();
30  JavaClassRepository repo = new JavaClassRepository();
31  MetricComputer computer = new MetricComputer(repo, null,
32      new RegExpWhiteList(), 1);
33  File reportDir = new File("target/reports/perf");
34
35  public void testStringAnalysisPerformance() throws Exception {
36    System.gc();
37    long startMem = usedMemory();
38    long startTime = System.currentTimeMillis();
39    ClassCost runPerformance = runPerformance(String.class);
40    long endTime = System.currentTimeMillis();
41    long endMem = usedMemory();
42    System.gc();
43    runPerformance.hashCode();
44    runPerformance = null;
45    System.gc();
46    long end2Mem = usedMemory();
47
48    double time = (endTime - startTime) / 1000d;
49    double memTotal = (endMem - startMem) / 1024d / 1024d;
50    double memCost = (endMem - end2Mem) / 1024d / 1024d;
51    System.out.println("Execution time: " + time + " sec");
52    System.out.println("Total Memory: " + memTotal + " MB");
53    System.out.println("ClassCost Memory: " + memCost + " MB");
54    reportDir.mkdirs();
55    writeResult(time, "time.prop");
56    writeResult(memTotal, "memTotal.prop");
57    writeResult(memCost, "memCost.prop");
58  }
59
60  private void writeResult(double result, String file)
61      throws FileNotFoundException {
62    Properties timeProp = new Properties();
63    timeProp.setProperty("YVALUE", ""+result);
64    PrintStream os = new PrintStream(new File(reportDir, file));
65    timeProp.list(os);
66    os.close();
67  }
68
69  private long usedMemory() {
70    return runtime.totalMemory() - runtime.freeMemory();
71  }
72
73  private ClassCost runPerformance(Class<String> clazz) {
74    return computer.compute(clazz.getName());
75  }
76
77}