Exemple #1
0
        private async Task CopyToExistingEntity(int id, WorkerProfile newWorker)
        {
            Entities.WorkerProfile existingWorker = await GetExistingWorker(id);

            existingWorker.Name = newWorker.Name;
            CopyAddress(existingWorker.Address, newWorker.Address);
            existingWorker.Skills = newWorker.Skills;
        }
Exemple #2
0
        public async Task <WorkerAccount> AddUserInformation(string authorization, WorkerProfile profile)
        {
            var token          = getTokenFromAuthorization(authorization);
            var currentAccount = GetByToken(token);

            currentAccount.WorkerProfile.Email    = profile.Email;
            currentAccount.WorkerProfile.Fullname = profile.Fullname;
            await context.SaveChangesAsync();

            return(currentAccount);
        }
        public async Task <IActionResult> UpdateWorker(int id, [FromBody] WorkerProfile worker)
        {
            worker.Id = id;
            if (workerRepository.Get(id) == null)
            {
                return(NotFound());
            }
            var savedWorker = await workerRepository.Save(worker, GetCreator(), id);

            return(CreatedAtRoute(nameof(GetWorkerById), new { id = savedWorker.Id }, savedWorker));
        }
 public Task <WorkerProfile> Save(WorkerProfile worker, string creator, int id = 0)
 {
     throw new System.NotImplementedException();
 }
 private bool HasAllSkills(WorkerProfile worker, IList <Skill> skills)
 {
     return(skills.All(s => ContainsSkill(worker.Skills, s)));
 }
