Пример #1
0
        public bool UpdateTask(GeneratorTask task, int users)
        {
            if (users < 1)
            {
                throw new ArgumentException("users < 1");
            }

            lock (this)
            {
                if (!_tasks.ContainsKey(task))
                {
                    throw new ArgumentException("task doesn`t exist in _tasks");
                }

                int usersDiff = users - _tasks[task];

                if (users + usersDiff > VirtualUsers - _currentUsers)
                {
                    return(false);
                }

                Send(new UpdateTask()
                {
                    TaskGroup    = task.TaskGroup,
                    VirtualUsers = users
                });

                _tasks[task]   = users;
                _currentUsers += usersDiff;
                ServerState.GeneratorsUpdated.Set();
                return(true);
            }
        }
Пример #2
0
        public static bool AddTask(TaskData taskData, string owner, out uint taskGroup, Client client)
        {
            if (taskData == null)
            {
                throw new NullReferenceException("taskData is null");
            }

            if (taskData.Duration < 1)
            {
                throw new ArgumentException("Duration < 1");
            }

            if (taskData.RequestDuration < 0)
            {
                throw new ArgumentException("RequestDuration < 0");
            }

            if (taskData.VirtualUsers < 1)
            {
                throw new ArgumentException("VirtualUsers < 1");
            }

            if (taskData._URLs.Count < 1)
            {
                throw new ArgumentException("URLs is empty");
            }

            if (owner == null)
            {
                throw new NullReferenceException("owner is null");
            }

            if (client == null)
            {
                throw new NullReferenceException("client is null");
            }

            lock (_generators)
            {
                if (taskData.VirtualUsers > _infCapacity - _infCapacityReserved)
                {
                    taskGroup = 0;
                    return(false);
                }
                else
                {
                    taskGroup             = _lastTaskGroupNumber++;
                    _infCapacityReserved += taskData.VirtualUsers;

                    GeneratorTask task = new GeneratorTask(taskData.VirtualUsers, taskData.Timeout,
                                                           taskData.RequestDuration, taskData.Duration, taskData.Strategy, taskData._URLs, taskGroup, owner, client);

                    _tasks.Add(task);
                    TasksUpdated.Set();
                    return(true);
                }
            }
        }
Пример #3
0
        public static void RemoveTask(GeneratorTask task)
        {
            if (task == null)
            {
                throw new NullReferenceException("task is null");
            }

            if (!_tasks.Contains(task))
            {
                throw new ArgumentException("_tasks list doesn`t contain task");
            }

            lock (_generators)
            {
                _infCapacityReserved -= task.VirtualUsers;
            }
        }
Пример #4
0
        public void RemoveTask(GeneratorTask task)
        {
            lock (this)
            {
                if (!_tasks.ContainsKey(task))
                {
                    throw new ArgumentException("task doesn`t exist in _tasks");
                }

                int activeUsers = _tasks[task];

                Send(new UpdateTask()
                {
                    TaskGroup    = task.TaskGroup,
                    VirtualUsers = 0
                });

                _tasks.Remove(task);
                _currentUsers -= activeUsers;
                ServerState.GeneratorsUpdated.Set();
            }
        }
Пример #5
0
        public bool AddTask(GeneratorTask task, int users)
        {
            if (users < 1)
            {
                throw new ArgumentException("users < 1");
            }

            if (task == null)
            {
                throw new NullReferenceException("task is null");
            }

            lock (this)
            {
                if (users > VirtualUsers - _currentUsers)
                {
                    return(false);
                }

                string[] array = new string[task.URLs.Count];

                task.URLs.CopyTo(array, 0);

                Send(new AddTask()
                {
                    TaskGroup       = task.TaskGroup,
                    VirtualUsers    = users,
                    Timeout         = task.Timeout,
                    RequestDuration = task.RequestDuration,
                    URLs            = array
                });

                _tasks.Add(task, users);
                _currentUsers += users;
                ServerState.GeneratorsUpdated.Set();
                return(true);
            }
        }
