private static void PersistData(Dictionary <String, Object> collectedData) { log.Debug("Starting persistData for transaction " + collectedData["TxnName"]); String strsuccess = "Success"; try { Dictionary <String, Object> hostDetails = new Dictionary <String, Object>(); Dictionary <String, Object> details = new Dictionary <String, Object>(); Dictionary <String, Object> others = new Dictionary <String, Object>(); Dictionary <String, Double> resourceTime = new Dictionary <String, Double>(); hostDetails.Add("name", Dns.GetHostName()); others.Add("domElementCount", collectedData["DomElements"]); others.Add("dom", collectedData["DOMContent"].ToString()); if (collectedData.ContainsKey("msFirstPaint")) { others.Add("msFirstPaint", collectedData["msFirstPaint"]); } details.Add("ClientName", collectedData["ClientName"]); details.Add("ProjectName", collectedData["ProjectName"]); details.Add("Scenario", collectedData["Scenario"]); details.Add("licenseKey", collectedData.ContainsKey("licenseKey")?collectedData["licenseKey"]:""); details.Add("dataStoreUrl", collectedData.ContainsKey("dataStoreUrl") ? collectedData["dataStoreUrl"]:""); details.Add("transactionName", collectedData["TxnName"]); details.Add("url", collectedData["Url"]); details.Add("txnStatus", collectedData["txnStatus"]); details.Add("Release", collectedData["Release"]); details.Add("RunID", collectedData["RunID"].ToString()); details.Add("RunTime", collectedData["RunTime"]); details.Add("BuildNumber", collectedData["BuildNumber"]); details.Add("staticResourceExtension", collectedData["staticResourceExtension"]); details.Add("imageResourceExtension", collectedData["imageResourceExtension"]); details.Add("resourceDurationThreshold", collectedData["resourceDurationThreshold"]); details.Add("source", "SeleniumCSharp"); log.Debug("Calling calculateBackendTime"); resourceTime = CalculateBackendTime((List <Dictionary <string, object> >)collectedData["ResourceTime"], collectedData["NavType"].ToString()); log.Debug("Completed calculateBackendTime"); if (Convert.ToBoolean(collectedData["NavType"])) { details.Add("NavType", "Hard"); details.Add("speedIndex", collectedData["SpeedIndex"]); details.Add("StartTime", collectedData["StartTime"]); details.Add("resourceLoadTime", resourceTime["backendTime"]); details.Add("visuallyComplete", resourceTime["totalTime"]); } else { details.Add("NavType", "Soft"); //details.put("SoftNavTotalTime",(Long.parseLong(collectedData.get("SoftNavTotalTime").toString()) <= 0 ? 0 : collectedData.get("SoftNavTotalTime"))); details.Add("StartTime", collectedData["StartTime"]); details.Add("resourceLoadTime", resourceTime["backendTime"]); details.Add("visuallyComplete", resourceTime["totalTime"]); } Dictionary <string, object> jsonDoc = new Dictionary <string, object>(); jsonDoc.Add("details", details); jsonDoc.Add("host", hostDetails); jsonDoc.Add("platform", collectedData["Browser"]); jsonDoc.Add("memory", collectedData["Memory"]); jsonDoc.Add("others", others); jsonDoc.Add("navtime", collectedData["NavigationTime"]); if (collectedData.ContainsKey("MarkTime")) { jsonDoc.Add("marktime", markListProcessed((List <Dictionary <string, object> >)collectedData["MarkTime"])); } Boolean crawlEnabled = (collectedData.ContainsKey("isResourceCrawlingEnabled") ? Convert.ToBoolean(collectedData["isResourceCrawlingEnabled"]) : true); log.Debug("Crawl Enabled is " + crawlEnabled + " for " + collectedData["TxnName"]); if (crawlEnabled) { jsonDoc.Add("resources", CrawlUtils.getResourceDetails((List <Dictionary <String, Object> >)collectedData["ResourceTime"])); } else { jsonDoc.Add("resources", collectedData["ResourceTime"]); } log.Debug("Crawl Completed: "); log.Debug("Data collected for " + collectedData["TxnName"] + " : " + jsonDoc.ToString()); ICXOptimizeService cxOpService = new CXOptimiseServiceImpl(); String post = cxOpService.uploadPerformanceData(JSONUtils.MapToJsonString(jsonDoc)); log.Debug("Post Status : " + post); //Checking if the response is null. If so, then it will go to finally block if (post == null) { log.Error("Unable to insert stats into datastore for " + collectedData["TxnName"] + ". Please check logs for further details."); return; } if (!post.Contains(strsuccess)) { log.Error("The data could not be uploaded for " + collectedData["TxnName"] + ". The response from data store is " + post); } else { log.Info("DataUploaded for succesfully for {}" + collectedData["TxnName"]); } } catch (Exception e) { log.Error("Exception in persistData for " + collectedData["TxnName"] + " at " + e); } }
public static Dictionary <String, Object> collateData(String txnName, String url, IJavaScriptExecutor jsExe, int txnStatus) { log.Debug("Started collate data for Transaction : " + txnName + " with status " + txnStatus); Dictionary <String, Object> rtnValue = new Dictionary <string, object>(); long currNavTime; long totalTime = 0; long serverTime = 0; Boolean navType = false; double speedIndex = 0; Dictionary <String, Object> collectedData = new Dictionary <string, object>(); try { Dictionary <string, object> navigationDetails = new Dictionary <string, object>(); Dictionary <string, object> memoryDetails = new Dictionary <string, object>(); Dictionary <string, object> browserDetails = new Dictionary <string, object>(); Dictionary <String, Object> heapUsage = new Dictionary <string, object>(); List <Dictionary <String, Object> > resourceDetails = null; List <Dictionary <String, Object> > markDetails = null; Boolean isNavigationAPIEnabled, isResourceAPIEnabled, isMemoryAPIEnabled, isMarkAPIEnabled; StringBuilder document = new StringBuilder(); long loadEventEnd, currentHeapUsage, msFirstPaint; long startTime, endTime, duration; //Load Configuration from remote service ConfigurationLoader config = ConfigurationLoader.getInstance(); if (config.clientConfig.Count > 0) { log.Debug("Configuration Loading.."); collectedData = (from x in config.clientConfig select x).ToDictionary(x => x.Key, x => x.Value); collectedData.Add("TxnName", txnName); collectedData.Add("txnStatus", txnStatus); collectedData.Add("Url", url); isNavigationAPIEnabled = ((config.clientConfig.ContainsKey("isNavigationAPIEnabled")) ? Convert.ToBoolean(config.clientConfig["isNavigationAPIEnabled"]) : false); isResourceAPIEnabled = ((config.clientConfig.ContainsKey("isResourceAPIEnabled")) ? Convert.ToBoolean(config.clientConfig["isResourceAPIEnabled"]) : false); isMemoryAPIEnabled = ((config.clientConfig.ContainsKey("isMemoryAPIEnabled")) ? Convert.ToBoolean(config.clientConfig["isMemoryAPIEnabled"]) : false); isMarkAPIEnabled = ((config.clientConfig.ContainsKey("isMarkAPIEnabled")) ? Convert.ToBoolean(config.clientConfig["isMarkAPIEnabled"]) : false); log.Debug("Configuration Loading completed"); log.Debug("Navigation API data collection started for " + txnName); if (isNavigationAPIEnabled) { log.Debug("User Agent : " + CollectorConstants.getUserAgent()); if (CollectorConstants.getUserAgent().Contains("Trident")) { log.Debug("Processing IE logic"); startTime = getCurrentMilliSeconds(); do { loadEventEnd = Convert.ToInt64(jsExe.ExecuteScript(CollectorConstants.getLoadEventEndIE()).ToString()); endTime = getCurrentMilliSeconds(); duration = (endTime - startTime) / 1000; if (duration > 180) { break; } }while (loadEventEnd <= 0); object strNavigationDetails = jsExe.ExecuteScript(CollectorConstants.getNavigationTimeIE()); navigationDetails = JSONUtils.JsonStringToMap(strNavigationDetails.ToString()); } else { log.Debug("Processing other browser logic"); //Loop until loadEventEnd is non zero or 3 minutes to avoid infinite loop startTime = getCurrentMilliSeconds(); do { //navigationDetails = (Map<String, Object>)jsExe.executeScript("var performance = window.performance || window.webkitPerformance || window.mozPerformance || window.msPerformance || {};var timings = performance.timing || {}; return timings.loadEventEnd;"); loadEventEnd = Convert.ToInt64(jsExe.ExecuteScript(CollectorConstants.getLoadEventEnd()).ToString()); endTime = getCurrentMilliSeconds(); duration = (endTime - startTime) / 1000; if (duration > 180) { break; } }while (loadEventEnd <= 0); //Adding the loop to avoid loadEventEnd = 0 navigationDetails = (Dictionary <String, Object>)jsExe.ExecuteScript(CollectorConstants.getNavigationTime()); if (CollectorConstants.getUserAgent().Contains("Chrome") && !CollectorConstants.getUserAgent().Contains("Edge")) { msFirstPaint = (long)jsExe.ExecuteScript(CollectorConstants.getFirstPaint()); if (msFirstPaint != -9999) { collectedData.Add("msFirstPaint", msFirstPaint); } else { collectedData.Add("msFirstPaint", navigationDetails["loadEventEnd"]); } } else { //No first paint event available for firefox if (!CollectorConstants.getUserAgent().Contains("Edge")) { collectedData.Add("msFirstPaint", navigationDetails["loadEventEnd"]); } } } log.Debug("Collected Navigation API data " + navigationDetails.ToString() + " for Transaction : " + txnName); //validate if this is a new transaction. If true persist data else break immediately currNavTime = (long)navigationDetails["navigationStart"]; if (currNavTime > getPrevTxnStartTime()) { setPrevTxnStartTime(currNavTime); navType = true; collectedData.Add("StartTime", currNavTime); totalTime = ((long)navigationDetails["loadEventEnd"] - (long)navigationDetails["navigationStart"]); serverTime = ((long)navigationDetails["responseStart"] - (long)navigationDetails["requestStart"]); log.Debug("Hard Navigation : {}" + txnName); speedIndex = Convert.ToDouble(jsExe.ExecuteScript(CollectorConstants.getSpeedIndex())); collectedData.Add("SpeedIndex", speedIndex); log.Debug("SpeedIndex for Transaction:" + txnName + " is " + speedIndex); } else { navType = false; collectedData.Add("StartTime", getCurrentMilliSeconds()); navigationDetails = null; log.Debug("Soft Navigation : {}" + txnName); } collectedData.Add("NavigationTime", navigationDetails); collectedData.Add("NavType", navType); } // Fetch Client-side Resource Details via Resource Timing API if (isResourceAPIEnabled) { long beforeLength, afterLength = 0; do { beforeLength = (long)jsExe.ExecuteScript(CollectorConstants.getResourceLength()); Thread.Sleep(CollectorConstants.getResourceSettleTime()); afterLength = (long)jsExe.ExecuteScript(CollectorConstants.getResourceLength()); } while (beforeLength < afterLength); if (CollectorConstants.getUserAgent().Contains("Trident")) { var serializer = new System.Web.Script.Serialization.JavaScriptSerializer(); resourceDetails = serializer.Deserialize <List <Dictionary <string, object> > > (jsExe.ExecuteScript(CollectorConstants.getResourceTimeIE()).ToString()); } else { resourceDetails = ((IReadOnlyCollection <Object>)jsExe.ExecuteScript(CollectorConstants.getResourceTime())).ToDictionaries(); } jsExe.ExecuteScript(CollectorConstants.clearResourceTiming()); log.Debug("Collected Resource Timing API : " + resourceDetails.ToString()); collectedData.Add("ResourceTime", resourceDetails); } if (navigationDetails == null && resourceDetails.Count <= 0) { log.Info("The transaction " + txnName + " probably did not make a server request and hence will be ignored."); return(rtnValue); } // Fetch Client-side Menory Details via Memory API if (isMemoryAPIEnabled) { if (CollectorConstants.getUserAgent().Contains("Chrome")) { heapUsage = (Dictionary <String, Object>)jsExe.ExecuteScript(CollectorConstants.getHeapUsage()); log.Debug("Heap Usage : " + heapUsage.ToString()); if (heapUsage.ContainsKey("totalJSHeapSize")) { memoryDetails.Add("jsHeapSizeLimit", heapUsage["jsHeapSizeLimit"]); memoryDetails.Add("totalJSHeapSize", heapUsage["totalJSHeapSize"]); memoryDetails.Add("usedJSHeapSize", heapUsage["usedJSHeapSize"]); if (getPrevTxnHeapSize() == 0) { setPrevTxnHeapSize((long)heapUsage["usedJSHeapSize"]); memoryDetails.Add("currentPageUsage", getPrevTxnHeapSize()); } else { currentHeapUsage = ((long)heapUsage["usedJSHeapSize"] - getPrevTxnHeapSize()); setPrevTxnHeapSize((long)heapUsage["usedJSHeapSize"]); memoryDetails.Add("currentPageUsage", currentHeapUsage); } } else { memoryDetails.Add("jsHeapSizeLimit", 0); memoryDetails.Add("totalJSHeapSize", 0); memoryDetails.Add("usedJSHeapSize", 0); memoryDetails.Add("currentPageUsage", 0); } } else { memoryDetails.Add("jsHeapSizeLimit", 0); memoryDetails.Add("totalJSHeapSize", 0); memoryDetails.Add("usedJSHeapSize", 0); memoryDetails.Add("currentPageUsage", 0); } log.Debug("Collected Memory Details : " + memoryDetails.ToString()); collectedData.Add("Memory", memoryDetails); } //Fetch dom element count long domElements = (long)jsExe.ExecuteScript(CollectorConstants.getDomLength()); log.Debug("DOM Element Count :{} " + domElements); collectedData.Add("DomElements", domElements); //Fetch Browser Details browserDetails.Add("UserAgent", CollectorConstants.getUserAgent()); collectedData.Add("Browser", browserDetails); // Fetch Client-side details Details via ResourceTiming API if (isMarkAPIEnabled) { Thread.Sleep(CollectorConstants.getMarkWaitTime()); if (CollectorConstants.getUserAgent().Contains("Trident")) { var serializer = new System.Web.Script.Serialization.JavaScriptSerializer(); markDetails = serializer.Deserialize <List <Dictionary <string, object> > > (jsExe.ExecuteScript(CollectorConstants.getMarkTimeIE()).ToString()); } else { markDetails = ((IReadOnlyCollection <Object>)jsExe.ExecuteScript(CollectorConstants.getResourceTime())).ToDictionaries(); } if (markDetails.Count > 0) { collectedData.Add("MarkTime", markDetails); } jsExe.ExecuteScript(CollectorConstants.clearMarkTiming()); log.Info("Collected mark details from Resource Timing API : " + markDetails.ToString()); } //Store DOM if required for analysis if (config.clientConfig.ContainsKey("isDOMNeeded") && Convert.ToBoolean(config.clientConfig["isDOMNeeded"].ToString())) { if (navType == true) { document = document.Append((jsExe.ExecuteScript(CollectorConstants.getDom())).ToString()); } else { document = null; } } else { document = null; } collectedData.Add("DOMContent", document); log.Debug("DOM Element : {}" + document); log.Debug("Calling data persist for " + txnName + " asynchronously"); AsyncpersistData(collectedData); log.Debug("Completed calling data persist for " + txnName + " asynchronously"); rtnValue.Add("UploadStatus", "Success"); rtnValue.Add("RunID", config.clientConfig["RunID"]); rtnValue.Add("totalTime", totalTime); rtnValue.Add("serverTime", serverTime); rtnValue.Add("txnName", txnName); if (txnStatus == 1) { rtnValue.Add("txnStatus", "Pass"); } else { rtnValue.Add("txnStatus", "Fail"); } } else { log.Error("Exception in collateData for transaction " + txnName + " in getting configuration"); } } catch (Exception e) { log.Error("Exception in collateData for transaction " + txnName + " at " + e); } log.Debug("Completed collating data for " + txnName); return(rtnValue); }