|
| 1 | +package com.microsoft.hydralab.center.openai; |
| 2 | + |
| 3 | +import com.alibaba.fastjson.JSON; |
| 4 | +import com.alibaba.fastjson.JSONArray; |
| 5 | +import com.alibaba.fastjson.JSONObject; |
| 6 | +import com.microsoft.hydralab.center.openai.data.ExceptionSuggestion; |
| 7 | +import com.microsoft.hydralab.center.openai.data.SimplifiedPerformanceDataSet; |
| 8 | +import com.microsoft.hydralab.center.openai.data.SimplifiedPerformanceResult; |
| 9 | +import com.microsoft.hydralab.common.entity.common.StorageFileInfo; |
| 10 | +import com.microsoft.hydralab.common.entity.common.TestRun; |
| 11 | +import com.microsoft.hydralab.common.file.StorageServiceClientProxy; |
| 12 | +import com.microsoft.hydralab.common.file.impl.AzureOpenaiConfig; |
| 13 | +import com.microsoft.hydralab.common.repository.StorageFileInfoRepository; |
| 14 | +import com.microsoft.hydralab.common.util.Const; |
| 15 | +import com.microsoft.hydralab.common.util.HydraLabRuntimeException; |
| 16 | +import com.microsoft.hydralab.performance.PerformanceInspectionResult; |
| 17 | +import com.microsoft.hydralab.performance.PerformanceResultParser; |
| 18 | +import com.microsoft.hydralab.performance.PerformanceTestResult; |
| 19 | +import com.microsoft.hydralab.performance.entity.AndroidBatteryInfo; |
| 20 | +import com.microsoft.hydralab.performance.entity.AndroidHprofMemoryInfo; |
| 21 | +import com.microsoft.hydralab.performance.entity.AndroidMemoryInfo; |
| 22 | +import com.microsoft.hydralab.performance.entity.IOSEnergyGaugeInfo; |
| 23 | +import com.microsoft.hydralab.performance.entity.IOSMemoryPerfInfo; |
| 24 | +import com.microsoft.hydralab.performance.entity.WindowsBatteryParsedData; |
| 25 | +import com.microsoft.hydralab.performance.entity.WindowsMemoryParsedData; |
| 26 | +import org.springframework.stereotype.Service; |
| 27 | + |
| 28 | +import javax.annotation.Resource; |
| 29 | +import java.io.BufferedReader; |
| 30 | +import java.io.File; |
| 31 | +import java.io.FileNotFoundException; |
| 32 | +import java.io.FileReader; |
| 33 | +import java.io.IOException; |
| 34 | +import java.util.ArrayList; |
| 35 | +import java.util.HashMap; |
| 36 | +import java.util.List; |
| 37 | +import java.util.Map; |
| 38 | + |
| 39 | +import org.springframework.context.ApplicationContext; |
| 40 | + |
| 41 | +import static com.microsoft.hydralab.center.util.CenterConstant.CENTER_TEMP_FILE_DIR; |
| 42 | + |
| 43 | +@Service |
| 44 | +public class SuggestionService { |
| 45 | + @Resource |
| 46 | + StorageFileInfoRepository storageFileInfoRepository; |
| 47 | + @Resource |
| 48 | + StorageServiceClientProxy storageServiceClientProxy; |
| 49 | + private final AzureOpenaiConfig openaiConfig; |
| 50 | + private AzureOpenAIServiceClient oaiClient = null; |
| 51 | + private final Map<PerformanceResultParser.PerformanceResultParserType, Class> performanceTypeMap = Map.ofEntries( |
| 52 | + Map.entry(PerformanceResultParser.PerformanceResultParserType.PARSER_ANDROID_BATTERY_INFO, AndroidBatteryInfo.class), |
| 53 | + Map.entry(PerformanceResultParser.PerformanceResultParserType.PARSER_WIN_MEMORY, WindowsMemoryParsedData.class), |
| 54 | + Map.entry(PerformanceResultParser.PerformanceResultParserType.PARSER_WIN_BATTERY, WindowsBatteryParsedData.class), |
| 55 | + Map.entry(PerformanceResultParser.PerformanceResultParserType.PARSER_ANDROID_MEMORY_INFO, AndroidMemoryInfo.class), |
| 56 | + Map.entry(PerformanceResultParser.PerformanceResultParserType.PARSER_ANDROID_MEMORY_DUMP, AndroidHprofMemoryInfo.class), |
| 57 | + Map.entry(PerformanceResultParser.PerformanceResultParserType.PARSER_IOS_ENERGY, IOSEnergyGaugeInfo.class), |
| 58 | + Map.entry(PerformanceResultParser.PerformanceResultParserType.PARSER_IOS_MEMORY, IOSMemoryPerfInfo.class) |
| 59 | + ); |
| 60 | + |
| 61 | + public SuggestionService(ApplicationContext applicationContext) { |
| 62 | + this.openaiConfig = applicationContext.getBean(Const.AzureOpenaiConfig.AZURE_OPENAI_CONFIG, AzureOpenaiConfig.class); |
| 63 | + if (openaiConfig.getApiKey() != null && openaiConfig.getDeployment() != null && openaiConfig.getEndpoint() != null) { |
| 64 | + this.oaiClient = new AzureOpenAIServiceClient( |
| 65 | + openaiConfig.getApiKey(), |
| 66 | + openaiConfig.getDeployment(), |
| 67 | + openaiConfig.getEndpoint()); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + public void performanceAnalyze(TestRun testRun) { |
| 72 | + List<PerformanceTestResult> performanceResults = getPerformanceResult(testRun); |
| 73 | + if (performanceResults == null) { |
| 74 | + return; |
| 75 | + } |
| 76 | + String perfsString = convertPerformanceTestToJsonString(performanceResults); |
| 77 | + String perfSuggestion = getOpenaiPerformanceSuggestion(perfsString); |
| 78 | + if (perfSuggestion != null) { |
| 79 | + testRun.setSuggestion(perfSuggestion); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + public ExceptionSuggestion exceptionAnalyze(TestRun testRun) { |
| 84 | + ExceptionSuggestion suggestion = new ExceptionSuggestion(); |
| 85 | + return suggestion; |
| 86 | + } |
| 87 | + |
| 88 | + @SuppressWarnings("unchecked") |
| 89 | + private List<PerformanceTestResult> getPerformanceResult(TestRun testRun) { |
| 90 | + List<PerformanceTestResult> performanceTestResult = null; |
| 91 | + |
| 92 | + String fileId = ""; |
| 93 | + for (StorageFileInfo f : testRun.getAttachments()) { |
| 94 | + if (f.getFileName().contains(Const.PerformanceConfig.DEFAULT_FILE_NAME)) { |
| 95 | + fileId = f.getFileId(); |
| 96 | + break; |
| 97 | + } |
| 98 | + } |
| 99 | + if (fileId.isEmpty()) { |
| 100 | + return null; |
| 101 | + } |
| 102 | + |
| 103 | + StorageFileInfo perfBlobFile = storageFileInfoRepository.findById(fileId).orElse(null); |
| 104 | + if (perfBlobFile == null) { |
| 105 | + throw new HydraLabRuntimeException("Graph zip file not exist!"); |
| 106 | + } |
| 107 | + File perfFile = new File(CENTER_TEMP_FILE_DIR, perfBlobFile.getBlobPath()); |
| 108 | + if (!perfFile.exists()) { |
| 109 | + storageServiceClientProxy.download(perfFile, perfBlobFile); |
| 110 | + } |
| 111 | + if (!perfFile.exists()) { |
| 112 | + return null; |
| 113 | + } |
| 114 | + |
| 115 | + try (FileReader reader = new FileReader(perfFile)) { |
| 116 | + BufferedReader br = new BufferedReader(reader); |
| 117 | + String line; |
| 118 | + StringBuilder sb = new StringBuilder(); |
| 119 | + while ((line = br.readLine()) != null) { |
| 120 | + sb.append(line); |
| 121 | + } |
| 122 | + br.close(); |
| 123 | + String jsonString = sb.toString(); |
| 124 | + |
| 125 | + List<PerformanceTestResult> results = JSON.parseArray(jsonString, PerformanceTestResult.class); |
| 126 | + JSONArray ja = JSON.parseArray(jsonString); |
| 127 | + for (int i = 0; i < ja.size(); i++) { |
| 128 | + PerformanceTestResult result = results.get(i); |
| 129 | + Class classType = this.performanceTypeMap.get(result.parserType); |
| 130 | + JSONObject jo = (JSONObject)ja.get(i); |
| 131 | + List<Object> inspects = new ArrayList<Object>(); |
| 132 | + Object performanceInspectionResults = jo.get("performanceInspectionResults"); |
| 133 | + if (performanceInspectionResults instanceof List<?>) { |
| 134 | + inspects = (List<Object>)performanceInspectionResults; |
| 135 | + } |
| 136 | + for (int j = 0; j < inspects.size(); j++) { |
| 137 | + JSONObject inspect = (JSONObject)inspects.get(j); |
| 138 | + if (inspect == null) { |
| 139 | + continue; |
| 140 | + } |
| 141 | + JSONObject parsedData = (JSONObject)inspect.get("parsedData"); |
| 142 | + if (parsedData == null) { |
| 143 | + continue; |
| 144 | + } |
| 145 | + result.performanceInspectionResults.get(j).parsedData = parsedData.toJavaObject(classType); |
| 146 | + } |
| 147 | + } |
| 148 | + performanceTestResult = results; |
| 149 | + } catch (FileNotFoundException e) { |
| 150 | + e.printStackTrace(); |
| 151 | + } catch (IOException e) { |
| 152 | + e.printStackTrace(); |
| 153 | + } catch (Exception e) { |
| 154 | + e.printStackTrace(); |
| 155 | + } |
| 156 | + return performanceTestResult; |
| 157 | + } |
| 158 | + |
| 159 | + private String convertPerformanceTestToJsonString(List<PerformanceTestResult> results) { |
| 160 | + List<SimplifiedPerformanceResult> sResults = new ArrayList<>(); |
| 161 | + for (PerformanceTestResult result : results) { |
| 162 | + SimplifiedPerformanceResult sResult = new SimplifiedPerformanceResult(); |
| 163 | + List<SimplifiedPerformanceDataSet> dataSetArr = new ArrayList<>(); |
| 164 | + for (PerformanceInspectionResult ins : result.performanceInspectionResults) { |
| 165 | + SimplifiedPerformanceDataSet ds = new SimplifiedPerformanceDataSet(); |
| 166 | + if (ins.parsedData instanceof AndroidMemoryInfo) { |
| 167 | + AndroidMemoryInfo info = (AndroidMemoryInfo)ins.parsedData; |
| 168 | + insertPerformanceData(ds, "CodePss", info.getCodePss()); |
| 169 | + insertPerformanceData(ds, "CodeRss", info.getCodeRss()); |
| 170 | + insertPerformanceData(ds, "GraphicsPss", info.getGraphicsPss()); |
| 171 | + insertPerformanceData(ds, "GraphicsRss", info.getGraphicsRss()); |
| 172 | + insertPerformanceData(ds, "StackPss", info.getStackPss()); |
| 173 | + insertPerformanceData(ds, "StackRss", info.getStackRss()); |
| 174 | + insertPerformanceData(ds, "HeapPss", info.getJavaHeapPss()); |
| 175 | + insertPerformanceData(ds, "HeapRss", info.getJavaHeapRss()); |
| 176 | + insertPerformanceData(ds, "SystemPss", info.getSystemPss()); |
| 177 | + insertPerformanceData(ds, "SystemRss", info.getSystemRss()); |
| 178 | + } else if (ins.parsedData instanceof AndroidBatteryInfo) { |
| 179 | + AndroidBatteryInfo info = (AndroidBatteryInfo)ins.parsedData; |
| 180 | + insertPerformanceData(ds, "Cpu", info.getCpu()); |
| 181 | + insertPerformanceData(ds, "Ratio", info.getRatio()); |
| 182 | + insertPerformanceData(ds, "AppUsage", info.getAppUsage()); |
| 183 | + insertPerformanceData(ds, "WakeLock", info.getWakeLock()); |
| 184 | + } else { |
| 185 | + continue; |
| 186 | + } |
| 187 | + ds.setTimestamp(ins.timestamp); |
| 188 | + dataSetArr.add(ds); |
| 189 | + } |
| 190 | + |
| 191 | + sResult.setType(result.inspectorType.toString()); |
| 192 | + sResult.setDataset(dataSetArr); |
| 193 | + sResults.add(sResult); |
| 194 | + } |
| 195 | + return JSON.toJSONString(sResults); |
| 196 | + } |
| 197 | + |
| 198 | + private void insertPerformanceData(SimplifiedPerformanceDataSet ds, String name, Object data) { |
| 199 | + if (ds.getInspect() == null) { |
| 200 | + ds.setInspect(new HashMap<String, Object>()); |
| 201 | + } |
| 202 | + ds.getInspect().put(name, data); |
| 203 | + } |
| 204 | + |
| 205 | + private String getOpenaiPerformanceSuggestion(String perfSuggestion) { |
| 206 | + if (this.oaiClient != null) { |
| 207 | + return oaiClient.completion(perfSuggestion); |
| 208 | + } |
| 209 | + return null; |
| 210 | + } |
| 211 | +} |
0 commit comments