Home | History | Annotate | Download | only in util
      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.compatibility.common.util;
     18 
     19 import com.android.tradefed.build.IBuildInfo;
     20 
     21 import java.util.concurrent.ConcurrentHashMap;
     22 
     23 /**
     24  * A simple in-memory store for metrics results. This should be used for hostside metrics reporting.
     25  */
     26 public class MetricsStore {
     27 
     28     // needs concurrent version as there can be multiple client accessing this.
     29     // But there is no additional protection for the same key as that should not happen.
     30     private static final ConcurrentHashMap<String, ReportLog> mMap =
     31             new ConcurrentHashMap<String, ReportLog>();
     32 
     33     private static final String START_TIME_TAG = "START_TIME_MS";
     34 
     35     private MetricsStore() {}
     36 
     37     /**
     38      * Stores a result. Existing result with the same key will be replaced.
     39      * Note that key is generated in the form of start_time#class#method name.
     40      * So there should be no concurrent test for the same (serial, class, method).
     41      * @param buildInfo
     42      * @param abi
     43      * @param classMethodName
     44      * @param reportLog Contains the result to be stored
     45      */
     46     public static void storeResult(IBuildInfo buildInfo, String abi, String classMethodName,
     47             ReportLog reportLog) {
     48         String startTime = buildInfo.getBuildAttributes().get(START_TIME_TAG);
     49         mMap.put(generateTestKey(startTime, abi, classMethodName), reportLog);
     50     }
     51 
     52     /**
     53      * retrieves a metric result for the given condition and remove it from the internal
     54      * storage. If there is no result for the given condition, it will return null.
     55      */
     56     public static ReportLog removeResult(IBuildInfo buildInfo, String abi, String classMethodName) {
     57         String startTime = buildInfo.getBuildAttributes().get(START_TIME_TAG);
     58         return mMap.remove(generateTestKey(startTime, abi, classMethodName));
     59     }
     60 
     61     /**
     62      * @return test key in the form of start_time#abi#class_name#method_name
     63      */
     64     private static String generateTestKey(String startTime, String abi, String classMethodName) {
     65         return String.format("%s#%s#%s", startTime, abi, classMethodName);
     66     }
     67 }
     68