Exemple #1
0
 public DbContext(string taskName, int index, LiteDatabase db, Logger logger, Stopwatch watch, ConcurrentCounter concurrent)
 {
     _taskName   = taskName;
     _index      = index;
     _db         = db;
     _logger     = logger;
     _watch      = watch;
     _concurrent = concurrent;
 }
Exemple #2
0
        public SqlDB(string taskName, LiteDatabase db, Logger logger, Stopwatch watch, ConcurrentCounter concurrent)
        {
            _taskName   = taskName;
            _db         = db;
            _logger     = logger;
            _watch      = watch;
            _concurrent = concurrent;

            _delay = watch.ElapsedMilliseconds;
        }
Exemple #3
0
        public Database(string taskName, LiteDatabase db, Logger logger, Stopwatch watch, ConcurrentCounter concurrent, int index)
        {
            _taskName   = taskName;
            _db         = db;
            _logger     = logger;
            _watch      = watch;
            _concurrent = concurrent;

            _delay = watch.ElapsedMilliseconds;

            this.Index = index;
        }
Exemple #4
0
        /// <summary>
        /// Run all methods
        /// </summary>
        public virtual void Run(TimeSpan timer)
        {
            var running    = true;
            var watch      = new Stopwatch();
            var concurrent = new ConcurrentCounter();
            var exec       = 0;
            var paused     = false;
            var waiter     = new ManualResetEventSlim();

            Console.WriteLine("Start running: " + this.GetType().Name);

            this.Timer = timer;

            // initialize database
            this.OnInit(new DbContext("OnInit", 0, _db, _logger, watch, concurrent));

            var tasks   = new List <Task>();
            var methods = this.GetType()
                          .GetMethods()
                          .Where(x => x.GetCustomAttribute <TaskAttribute>() != null)
                          .Select(x => new Tuple <MethodInfo, TaskAttribute>(x, x.GetCustomAttribute <TaskAttribute>()))
                          .ToArray();

            watch.Start();

            foreach (var method in methods)
            {
                for (var i = 0; i < method.Item2.Threads; i++)
                {
                    var index = i;

                    // create one task per method
                    tasks.Add(Task.Factory.StartNew(() =>
                    {
                        var count = 0;

                        var context = new DbContext(method.Item1.Name, index, _db, _logger, watch, concurrent);

                        // running loop
                        while (running && watch.Elapsed < timer)
                        {
                            var wait  = count == 0 ? method.Item2.Start : method.Item2.Repeat;
                            var delay = wait + this.Rnd.Next(0, method.Item2.Random);

                            Task.Delay(delay).GetAwaiter().GetResult();

                            if (running == false || watch.Elapsed > timer)
                            {
                                break;
                            }

                            if (paused)
                            {
                                waiter.Wait();
                            }

                            try
                            {
                                if (_synced)
                                {
                                    Monitor.Enter(_db);
                                }

                                method.Item1.Invoke(this, new object[] { context });

                                exec++;
                            }
                            catch (TargetInvocationException ex)
                            {
                                running = false;

                                Console.WriteLine($"ERROR {method.Item1.Name} : {ex.InnerException.Message}");
                            }
                            catch (Exception ex)
                            {
                                running = false;

                                Console.WriteLine($"ERROR {method.Item1.Name} : {ex.Message}");
                            }
                            finally
                            {
                                if (_synced)
                                {
                                    Monitor.Exit(_db);
                                }
                            }

                            count++;
                        }
                    }));
                }
            }

            // progress task
            tasks.Add(Task.Factory.StartNew(() =>
            {
                while (running && watch.Elapsed < timer)
                {
                    Console.WriteLine(string.Format("{0:00}:{1:00}:{2:00}: {3}",
                                                    watch.Elapsed.Hours,
                                                    watch.Elapsed.Minutes,
                                                    watch.Elapsed.Seconds,
                                                    exec));

                    Task.Delay(10000).GetAwaiter().GetResult();

                    if (paused)
                    {
                        waiter.Wait();
                    }
                }
            }));

            // pause tasks
            tasks.Add(Task.Factory.StartNew(() =>
            {
                while (running && watch.Elapsed < timer)
                {
                    Task.Delay(250).GetAwaiter().GetResult();

                    if (Console.KeyAvailable == false)
                    {
                        continue;
                    }

                    var key = Console.ReadKey(true);

                    if (key.Key == ConsoleKey.P)
                    {
                        if (paused == false)
                        {
                            Console.WriteLine(string.Format("{0:00}:{1:00}:{2:00}: {3}",
                                                            watch.Elapsed.Hours,
                                                            watch.Elapsed.Minutes,
                                                            watch.Elapsed.Seconds,
                                                            exec));

                            Console.WriteLine("[Paused]");
                            waiter.Reset();
                            paused = true;
                            watch.Stop();
                        }
                        else
                        {
                            Console.WriteLine("[Running]");
                            paused = false;
                            waiter.Set();
                            watch.Start();
                        }
                    }
                }
            }));

            // wait finish all tasks
            Task.WaitAll(tasks.ToArray());

            // finalize database
            this.OnCleanUp(new DbContext("OnInit", 0, _db, _logger, watch, concurrent));
        }
