public void BurstResultsPath()
        {
            //arrange
            string pathToJtlFile    = "chauncee test/2021/06/14/20210614T1755279470Zresults/results.jtl";
            string demoJtlFileName  = "results.jtl";
            string expectedTestName = "chauncee test";
            string expectedTestRun  = "20210614T1755279470Zresults";

            using var csvJtl = new CsvJtl(pathToJtlFile);
            //act
            using Stream jtlStream             = File.OpenRead(demoJtlFileName);
            using StreamReader jtlStreamReader = new StreamReader(jtlStream);
            csvJtl.InitJtlReader(jtlStreamReader);
            csvJtl.ReadNextCsvLine();
            var csvRow  = csvJtl.GetCsvRow();
            var csvDict = csvRow as IDictionary <string, object>;

            foreach (var row in csvDict)
            {
                Console.WriteLine($"Key:{row.Key},value:{row.Value}");
            }
            ;
            //assert
            Assert.AreEqual(expectedTestName, csvRow.TestPlan);
            Assert.AreEqual(expectedTestRun, csvRow.TestRun);
        }
Beispiel #2
0
        public void SendAllRows()
        {
            //arrange
            var secrets = new ConfigurationBuilder()
                          .AddUserSecrets <Settings>()
                          .Build();
            string sqlConnectionString = secrets["JtlReportingDatabase"];
            string pathToJtlFile       = "chauncee test/2021/06/14/20210614T1755279470Zresults/results.jtl";
            string demoJtlFileName     = "results.jtl";
            string expectedTestName    = "chauncee test";
            string expectedTestRun     = "20210614T1755279470Zresults";

            using var csvJtl      = new CsvJtl(pathToJtlFile);
            using var jtlCsvToSql = new JtlCsvToSql(sqlConnectionString);
            //act
            using Stream jtlStream             = File.OpenRead(demoJtlFileName);
            using StreamReader jtlStreamReader = new StreamReader(jtlStream);
            csvJtl.InitJtlReader(jtlStreamReader);
            jtlCsvToSql.DeleteReport(csvJtl.TestPlan, csvJtl.TestRun);
            int i = 0;
            int j = 0;

            while (csvJtl.ReadNextCsvLine())
            {
                var csvRow = csvJtl.GetCsvRow();
                jtlCsvToSql.AddJtlRow(csvRow);
                if (i > 10000)
                {
                    Console.WriteLine($"Sending {i} records");
                    jtlCsvToSql.CommitBatch();
                    i = 0;
                }
                //try
                //{
                //    jtlCsvToSql.AddJtlRow(csvRow);
                //}
                //catch (Exception e)
                //{
                //    Console.WriteLine($"Skipping line {e.ToString()}");
                //    System.Diagnostics.Debugger.Break();
                //}
                i++;
                j++;
            }
            jtlCsvToSql.AddReport(csvJtl.TestPlan, csvJtl.TestRun, csvJtl.TestStartTime);
            //assert
            bool reportWasFiled = JtlCsvToSql.ReportAlreadyProcessed(csvJtl.TestPlan, csvJtl.TestRun, sqlConnectionString);

            jtlCsvToSql.DeleteReport(csvJtl.TestPlan, csvJtl.TestRun);
            Assert.IsTrue(j > 1000);
            Assert.IsTrue(reportWasFiled);
        }
        public void ReadJtlFile()
        {
            //arrange
            string demoJtlFileName = "results.jtl";

            using var csvJtl = new CsvJtl();
            //act
            using Stream jtlStream             = File.OpenRead(demoJtlFileName);
            using StreamReader jtlStreamReader = new StreamReader(jtlStream);
            csvJtl.InitJtlReader(jtlStreamReader);
            int i = 0;

            while (csvJtl.ReadNextCsvLine())
            {
                var csvRow = csvJtl.GetCsvRow();
                i++;
            }
            //assert
            Assert.IsTrue(i > 100);
        }
        public void AddElapsedTime()
        {
            //arrange
            string demoJtlFileName = "results.jtl";

            using var csvJtl = new CsvJtl();
            //act
            using Stream jtlStream             = File.OpenRead(demoJtlFileName);
            using StreamReader jtlStreamReader = new StreamReader(jtlStream);
            csvJtl.InitJtlReader(jtlStreamReader);
            int i = 0;
            int elapsedMilliseconds = 0;

            while (csvJtl.ReadNextCsvLine())
            {
                var csvRow = csvJtl.GetCsvRow();
                elapsedMilliseconds = csvRow.ElapsedMS;
                i++;
            }
            //assert
            Console.WriteLine($"elapsed milliseconds was {elapsedMilliseconds}");
            Assert.IsTrue(elapsedMilliseconds > 0);
        }
        void AddResultsToTheQueue(QueueClient queueClient, string storageConnectionString, string sqlConnectionString)
        {
            logger.LogInformation("Checking to see if there are any messages to process.");

            logger.LogInformation("Adding all of the results to the queue");
            BlobContainerClient jmeterResultsContainer = new BlobContainerClient(storageConnectionString, "jmeterresults");

            foreach (var jmeterResultsBlobItem in jmeterResultsContainer.GetBlobs())
            {
                if (jmeterResultsBlobItem.Name.EndsWith("results.jtl", StringComparison.OrdinalIgnoreCase))
                {
                    string testPlan = CsvJtl.ExtractTestPlan(jmeterResultsBlobItem.Name);
                    string testRun  = CsvJtl.ExtractTestRun(jmeterResultsBlobItem.Name);
                    logger.LogInformation($"Checking if test run {testRun} for test plan {testPlan} has already been added to Sql.");
                    if (!JtlCsvToSql.ReportAlreadyProcessed(testPlan, testRun, sqlConnectionString))
                    {
                        logger.LogInformation($"There is no report for test run {testRun} for test plan {testPlan}. Sending a message.");
                        logger.LogInformation($"Adding {jmeterResultsBlobItem.Name}");
                        var resultsJtlBlobPathBytes = Encoding.UTF8.GetBytes(jmeterResultsBlobItem.Name);
                        queueClient.SendMessage(Convert.ToBase64String(resultsJtlBlobPathBytes));
                    }
                }
            }
        }
        void SendResultsToSql(QueueClient queueClient, string storageConnectionString, string sqlConnectionString)
        {
            while (queueClient.PeekMessages().Value.Length > 0)
            {
                logger.LogInformation("Checking for new messages");
                // Get the next message
                QueueMessage[] retrievedMessages = queueClient.ReceiveMessages();
                if (retrievedMessages == null || retrievedMessages.Length == 0)
                {
                    logger.LogInformation("No messages to process.");
                }
                else
                {
                    string resultsJtlBlobPath = null;
                    string csvFileName        = $"{Guid.NewGuid()}.csv";
                    try
                    {
                        resultsJtlBlobPath = Encoding.UTF8.GetString(Convert.FromBase64String(retrievedMessages[0].Body.ToString()));
                        logger.LogInformation($"Downloading {resultsJtlBlobPath}");
                        BlobClient blobClient = new BlobClient(storageConnectionString, "jmeterresults", resultsJtlBlobPath);
                        blobClient.DownloadTo(csvFileName);
                        // Delete the message
                        queueClient.DeleteMessage(retrievedMessages[0].MessageId, retrievedMessages[0].PopReceipt);
                        logger.LogInformation($"Opening JTL FILE {resultsJtlBlobPath}");

                        using var csvJtl = new CsvJtl(resultsJtlBlobPath);
                        logger.LogInformation("Connecting to the Sql reporting dB");
                        using var jtlCsvToSql = new JtlCsvToSql(sqlConnectionString);
                        csvJtl.InitJtlReader(csvFileName);
                        if (!JtlCsvToSql.ReportAlreadyProcessed(csvJtl.TestPlan, csvJtl.TestRun, sqlConnectionString))
                        {
                            logger.LogInformation($"Deleting existing report for test plan {csvJtl.TestPlan} and test run {csvJtl.TestRun}");
                            jtlCsvToSql.DeleteReport(csvJtl.TestPlan, csvJtl.TestRun);
                            logger.LogInformation("Sending results to SQL Server");
                            int i = 0;
                            while (csvJtl.ReadNextCsvLine())
                            {
                                var csvRow = csvJtl.GetCsvRow();
                                try
                                {
                                    if (i > 10000)
                                    {
                                        logger.LogInformation($"Committing {i} rows to sql for {csvJtl.TestRun}");
                                        jtlCsvToSql.CommitBatch();
                                        i = 0;
                                    }
                                    jtlCsvToSql.AddJtlRow(csvRow);
                                    i++;
                                }
                                catch (Exception e)
                                { logger.LogWarning($"Skipping line {e.ToString()}"); }
                            }
                            logger.LogInformation("Successfully added rows to database.  Now adding test report");
                            jtlCsvToSql.AddReport(csvJtl.TestPlan, csvJtl.TestRun, csvJtl.TestStartTime);
                        }
                        else
                        {
                            logger.LogInformation($"The test run {csvJtl.TestRun} for test plan {csvJtl.TestPlan} is already in sql.  Skipping.");
                        }
                    }
                    catch (Exception e)
                    {
                        logger.LogError(e.ToString());
                        logger.LogWarning($"Adding report back to the queue for path: {resultsJtlBlobPath}");
                        var resultsJtlBlobPathBytes = Encoding.UTF8.GetBytes(resultsJtlBlobPath);
                        queueClient.SendMessage(Convert.ToBase64String(resultsJtlBlobPathBytes));
                    }
                    finally
                    {
                        File.Delete(csvFileName);
                    }
                }
            }
        }