Esempio n. 1
0
        static void RunEnumerablesImportExamples()
        {
            Console.WriteLine("RunEnumerablesImportExamples()");

            Console.WriteLine("File read started");
            Stopwatch sw = Stopwatch.StartNew();

            var companies = ExcelIO.ReadExcelFile1().ToList();

            //var companies = ExcelIO.ReadExcelFile2(Common.ExcelDataPath, 2, 3).ToList();

            sw.Stop();
            Common.WriteTime(String.Format("\r\nRead file returning {0} companies total seconds: ", companies.Count), sw.Elapsed);

            //Using a file here to get the enumerable, but in a normal application this would come from any collection
            Console.WriteLine("\r\nImport started");
            sw = Stopwatch.StartNew();

            EnumerablesDataImport.EnumerableImport1(companies);
            //EnumerablesDataImport.EnumerableImport2(companies);

            sw.Stop();
            Common.WriteTime("Import total seconds: ", sw.Elapsed);
            Console.WriteLine("\r\nPress any key to continue.");
            Console.ReadKey(true);
        }
Esempio n. 2
0
        static void RunParallelImportExamples()
        {
            /* This example demonstrates that multiple inserts can hit the same destination table at the same time.
             * While the insert process slows down, they do run. The slowdown is to be expected.
             *
             * Normally I would NOT recommend doing parallelism for data imports. This however demonstrates that multiple
             * imports could be instantiated from different users of a web site.
             */
            Console.WriteLine("RunParallelImportExamples()");

            var companies = Common.GetCompanies().ToList();

            Console.WriteLine("\r\nImport started");
            Stopwatch sw = Stopwatch.StartNew();

            //apply a max threads count
            var options = new ParallelOptions()
            {
                MaxDegreeOfParallelism = 8
            };

            int    loopCount    = 50;
            double totalSeconds = 0;
            double minSeconds   = 99999;
            double maxSeconds   = 0;

            Parallel.For(0, loopCount, options, (x) =>
            {
                Console.WriteLine("Import {0} started", x);
                Stopwatch sw2 = Stopwatch.StartNew();

                //CsvDataImport.CsvImport1(Common.CsvDataPath);
                //cant parallel the same excel file. The first time the file is opened will cause the file to be locked
                //ExcelDataImport.ExcelImport1(Common.ExcelDataPath);
                EnumerablesDataImport.EnumerableImport2(companies.ToArray());

                sw2.Stop();
                Common.WriteTime(String.Format("Import {0} finished. total seconds: ", x), sw2.Elapsed);

                //lock this code area so multiple threads can not run it at the same time
                lock (_lockObj)
                {
                    minSeconds    = Math.Min(minSeconds, sw2.Elapsed.TotalSeconds);
                    maxSeconds    = Math.Max(maxSeconds, sw2.Elapsed.TotalSeconds);
                    totalSeconds += sw2.Elapsed.TotalSeconds;
                }
            });

            sw.Stop();
            Console.WriteLine("\r\nSummary:");
            Common.WriteMessage("Minimum (seconds): ", minSeconds);
            Common.WriteMessage("Maximum (seconds): ", maxSeconds);
            Common.WriteMessage("Average (seconds): ", totalSeconds / Convert.ToDouble(loopCount));
            Common.WriteMessage("Total (seconds): ", sw.Elapsed.TotalSeconds);
            Common.WriteMessage("Row Count: ", String.Format("{0:n0}", loopCount * companies.Count()));
            Console.WriteLine("\r\nPress any key to continue.");
            Console.ReadKey(true);
        }