Пример #6
0
        public static void HandleTaskInfo(Client client, object data)
        {
            if (!client.Authenticated)
            {
                return;
            }

            RequestTaskInfo info = (RequestTaskInfo)data;
            GeneratorTask   task = ServerState.Tasks.ElementAtOrDefault((int)info.TaskGroup - 1);

            if (task == null)
            {
                return;
            }

            if (task.Owner != client.Email)
            {
                return;
            }

            string[] URLs   = new string[task.URLs.Count];
            Point[]  points = new Point[task.Points.Count];

            task.URLs.CopyTo(URLs, 0);
            task.Points.CopyTo(points, 0);

            client.Send(new TaskInfoClient()
            {
                URLs            = new List <string>(URLs),
                VirtualUsers    = task.VirtualUsers,
                Timeout         = task.Timeout,
                RequestDuration = task.RequestDuration,
                Duration        = task.Duration,
                Strategy        = task.Strategy,
                Points          = new List <Point>(points),
                State           = task.State
            });
        }
Пример #7
0
        public static void RemoveGenerator(Generator generator)
        {
            lock (_generators)
            {
                if (_generators.Contains(generator))
                {
                    if (generator.Tasks != null)
                    {
                        GeneratorTask[] tasks = new GeneratorTask[generator.Tasks.Keys.Count];

                        generator.Tasks.Keys.CopyTo(tasks, 0);

                        foreach (var task in tasks)
                        {
                            task.Abort();
                        }
                    }

                    _generators.Remove(generator);
                    _infCapacity -= generator.VirtualUsers;
                    GeneratorsUpdated.Set();
                }
            }
        }
Пример #8
0
        public static void Distribute(GeneratorTask task, int users)
        {
            int currentUsers = 0;

            foreach (var generator in ServerState.Generators)
            {
                if (generator.Tasks.ContainsKey(task))
                {
                    currentUsers += generator.Tasks[task];
                }
            }

            int diff = users - currentUsers;

            if (diff > 0)
            {
                int added     = 0;
                int addon     = diff / ServerState.Generators.Count;
                int remainder = diff % ServerState.Generators.Count;

                var free = ServerState.Generators.Where(obj => !obj.Tasks.ContainsKey(task))
                           .OrderByDescending(obj => obj.VirtualUsers - obj.CurrentUsers);

                var executing = ServerState.Generators.Where(obj => obj.Tasks.ContainsKey(task))
                                .OrderBy(obj => obj.Tasks[task]);

                var sorted = free.Concat(executing);

                for (int i = 0; i < sorted.Count() && added < diff; i++)
                {
                    var current = sorted.ElementAt(i);

                    if (current.Tasks.ContainsKey(task))
                    {
                        int toAdd = addon + (remainder > 0 ? remainder-- : 0);

                        if (current.UpdateTask(task, current.Tasks[task] + toAdd))
                        {
                            added += toAdd;
                        }
                    }
                    else
                    {
                        int toAdd = addon + (remainder > 0 ? remainder-- : 0);

                        if (current.AddTask(task, toAdd))
                        {
                            added += toAdd;
                        }
                    }
                }
            }
            else if (diff < 0)
            {
                int i = 0;
                List <Generator> generators = new List <Generator>();

                foreach (var generator in ServerState.Generators)
                {
                    if (generator.Tasks.ContainsKey(task))
                    {
                        generators.Add(generator);
                    }
                }

                int[] generatorsUpdatedLoading = new int[generators.Count()];

                for (int j = 0; j < generators.Count; j++)
                {
                    generatorsUpdatedLoading[j] = generators[j].Tasks[task];
                }

                do
                {
                    for (int j = 0; j < generators.Count && i != -diff; j++)
                    {
                        generatorsUpdatedLoading[j]--;
                        i++;
                    }
                } while (i != -diff);

                for (int j = 0; j < generatorsUpdatedLoading.Length; j++)
                {
                    if (generatorsUpdatedLoading[j] < 1)
                    {
                        generators[j].RemoveTask(task);
                    }
                    else
                    {
                        generators[j].UpdateTask(task, generatorsUpdatedLoading[j]);
                    }
                }
            }
        }