示例#1
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);
        }
        private void DeleteReportsFromDatabase(String testPlan, string testRun)
        {
            // Making connection to database
            var sqlConnectionString = Environment.GetEnvironmentVariable("JtlReportingDatabase");

            // Checking if test plan and test run is in database
            logger.LogInformation("Connecting to the Sql reporting dB");
            using var jtlCsvToSql = new JtlCsvToSql(sqlConnectionString);
            if (JtlCsvToSql.ReportAlreadyProcessed(testPlan, testRun, sqlConnectionString))
            {
                logger.LogInformation($"Test run {testRun} for test plan {testPlan} found in database.");
                jtlCsvToSql.DeleteReport(testPlan, testRun);
                logger.LogInformation($"Removing Test run {testRun} for test plan {testPlan} from the database.");
            }
            else
            {
                logger.LogWarning($"Test run {testRun} for test plan {testPlan} not found in database.");
            }
        }
        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);
                    }
                }
            }
        }