コード例 #1
0
        public Dictionary <string, object> getConfiguration()
        {
            log.Debug("Entering  getConfiguration");
            log.Debug("Get Configuration " + CollectorConstants.getBeaconURL());
            log.Debug("Get Configuration " + CollectorConstants.getClientName());
            log.Debug("Get Configuration " + CollectorConstants.getProjectName());
            log.Debug("Get Configuration " + CollectorConstants.getScenarioName());
            String url = CollectorConstants.getBeaconURL() + "/getConfig?ClientName=" + CollectorConstants.getClientName() + "&ProjectName=" + CollectorConstants.getProjectName() + "&Scenario=" + CollectorConstants.getScenarioName() + "&updateRunID=true";

            if (CollectorConstants.getLoadTest().Equals("true"))
            {
                url = url + "&isLoadTest=true";
            }
            //Map<String, Object> result = new HashMap<String, Object>();
            //result = HttpUtils.callService(url,"GET",null,ConfigurationLoader.getApiToken());
            try
            {
                Dictionary <String, Object> results = HTTPUtils.callService(url, "GET", null, CollectorConstants.getApiToken());
                Dictionary <String, Object> jsonObj = JSONUtils.JsonStringToMap(results["response"].ToString());
                return(jsonObj);
            }
            catch (Exception e)
            {
                log.Error("Exception in parsing configuration : {}", e);
                return(null);
            }
        }
コード例 #2
0
        public static Dictionary <String, Object> extractData(String txnName, String url, IWebDriver browser, int txnStatus)
        {
            log.Debug("Started extracting data for Transaction : " + txnName + " with status " + txnStatus);
            Dictionary <String, Object> rtnValue = new Dictionary <string, object>();

            try
            {
                IJavaScriptExecutor jsExe = (IJavaScriptExecutor)browser;

                log.Debug("Check Browser Type");
                String userAgent = (String)jsExe.ExecuteScript("var nAgt = navigator.userAgent|| {}; return nAgt;");
                log.Debug("User Agent : " + userAgent);

                if (userAgent != null)
                {
                    CollectorConstants.setUserAgent(userAgent);
                    rtnValue = collateData(txnName, url, jsExe, txnStatus);
                }
                else
                {
                    /*Adding conditions to check if Client Config is null or User Agent is null*/
                    log.Debug("No performance data will be collected for " + txnName + " since UserAgent: " + userAgent + " is null");
                    rtnValue = null;
                }
            }
            catch (Exception e)
            {
                log.Error("Exception in extractData for " + txnName + " at " + e);
            }
            log.Debug("Completed extracting data for Transaction : " + txnName + " with status " + txnStatus);
            return(rtnValue);
        }
コード例 #3
0
        public string getAuthToken()
        {
            log.Debug("Entering  getAuthToken");
            String url  = CollectorConstants.getBeaconURL() + "/authToken";
            String body = "{\"username\":\"" + CollectorConstants.getUserName() + "\",\"password\":\"" + CollectorConstants.getPassword() + "\"}";

            return(HTTPUtils.callService(url, "POST", body, null)["authToken"].ToString());
        }
