public void TestSimpleParallelHttpPostRequests() { var entity = new PerformanceEntity(); entity.Id = "1234567890123456789012345678901234"; entity.Key = "1234567"; entity.Revision = "1234567"; entity.Name = "Mohamad Abu Bakar"; entity.IcNumber = "1234567-12-3444"; entity.Department = "IT Department"; entity.Height = 1234; entity.DateOfBirth = new DateTime(2015, 1, 27, 3, 33, 3); entity.Salary = 3333; var jsonEntity = JSON.ToJSON(entity); var threadCount = 10; var tasks = new Task[threadCount]; Stopwatch stopwatch = Stopwatch.StartNew(); for (int i = 0; i < threadCount; i++) { tasks[i] = Task.Factory.StartNew(() => { int messages_sent_by_one_task = 0; while(messages_sent_by_one_task < 100) { SimpleHttpPostCreateDocument( "http://localhost:8529/_db/" + Database.TestDatabaseGeneral + "/_api/document?collection=" + Database.TestDocumentCollectionName, jsonEntity ); messages_sent_by_one_task++; } }); } while (tasks.Any(t => !t.IsCompleted)) { } Console.WriteLine("Elapsed time [s]: {0}", stopwatch.Elapsed.TotalMilliseconds / 1000); }
/// <summary> /// /// </summary> /// <param name="ImgSec"></param> /// <param name="ColorForgive"></param> /// <param name="ColorDetail"></param> /// <param name="MinMargin"></param> /// <returns></returns> internal static Child[] ScanImageSection(ImageSection ImgSec, int ColorForgive, int ColorDetail, int MinMargin, Child Parent = null) { #if GUIOUTPUT mScannedPx = 0; int MaxSize = ImgSec.Width * ImgSec.Height; frmImgSecDisplay DisplayOut = null; //DisplayOut.Show(); //Master.sInvokable.Invoke(Master.sAddable, null => {return new frmImgSecDisplay();}, DisplayOut); MakeForm NewDisp = delegate() { return new frmImgSecDisplay(); }; AssignForm AssDisp = delegate(System.Windows.Forms.Form ToAssign) { DisplayOut = (frmImgSecDisplay)ToAssign; }; Master.sInvokable.Invoke(Master.sAddable, new object[] { NewDisp, AssDisp }); #endif //Break the image into ImageSections List<ImageSection> Sections = new List<ImageSection>(); ImageSectionFactory Factory; for (int y = 0; y < ImgSec.Height; y++) { for (int x = 0; x < ImgSec.Width; x++) { if (ImgSec.GetPixel(x, y).A == byte.MaxValue) { Factory = new ImageSectionFactory(new Point(x, y), ColorForgive, ColorDetail #if GUIOUTPUT ,DisplayOut #endif ); Sections.Add(Factory.Recognize(ref ImgSec, x, y)); #if GUIOUTPUT UpdateDisplay((mScannedPx += Factory.SelectedPixels().Count()), MaxSize, 0); #endif } } } Sections.RemoveAll(imsec => imsec == null); //Figure out if it just selected the whole thing. If so, cancel the scan if (Sections.Count == 1 && Sections[0].Size == ImgSec.Size) return new Child[0]; //Figure out which ones of those are too small and should be discounted for (int remove = 0; remove < Sections.Count; remove++) { if (Sections[remove].PixelsUsed().Length <= MinMargin) Sections.RemoveAt(remove); } Sections.TrimExcess(); // Once that's done, turn the remaining Image Sections into Children Child[] Children = new Child[Sections.Count]; #if MULTITHREAD Task<Child>[] Tasks = new Task<Child>[Sections.Count]; #endif for (int i = 0; i < Sections.Count; i++) { #if MULTITHREAD Tasks[i] = new Task<Child>(s => Child.FromSection((Child)((object[])s)[0], (ImageSection)((object[])s)[1]), new object[]{Parent, Sections[i]}); Tasks[i].Start(); #else Children[i] = Child.FromSection(Parent, Sections[i]); #endif #if GUIOUTPUT UpdateDisplay(MaxSize, MaxSize, 1); #endif } #if MULTITHREAD try { while (Tasks.Any(t => !t.IsCompleted)) Tasks.First(t => !t.IsCompleted).Wait(); } catch (NullReferenceException) { } for (int i = 0; i < Tasks.Length; i++) { Children[i] = Tasks[i].Result; } #endif return Children; }
private static void EvaluateFormula(FormulaTree formulaTree, Range[] ranges, Size areaSize, int threadsCount, float[] evaluatedValuesBuffer) { Stopwatch evaluationStopwatch = new Stopwatch(); evaluationStopwatch.Start(); if (evaluatedValuesBuffer.Length != areaSize.Square) throw new ArgumentException("Result buffer size isn't equal to ranges area size.", "evaluatedValuesBuffer"); int xCount = areaSize.Width; int yCount = areaSize.Height; if (threadsCount < 1) threadsCount = 1; ranges = ranges.Select((r, i) => new Range(r.Start, r.End, i % 2 == 0 ? areaSize.Width : areaSize.Height)).ToArray(); const int progressSteps = 100; using (ProgressReporter.CreateScope(progressSteps)) { int steps = 0; double lastProgress = 0; double progress = 0; ProgressObserver progressObserver = new ProgressObserver(p => progress = p.Progress); Task[] tasks = new Task[threadsCount]; int yStepCount = yCount/threadsCount; for (int i = 0; i < threadsCount; i++) { int li = i; tasks[i] = Task.Run(() => { FormulaTree ft = li == 0 ? formulaTree : FormulaTreeSerializer.Deserialize(FormulaTreeSerializer.Serialize(formulaTree)); int yStart = li * yStepCount; ProgressReporter.Subscribe(progressObserver); ft.EvaluateRangesIn2DProjection(ranges, xCount, yStart, yStepCount, evaluatedValuesBuffer); }); } while (tasks.Any(t => t.Status != TaskStatus.RanToCompletion && t.Status != TaskStatus.Faulted && t.Status != TaskStatus.Canceled)) { Task.WaitAny(tasks, 100); double progressCopy = progress; int inc = (int)((progressCopy - lastProgress) * progressSteps); if (inc > 0) { for (int i = 0; i < inc && steps < progressSteps; i++) { ProgressReporter.Increase(); steps++; } lastProgress = progress; } } Task.WaitAll(); } //double x = 1; //double y = 2; //double z = 3; //double w = 4; //double x1 = -1; //double y1 = -2; //double z1 = -3; //double w1 = -4; //Stopwatch stopwatch2 = new Stopwatch(); //stopwatch2.Start(); //double[] arr = new double[Ranges[0].Count*Ranges[1].Count]; //for (int i = 0; i < arr.Length; i++) //{ // arr[i] = Math.Sqrt((Math.Sin(x) * Math.Sin(y) + Math.Sin(z) * Math.Sin(w)) * (Math.Sin(x1) * Math.Sin(y1) + Math.Sin(z1) * Math.Sin(w1))); // x += 0.1; // y += 0.1; // z += 0.1; // w += 0.1; // x1 += 0.3; // y1 += 0.3; // z1 += 0.3; // w1 += 0.3; //} //stopwatch2.Stop(); evaluationStopwatch.Stop(); }
/// <summary> /// Gets the outcome for a full round of responses from all the clusters. /// </summary> /// <param name="responsePromises">Promises fot the responses for a particular grain from all of the clusters in the multi-cluster network</param> /// <param name="grainId">The ID of the grain that we want to know its owner status</param> /// <param name="logger">The logger in case there is useful information to log.</param> /// <returns>The outcome of aggregating all of the responses. The task will complete as soon as it has enough responses to make a determination, even if not all of the clusters responded yet.</returns> public static Task<GlobalSingleInstanceResponseOutcome> GetOutcomeAsync(Task<RemoteClusterActivationResponse>[] responsePromises, GrainId grainId, Logger logger) { if (responsePromises.Any(t => t == null)) throw new ArgumentException("All response promises should have been initiated", nameof(responsePromises)); var details = new GlobalSingleInstanceResponseTracker(responsePromises, grainId, logger); return details.Task; }