Esempio n. 1
0
        /*
         * private Dictionary<String, String> GetBeaconPropertyLoadConfig(String path)
         * {
         *
         *  string fullPathName = string.Empty;
         *  if (path != null)
         *  {
         *      fullPathName = propertyDirPath + "\\Collector.properties";
         *  }
         *  else
         *  {
         *      string projectDir = AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
         *      LoggingMessage.DisplayMessage("CXOptimize - Project Target Dir Path -> " + projectDir, LoggingMessage.MessageType.DEBUG, log);
         *      GetPropertyFile(projectDir);
         *
         *  }
         *  string[] split;
         *  string line = String.Empty;
         *  Dictionary<String, String> config = null;
         *
         *  //string projectDir = AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
         *
         *
         *  string environmentKey = ConfigurationManager.AppSettings["Environment"];
         *  if (environmentKey == null || environmentKey == string.Empty)
         *      fullPathName = propertyDirPath + "\\Collector.properties";
         *  else
         *      fullPathName = string.Format("{0}\\{1}_Collector.properties", propertyDirPath, environmentKey);
         *
         *  LoggingMessage.DisplayMessage("CXOptimize - Config Path-> " + fullPathName, LoggingMessage.MessageType.DEBUG, log);
         *
         *  if (File.Exists(fullPathName))
         *  {
         *      config = new Dictionary<string, string>();
         *      using (StreamReader file = new StreamReader(fullPathName))
         *      {
         *          while ((line = file.ReadLine()) != null)
         *          {
         *              split = line.Split('=');
         *              if (split.Length == 2)
         *                  config.Add(split[0], split[1]);
         *          }
         *      }
         *
         *  }
         *  else
         *  {
         *      throw new ArgumentException("Collector Property file missing");
         *  }
         *
         *  return config;
         * }
         */

        protected ConfigurationLoader()
        {
            try
            {
                log.Debug("Starting Loading Configuration");

                String path = CollectorConstants.getCollectorProperties();

                if (path == null)
                {
                    GetPropertyFile(AppDomain.CurrentDomain.SetupInformation.ApplicationBase);
                    path = propertyDirPath + "\\Collector.properties";
                }
                else
                {
                    path = path + "\\Collector.properties";
                }

                Dictionary <String, String> config;
                string[] split;
                string   line = String.Empty;

                if (File.Exists(path))
                {
                    config = new Dictionary <string, string>();
                    using (StreamReader file = new StreamReader(path))
                    {
                        while ((line = file.ReadLine()) != null)
                        {
                            split = line.Split('=');
                            if (split.Length == 2)
                            {
                                config.Add(split[0], split[1]);
                            }
                        }
                    }
                }
                else
                {
                    throw new ArgumentException("Collector Property file missing");
                }


                if (config != null)
                {
                    log.Debug("Properties Load Successfull from file : " + config.ToString());
                    CollectorConstants.setBeaconURL(config["beaconUrl"]);
                    CollectorConstants.setClientName(config["clientName"]);
                    CollectorConstants.setProjectName(config["projectName"]);
                    CollectorConstants.setScenarioName(config["scenarioName"]);
                    CollectorConstants.setLicenseKey(config["LicenseKey"]);
                    CollectorConstants.setUserName(config["UserName"]);
                    CollectorConstants.setPassword(config["Password"]);

                    /*
                     * if (config.ContainsKey("Release") && config["Release"] != String.Empty)
                     * {
                     *  CollectorConstants.setRelease(config["Release"].ToString());
                     * }
                     * else
                     * {
                     *  CollectorConstants.setRelease("NA");
                     * }
                     * if (config.ContainsKey("Build") && config["Build"] != String.Empty)
                     * {
                     *  CollectorConstants.setBuild(config["Build"].ToString());
                     * }
                     * else
                     * {
                     *  CollectorConstants.setBuild("NA");
                     * }*/
                    if (config.ContainsKey("isLoadTest") && config["isLoadTest"].Contains("true"))
                    {
                        CollectorConstants.setLoadTest("true");
                    }
                    else
                    {
                        CollectorConstants.setLoadTest("false");
                    }
                    if (config.ContainsKey("markWaitTime") && config["markWaitTime"] != String.Empty)
                    {
                        CollectorConstants.setMarkWaitTime(Int32.Parse(config["markWaitTime"]));
                    }
                    else
                    {
                        CollectorConstants.setMarkWaitTime(5000);
                    }
                    if (config.ContainsKey("resourceSettleTime"))
                    {
                        CollectorConstants.setResourceSettleTime(Int32.Parse(config["resourceSettleTime"]));
                    }
                    else
                    {
                        CollectorConstants.setResourceSettleTime(2000);
                    }
                    log.Debug("Loaded initialization parameters");
                }


                if (CollectorConstants.getBeaconURL() == null || CollectorConstants.getBeaconURL() == "")
                {
                    throw new ArgumentException("Missing value for BeaconURL");
                }
                if (CollectorConstants.getClientName() == null || CollectorConstants.getClientName() == "")
                {
                    throw new ArgumentException("Missing value for ClientName");
                }
                if (CollectorConstants.getProjectName() == null || CollectorConstants.getProjectName() == "")
                {
                    throw new ArgumentException("Missing value for ProjectName");
                }
                if (CollectorConstants.getScenarioName() == null || CollectorConstants.getScenarioName() == "")
                {
                    throw new ArgumentException("Missing value for ScenarioName");
                }
                if (CollectorConstants.getUserName() == null || CollectorConstants.getUserName() == "")
                {
                    throw new ArgumentException("Missing value for UserName");
                }
                if (CollectorConstants.getPassword() == null || CollectorConstants.getPassword() == "")
                {
                    throw new ArgumentException("Missing value for Password");
                }

                if (CollectorConstants.getMarkWaitTime() == 0)
                {
                    CollectorConstants.setMarkWaitTime(5000);
                }
                if (CollectorConstants.getResourceSettleTime() == 0)
                {
                    CollectorConstants.setResourceSettleTime(2000);
                }
                if (CollectorConstants.getRelease() == null || CollectorConstants.getRelease() == string.Empty)
                {
                    CollectorConstants.setRelease(DateTime.Now.ToString("yyyyMM"));
                }
                if (CollectorConstants.getBuild() == null || CollectorConstants.getBuild() == string.Empty)
                {
                    CollectorConstants.setBuild(DateTime.Now.ToString("yyyyMMddHH"));
                }


                Dictionary <string, object> configs     = new Dictionary <string, object>();
                ICXOptimizeService          cxOpService = new CXOptimizeServiceImpl();
                CollectorConstants.setApiToken(cxOpService.getAuthToken());

                log.Debug("Created ApiToken for the session " + CollectorConstants.getApiToken());

                configs = cxOpService.getConfiguration();

                if (config.ContainsKey("Status")) // Failed to get the config
                {
                    log.Info("Cannot load configuration: " + config["Reason"]);
                    config.Clear();
                }
                else
                {
                    configs.Add("beaconUrl", CollectorConstants.getBeaconURL());
                    configs.Add("RunTime", configs.ContainsKey("runTimestamp") ? configs["runTimestamp"] :
                                (DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalMilliseconds);
                    CollectorConstants.setStaticExt(configs.ContainsKey("staticResourceExtension") ? configs["staticResourceExtension"].ToString() : "");
                    CollectorConstants.setImages(configs.ContainsKey("imageResourceExtension") ? configs["imageResourceExtension"].ToString() : "");
                    CollectorConstants.setResDurThreshold((configs.ContainsKey("resourceDurationThreshold") ? Double.Parse(configs["resourceDurationThreshold"].ToString())
                        : Double.Parse("10")));
                    configs.Add("Release", CollectorConstants.getRelease());
                    configs.Add("BuildNumber", CollectorConstants.getBuild());
                    clientConfig = configs;

                    log.Debug("Complete Configuration " + config.ToString());
                }
            }
            catch (Exception e)
            {
                log.Error("Cannot load configuration due to exception : " + e);
            }
        }
Esempio n. 2
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);
        }