Exemple #6
0
        void WorkerThread(object obj)
        {
            Thread.Sleep(1000);

            WorkerProfile workerProfile = (WorkerProfile)obj;
            List <int>    OpPercentages = workerProfile.OpRatio;
            long          IterationCount = workerProfile.TotalIteration;
            int           CurrentThreadId = workerProfile.WorkerId;
            long          OpCountPerIteration = workerProfile.OpCountPerIteration;
            long          UseAttempts = 0, DeleteAttempts = 0, AddAttempts = 0, UpdateAttempts = 0, LoadAttempts = 0;

            int ms2Tick = (int)(5 * Stopwatch.Frequency / 1000);

            OpPerformanceRecord useRec    = new OpPerformanceRecord(workerProfile.WorkerId, "USE", ms2Tick);
            OpPerformanceRecord deleteRec = new OpPerformanceRecord(workerProfile.WorkerId, "DELETE", ms2Tick);
            OpPerformanceRecord updateRec = new OpPerformanceRecord(workerProfile.WorkerId, "UPDATE", ms2Tick);
            OpPerformanceRecord addRec    = new OpPerformanceRecord(workerProfile.WorkerId, "ADD", ms2Tick);
            OpPerformanceRecord loadRec   = new OpPerformanceRecord(workerProfile.WorkerId, "LOAD", ms2Tick);

            //byte[] cellBuffer = new byte[CellSize * 3];


            long[] _TimeoutOpCount = new long[OperationCategoryCount];
            long[] _TimeOpSum      = new long[OperationCategoryCount];
            for (int i = 0; i < OperationCategoryCount; i++)
            {
                _TimeoutOpCount[i] = 0;
                _TimeOpSum[i]      = 0;
            }

            #region initialization
            int opCount = 0;
            for (int i = 0; i < OpPercentages.Count; i++)
            {
                opCount += OpPercentages[i];
            }

            CellOperationType[] OpArray = new CellOperationType[opCount];
            opCount = 0;
            for (int i = 0; i < OpPercentages.Count; i++)
            {
                for (int j = 0; j < OpPercentages[i]; j++)
                {
                    OpArray[opCount] = (CellOperationType)i;
                    opCount++;
                }
            }
            #endregion

            long garbage = 0;
            for (int currentIteration = 0; currentIteration < IterationCount; currentIteration++)
            {
                Stopwatch _sw_ = new Stopwatch();
                int       cellCountOfCurrentThread = ThreadCellIds[CurrentThreadId].Count;
                Random    tRandom = Randoms[CurrentThreadId];
                for (long opI = 0; opI < OpCountPerIteration; opI++)
                {
                    CellOperationType CurrentOpType = OpArray[tRandom.Next(opCount)];
                    int startI = tRandom.Next(cellCountOfCurrentThread);

                    int finalI = startI + tRandom.Next(10); //! try a random number of attempts
                    for (int i = startI; i < finalI; i++)
                    {
                        long cellId = ThreadCellIds[CurrentThreadId][(int)(i % cellCountOfCurrentThread)];

                        if (CurrentOpType == CellOperationType.USE)
                        {
                            UseAttempts++;
                            #region USE
                            _sw_.Restart();
                            int              size, entryIndex;
                            byte *           cellPtr;
                            ushort           cellType;
                            TrinityErrorCode eResult = Global.LocalStorage.GetLockedCellInfo(cellId, out size, out cellType, out cellPtr, out entryIndex);
                            if (eResult == TrinityErrorCode.E_SUCCESS)
                            {
                                var junk = new byte[100];

                                if (size > 0)
                                {
                                    int offset = Randoms[CurrentThreadId].Next(0, size);

                                    int delta = Randoms[CurrentThreadId].Next(0, size);

                                    if (Randoms[CurrentThreadId].NextDouble() < 0.8)
                                    {
                                        int remaining_bytes = (size - offset);
                                        if (remaining_bytes > 0)
                                        {
                                            delta = -Randoms[CurrentThreadId].Next(0, remaining_bytes);
                                        }
                                    }
                                    else if (size + delta > CellSize * 2)
                                    {
                                        delta = CellSize * 2 - size;
                                        if (delta < 0 && offset - delta > size)
                                        {
                                            delta = size - offset;
                                        }
                                    }

                                    if (delta != 0)
                                    {
                                        try{
                                            cellPtr = Global.LocalStorage.ResizeCell(cellId, entryIndex, offset, delta);
                                        }catch {
                                            Console.WriteLine($"{size}:{delta}");
                                            WorkerResizeException[CurrentThreadId] = true;
                                        }
                                    }

                                    size = size + delta;
                                    for (int c = 0; c < size; c++)
                                    {
                                        cellPtr[c] = (byte)(cellId + c);
                                    }
                                }

                                Global.LocalStorage.ReleaseCellLock(cellId, entryIndex);
                            }
                            _sw_.Stop();

                            useRec.Record(_sw_);
                            #endregion
                            continue;
                        }

                        if (CurrentOpType == CellOperationType.DELETE)
                        {
                            DeleteAttempts++;
                            #region DELETE
                            _sw_.Restart();
                            if (Global.LocalStorage.RemoveCell(cellId) == TrinityErrorCode.E_SUCCESS)
                            {
                                Interlocked.Decrement(ref DeltaCellCount[CurrentThreadId]);
                            }
                            _sw_.Stop();

                            /*
                             * if (_sw_.ElapsedMilliseconds > 10)
                             * {
                             *  _TimeoutOpCount[(int)CellOperationType.DELETE]++;
                             *  Log("Warning : A Remove elapsed : " + _sw_.ElapsedMilliseconds + " millis.");
                             * }*/
                            deleteRec.Record(_sw_);
                            #endregion
                            continue;
                        }

                        if (CurrentOpType == CellOperationType.UPDATE)
                        {
                            UpdateAttempts++;
                            #region UPDATE
                            _sw_.Restart();

                            double prob_value = Randoms[CurrentThreadId].NextDouble();

                            int cell_size = 0;
                            if (prob_value < 0.0003)
                            {
                                cell_size = Randoms[CurrentThreadId].Next(2, MaxLargeObjectSize);
                            }
                            else
                            {
                                cell_size = Randoms[CurrentThreadId].Next(0, CellSize * 2);
                            }

                            Global.LocalStorage.UpdateCell(cellId, GetCellContent(cellId, cell_size));
                            _sw_.Stop();
                            updateRec.Record(_sw_);
                            #endregion
                            continue;
                        }

                        if (CurrentOpType == CellOperationType.ADD)
                        {
                            AddAttempts++;
                            #region ADD
                            _sw_.Restart();
                            if (Global.LocalStorage.AddCell(cellId, GetCellContent(cellId, CellSize), 0, CellSize, ushort.MaxValue) == TrinityErrorCode.E_SUCCESS)
                            {
                                Interlocked.Increment(ref DeltaCellCount[CurrentThreadId]);
                            }
                            _sw_.Stop();
                            addRec.Record(_sw_);
                            #endregion
                            continue;
                        }

                        if (CurrentOpType == CellOperationType.Load)
                        {
                            LoadAttempts++;
                            #region Load
                            _sw_.Restart();

                            byte[] cellBuff;
                            Global.LocalStorage.LoadCell(cellId, out cellBuff);
                            garbage += cellBuff.Length;
                            _sw_.Stop();

                            loadRec.Record(_sw_);
                            #endregion
                            continue;
                        }
                    }
                }
            }

            Log("====WorkId : {0} all operations complete==== \n" +
                "WorkId : {0} add attempts : {1}\n" +
                "WorkId : {0} delete attempts : {2}\n" +
                "WorkId : {0} update attempts : {3}\n" +
                "WorkId : {0} cell count delta : {4}\n" +
                "WorkId : {0} garbage : {5}\n"
                ,
                CurrentThreadId, AddAttempts, DeleteAttempts, UpdateAttempts, DeltaCellCount[CurrentThreadId], garbage
                );


            lock (TimeoutOpCountLock)
            {
                for (int i = 0; i < OperationCategoryCount; i++)
                {
                    TimeoutOpCount[i] += _TimeoutOpCount[i];
                }
                deleteRec.Output();
                updateRec.Output();
                addRec.Output();
            }
        }
        public async Task <IActionResult> CreateWorker([FromBody] WorkerProfile worker)
        {
            var savedWorker = await workerRepository.Save(worker, GetCreator());

            return(CreatedAtRoute(nameof(GetWorkerById), new { id = savedWorker.Id }, savedWorker));
        }
 private void AssertHasSkill(WorkerProfile worker, string skill)
 {
     worker.Skills
     .Any(s => s.Name.Equals(skill, StringComparison.InvariantCultureIgnoreCase))
     .Should().BeTrue();
 }