Exemple #5
0
        /// <summary>
        /// Run all methods
        /// </summary>
        public void Run(TimeSpan timer)
        {
            var running    = true;
            var finish     = DateTime.Now.Add(timer);
            var watch      = new Stopwatch();
            var concurrent = new ConcurrentCounter();
            var exec       = 0;

            Console.WriteLine("Start running: " + this.GetType().Name);

            // setting log name
            _logger.Initialize(this.GetType().Name + "_Log");

            // initialize database
            this.OnInit(new SqlDB("OnInit", _db.Database, _logger, watch, concurrent, 0));

            var tasks   = new List <Task>();
            var methods = this.GetType()
                          .GetMethods()
                          .Where(x => x.GetCustomAttribute <TaskAttribute>() != null)
                          .Select(x => new Tuple <MethodInfo, TaskAttribute>(x, x.GetCustomAttribute <TaskAttribute>()))
                          .ToArray();

            watch.Start();

            foreach (var method in methods)
            {
                for (var i = 0; i < method.Item2.Tasks; i++)
                {
                    var index = i;

                    // create one task per method
                    tasks.Add(Task.Factory.StartNew(() =>
                    {
                        var count = 0;

                        // running loop
                        while (running && DateTime.Now < finish)
                        {
                            var wait  = count == 0 ? method.Item2.Delay : method.Item2.Wait;
                            var delay = wait + _rnd.Next(0, method.Item2.Random);
                            var name  = method.Item2.Tasks == 1 ? method.Item1.Name : method.Item1.Name + "_" + index;

                            var sql = new SqlDB(name, _db.Database, _logger, watch, concurrent, index);

                            Task.Delay(delay).GetAwaiter().GetResult();

                            try
                            {
                                method.Item1.Invoke(this, new object[] { sql });

                                exec++;
                            }
                            catch (Exception ex)
                            {
                                running = false;

                                Console.WriteLine($"ERROR {method.Item1.Name} : {ex.Message}");
                            }

                            count++;
                        }
                    }));
                }
            }

            // progress task
            tasks.Add(Task.Factory.StartNew(() =>
            {
                while (running && DateTime.Now < finish)
                {
                    Task.Delay(10000).GetAwaiter().GetResult();

                    Console.WriteLine(string.Format("{0:00}:{1:00}:{2:00}: {3}",
                                                    watch.Elapsed.Hours,
                                                    watch.Elapsed.Minutes,
                                                    watch.Elapsed.Seconds,
                                                    exec));
                }
            }));


            // wait finish all tasks
            Task.WaitAll(tasks.ToArray());
        }