Home | History | Annotate | Download | only in test
      1 /*
      2  * Copyright (C) 2015 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of 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,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.ahat;
     18 
     19 import com.android.tools.perflib.heap.ClassObj;
     20 import com.android.tools.perflib.heap.Field;
     21 import com.android.tools.perflib.heap.Instance;
     22 import java.io.File;
     23 import java.io.IOException;
     24 import java.util.Map;
     25 
     26 /**
     27  * The TestDump class is used to get an AhatSnapshot for the test-dump
     28  * program.
     29  */
     30 public class TestDump {
     31   // It can take on the order of a second to parse and process the test-dump
     32   // hprof. To avoid repeating this overhead for each test case, we cache the
     33   // loaded instance of TestDump and reuse it when possible. In theory the
     34   // test cases should not be able to modify the cached snapshot in a way that
     35   // is visible to other test cases.
     36   private static TestDump mCachedTestDump = null;
     37 
     38   private AhatSnapshot mSnapshot = null;
     39 
     40   /**
     41    * Load the test-dump.hprof file.
     42    * The location of the file is read from the system property
     43    * "ahat.test.dump.hprof", which is expected to be set on the command line.
     44    * For example:
     45    *   java -Dahat.test.dump.hprof=test-dump.hprof -jar ahat-tests.jar
     46    *
     47    * An IOException is thrown if there is a failure reading the hprof file.
     48    */
     49   private TestDump() throws IOException {
     50       String hprof = System.getProperty("ahat.test.dump.hprof");
     51       mSnapshot = AhatSnapshot.fromHprof(new File(hprof));
     52   }
     53 
     54   /**
     55    * Get the AhatSnapshot for the test dump program.
     56    */
     57   public AhatSnapshot getAhatSnapshot() {
     58     return mSnapshot;
     59   }
     60 
     61   /**
     62    * Return the value of a field in the DumpedStuff instance in the
     63    * snapshot for the test-dump program.
     64    */
     65   public Object getDumpedThing(String name) {
     66     ClassObj main = mSnapshot.findClass("Main");
     67     Instance stuff = null;
     68     for (Map.Entry<Field, Object> fields : main.getStaticFieldValues().entrySet()) {
     69       if ("stuff".equals(fields.getKey().getName())) {
     70         stuff = (Instance) fields.getValue();
     71       }
     72     }
     73     return InstanceUtils.getField(stuff, name);
     74   }
     75 
     76   /**
     77    * Get the test dump.
     78    * An IOException is thrown if there is an error reading the test dump hprof
     79    * file.
     80    * To improve performance, this returns a cached instance of the TestDump
     81    * when possible.
     82    */
     83   public static synchronized TestDump getTestDump() throws IOException {
     84     if (mCachedTestDump == null) {
     85       mCachedTestDump = new TestDump();
     86     }
     87     return mCachedTestDump;
     88   }
     89 }
     90