Пример #1
0
        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);
        }