コード例 #4
0
        public static int StartTransaction(String txnName, BrowserWindow browser)
        {
            int rtnValue = 0;

            CollectorConstants.setScriptStartTime(Convert.ToInt64((DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalMilliseconds));
            //IJavaScriptExecutor jsExe = (IJavaScriptExecutor)browser;
            browser.ExecuteScript(CollectorConstants.clearResourceTiming());
            LOGGER.Debug("Cleared Resource Timing API data");
            return(rtnValue);
        }
コード例 #5
0
        public void HardNavigation()
        {
            // To generate code for this test, select "Generate Code for Coded UI Test" from the shortcut menu and select one of the menu items.

            CollectorConstants.setCollectorProperties("C:\\Work\\CXOptimize\\Collector\\CodedUI\\CXOptimizeCollectorTest");
            string        AppURL  = "https://www.wikipedia.org/";
            BrowserWindow Browser = new BrowserWindow();

            //CXOptimizeCollector.StartTransaction("CodedUI",Browser);
            Browser = BrowserWindow.Launch(new Uri(AppURL));
            Console.WriteLine(CXOptimizeCollector.EndTransaction("CodedUI", Browser));
        }
コード例 #6
0
        public void HardTransactionChrome()
        {
            CollectorConstants.setCollectorProperties("C:\\Work\\CXOptimize\\Collector\\SeleniumC#\\CXOptimizeCollectorTest");

            using (var driver = new ChromeDriver("C:\\Work\\CXOptimize\\Collector\\SeleniumC#\\CXOptimizeCollectorTest"))
            {
                CXOptimizeCollector.StartTransaction("Test", driver);
                driver.Navigate().GoToUrl("https://www.wikipedia.org/");
                var result = CXOptimizeCollector.EndTransaction("Test", driver);
                Assert.AreEqual("Success", result["UploadStatus"].ToString());
            }
        }
コード例 #7
0
        public void HardTransactionIE()
        {
            CollectorConstants.setCollectorProperties("C:\\Work\\CXOptimize\\Collector\\SeleniumC#\\CXOptimizeCollectorTest");
            var options = new InternetExplorerOptions();

            options.IntroduceInstabilityByIgnoringProtectedModeSettings = true;
            using (var driver = new InternetExplorerDriver("C:\\Work\\CXOptimize\\Collector\\SeleniumC#\\CXOptimizeCollectorTest", options))
            {
                CXOptimizeCollector.StartTransaction("Test", driver);
                driver.Navigate().GoToUrl("https://www.wikipedia.org/");
                var result = CXOptimizeCollector.EndTransaction("Test", driver);
                Assert.AreEqual("Success", result["UploadStatus"].ToString());
            }
        }
コード例 #8
0
        public string uploadPerformanceData(string body)
        {
            String url = CollectorConstants.getBeaconURL() + "/insertStats";
            Dictionary <String, Object> result = HTTPUtils.callService(url, "POST", body, CollectorConstants.getApiToken());

            if (result.ContainsKey("status") && result["status"].Equals("pass"))
            {
                log.Debug("Data uploaded successfully : {}" + result["response"]);
                return(result["response"].ToString());
            }
            else
            {
                if (result["reason"].Equals("JWTExpiry"))
                {
                    log.Debug("JWT Token expired.Retrying again");
                    CollectorConstants.setApiToken(getAuthToken());
                    log.Debug("New JWT Token set : " + CollectorConstants.getApiToken());
                    result = HTTPUtils.callService(url, "POST", body, CollectorConstants.getApiToken());
                    if (result["status"].Equals("pass"))
                    {
                        log.Debug("Data uploaded successfully for retry : {}" + result["response"].ToString());
                        return(result["response"].ToString());
                    }
                    else
                    {
                        log.Debug("Data uploaded failed for retry");
                        return(null);
                    }
                }
                else
                {
                    log.Debug("Data uploaded failed for first try");
                    return(null);
                }
            }
        }
コード例 #9
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);
            }
        }
コード例 #10
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);
        }
コード例 #11
0
        private static Dictionary <String, Object> callResource(Dictionary <String, Object> resData)
        {
            String resUrl   = resData["name"].ToString();
            String truncUrl = resUrl.ToLower().Split(new string[] { "\\?" }, StringSplitOptions.None)[0];

            Dictionary <String, Object> rs = new Dictionary <string, object>();
            StringBuilder sb = new StringBuilder();
            Boolean       staticResrcStatus = false;
            Boolean       isImage           = false;
            Boolean       flagSet           = false;
            String        resourceType      = "others";

            //LoggingMessage.DisplayMessage("Started Thread to get ResourceTiming : " + resUrl, LoggingMessage.MessageType.DEBUG, log);

            rs = (from x in resData select x).ToDictionary(x => x.Key, x => x.Value);

            try
            {
                if (Convert.ToDouble(resData["duration"]) <= CollectorConstants.getResDurThreshold() || Convert.ToDouble(resData["duration"]) <= 0.0)
                {
                    rs.Add("IsCached", true);
                }
                else
                {
                    rs.Add("IsCached", false);
                }
            }
            catch (Exception e)
            {            }

            foreach (String img in CollectorConstants.getImageList())
            {
                if (truncUrl.Contains(img.ToLower().Trim()))
                {
                    isImage           = true;
                    staticResrcStatus = true;
                    flagSet           = true;
                    resourceType      = img.Trim();
                    break;
                }
            }

            if (!flagSet)
            {
                foreach (String stat in CollectorConstants.getStaticList())
                {
                    if (truncUrl.Contains(stat.ToLower().Trim()))
                    {
                        staticResrcStatus = true;
                        isImage           = false;
                        resourceType      = stat.Trim();
                        break;
                    }
                }
            }

            rs.Add("IsStaticResrc", staticResrcStatus);
            rs.Add("IsImage", isImage);
            rs.Add("ResourceType", resourceType);
            rs.Add("HostName", getHostName(truncUrl));

            //if(resUrl.contains(".js") || resUrl.contains(".css") || resUrl.contains(".png") || resUrl.contains(".jpg") || resUrl.contains(".jpeg") || resUrl.contains(".gif") || resUrl.contains(".svg") || resUrl.contains(".html"))
            if (stringContainsItemFromList(resUrl, CollectorConstants.getStaticExt() + "," + CollectorConstants.getImages()))
            {
                try
                {
                    HttpWebRequest httpReq = (HttpWebRequest)WebRequest.Create(resUrl);
                    httpReq.Timeout   = 5000;
                    httpReq.UserAgent = CollectorConstants.getUserAgent();

                    if (stringContainsItemFromList(resUrl, CollectorConstants.getStaticExt() + ",.svg"))
                    {
                        httpReq.Headers["Accept-Encoding"] = "gzip,deflate,sdch";
                    }

                    using (HttpWebResponse httpRes = (HttpWebResponse)httpReq.GetResponse())
                    {
                        int statusCode = (int)httpRes.StatusCode;
                        rs.Add("Status", statusCode);
                        if (statusCode == 200)
                        {
                            string[] header = httpRes.Headers.AllKeys;

                            if (httpRes.GetResponseHeader("Last-Modified") != null)
                            {
                                rs.Add("Last-Modified", httpRes.GetResponseHeader("Last-Modified").Replace(",", ""));
                            }
                            if (httpRes.GetResponseHeader("Content-Length") != null)
                            {
                                rs.Add("Content-Length", httpRes.GetResponseHeader("Content-Length"));
                            }
                            if (httpRes.GetResponseHeader("Connection") != null)
                            {
                                rs.Add("Connection", httpRes.GetResponseHeader("Connection"));
                            }
                            if (httpRes.GetResponseHeader("Cache-Control") != null)
                            {
                                rs.Add("Cache-Control", httpRes.GetResponseHeader("Cache-Control").Replace(",", "#").Replace("=", "#"));
                            }
                            if (httpRes.GetResponseHeader("ETag") != null)
                            {
                                rs.Add("ETag", httpRes.GetResponseHeader("ETag").Replace("\"", ""));
                            }
                            if (httpRes.GetResponseHeader("Expires") != null)
                            {
                                rs.Add("Expires", httpRes.GetResponseHeader("Expires").Replace(",", ""));
                            }

                            if (stringContainsItemFromList(resUrl, CollectorConstants.getImages()))
                            {
                                if (stringContainsItemFromList(resUrl, ".svg"))
                                {
                                    if (httpRes.GetResponseHeader("Content-Encoding") != null)
                                    {
                                        rs.Add("Content-Encoding", httpRes.GetResponseHeader("Content-Encoding").Replace(",", ""));
                                    }
                                }
                                Image image = Image.FromStream(httpRes.GetResponseStream());
                                rs.Add("Height", image.Height);
                                rs.Add("Width", image.Width);
                            }
                            else
                            {
                                if (header.Contains("Content-Encoding") && stringContainsItemFromList(header[Array.IndexOf(header, "Content-Encoding")], "gzip"))
                                {
                                    try
                                    {
                                        using (GZipStream ungzippedResponse = new GZipStream(httpRes.GetResponseStream(), CompressionMode.Decompress))
                                        {
                                            using (StreamReader file = new StreamReader(ungzippedResponse, Encoding.UTF8))
                                            {
                                                String line;
                                                while ((line = file.ReadLine()) != null)
                                                {
                                                    sb.Append(line);
                                                }
                                                if (httpRes.GetResponseHeader("Content-Encoding") != null)
                                                {
                                                    rs.Add("Content-Encoding", httpRes.GetResponseHeader("Content-Encoding").Replace(",", ""));
                                                }
                                                rs.Add("OrgSize", sb.ToString().Length);
                                                if (stringContainsItemFromList(resUrl, ".js,.css"))
                                                {
                                                    rs.Add("MinfSize", compress(sb.ToString(), ref resUrl));
                                                }
                                            }
                                        }
                                    }
                                    catch { }
                                }
                                else
                                {
                                    using (StreamReader file = new StreamReader(httpRes.GetResponseStream(), Encoding.UTF8))
                                    {
                                        String line;
                                        while ((line = file.ReadLine()) != null)
                                        {
                                            sb.Append(line);
                                        }
                                    }

                                    rs.Add("OrgSize", sb.ToString().Length);
                                    if (stringContainsItemFromList(resUrl, ".js,.css"))
                                    {
                                        if (httpRes.GetResponseHeader("Content-Encoding") != null)
                                        {
                                            rs.Add("Content-Encoding", httpRes.GetResponseHeader("Content-Encoding").Replace(",", ""));
                                        }
                                        //striplen = sb.ToString().Replace("\\n| {2}|\\t|\\r", "").Length;
                                        rs.Add("MinfSize", compress(sb.ToString(), ref resUrl));
                                    }
                                }
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    LoggingMessage.DisplayMessage("PerfInsight - CallResource Error: " + ex.Message, LoggingMessage.MessageType.ERROR, log);
                }
            }

            return(rs);
        }