public void Destroy() { Inner?.Clear(); if (_addQueue?.Any() ?? false) { KeyValuePair <T, TV> item; while (_addQueue.TryDequeue(out item)) { continue; } } if (_updateQueue?.Any() ?? false) { KeyValuePair <T, TV> item; while (_updateQueue.TryDequeue(out item)) { continue; } } if (_removeQueue?.Any() ?? false) { T item; while (_removeQueue.TryDequeue(out item)) { continue; } } if (_onCycleEventQueue?.Any() ?? false) { OnCycleDoneDelegate item; while (_onCycleEventQueue.TryDequeue(out item)) { continue; } } Inner = null; _addQueue = null; _updateQueue = null; _removeQueue = null; _onCycleEventQueue = null; _onAdd = null; _onUpdate = null; _onRemove = null; _onCycleDone = null; }
public void Can_Use_Any_Method_to_Determine_if_Items_Are_In_the_Queue() { var queue = new ConcurrentQueue<string>(); for (int i = 0; i <= 4; i++) { var msg = string.Format("I am item {0}", i); queue.Enqueue(msg); } Assert.AreEqual(true, queue.Any()); }
static async Task WaitForErrorEventAsync(ConcurrentQueue <EventWrittenEventArgs> events) { DateTime startTime = DateTime.UtcNow; while (!events.Any(e => e.EventName == "ErrorMessage")) { if (DateTime.UtcNow.Subtract(startTime) > TimeSpan.FromSeconds(30)) { throw new TimeoutException("Timeout waiting for error event"); } await Task.Delay(100); } }
private void Enqueue(double percentComplete, TimeSpan duration) { if (duration < TimeSpan.FromSeconds(0.1) && _samples.Any()) { return; } _samples.Enqueue(new ProgressSampleUnit { DateSampled = DateTime.Now, Duration = duration, PercentComplete = percentComplete }); }
/// <summary> /// 线程序号 /// </summary> /// <param name="index"></param> private void DownloadThread(int index) { while (m_taskQueue.Any()) { if (m_taskQueue.TryDequeue(out var mapLine)) { } else { // 获取失败则重试 Thread.Sleep(1000); } } }
/// <summary> /// Serialises the given <see cref="Packet"/> using the current <see cref="PacketConverter"/> and queues it to be sent /// to the network. /// </summary> /// <param name="packet">The <see cref="Packet"/> to be sent across the network.</param> /// <param name="instance">The <see cref="object"/> instance which sent the packet.</param> /// <param name="ignoreWriteLock">Currently unused. :(</param> internal void Send(Packet packet, object instance, bool ignoreWriteLock) { //Ensure that everyone is aware of that packetType. if (!typeByte.ContainsKey(packet.GetType()) || pendingUnknownPackets.Any(p => p.Item1.GetType().Assembly.Equals(packet.GetType().Assembly))) { AddExternalPackets(packet.GetType().Assembly); pendingUnknownPackets.Enqueue(new Tuple <Packet, object>(packet, instance)); Send(new AddPacketTypeRequest(packet.GetType().Assembly.FullName)); return; //Wait till we receive green light } sendPackets.Enqueue(new Tuple <Packet, object>(packet, instance)); dataAvailableEvent.Set(); }
async Task SendOutgoingMessages(ConcurrentQueue <OutgoingMessage> outgoingMessages, ITransactionContext context) { if (!outgoingMessages.Any()) { return; } var client = GetClientFromTransactionContext(context); var messagesByDestination = outgoingMessages .GroupBy(m => m.DestinationAddress) .ToList(); await Task.WhenAll( messagesByDestination .Select(async batch => { var entries = batch .Select(message => { var transportMessage = message.TransportMessage; var headers = transportMessage.Headers; return(new SendMessageBatchRequestEntry { Id = headers[Headers.MessageId], MessageBody = GetBody(transportMessage.Body), MessageAttributes = CreateAttributesFromHeaders(headers), DelaySeconds = GetDelaySeconds(headers) }); }) .ToList(); var destinationUrl = GetDestinationQueueUrlByName(batch.Key, context); var request = new SendMessageBatchRequest(destinationUrl, entries); var response = await client.SendMessageBatchAsync(request); if (response.Failed.Any()) { var failed = response.Failed.Select(f => new AmazonSQSException($"Failed {f.Message} with Id={f.Id}, Code={f.Code}, SenderFault={f.SenderFault}")); throw new AggregateException(failed); } }) ); }
public async Task <int> CreateAsync(CreateProductRepositoryModel model) { var id = _data.Any() ? _data.Max(o => o.Id) + 1 : 1; var toAdd = new ProductRepositoryModel { Id = id, MPN = model.MPN, Title = model.Title, Price = model.Price }; _data.Enqueue(toAdd); return(await Task.FromResult(id)); }
private void Process() { // Each iteration here we'll call a round while (true) { // Process any requests that have come in that want to perform blocking work on the processor thread. ProcessDispatches(); var wasItemDequeued = false; foreach (var queue in queues) { if (queue.IsAvailable && (!queue.cancellationToken.IsCancellationRequested || stopReason.IsCancellable(this, queue.Type))) { var queueItem = queue.Dequeue(); wasItemDequeued = true; queue.Activate(queueItem); Task.Run(() => queueItem.Execute().ContinueWith(_ => Dispatch(processor => queue.Deactivate(queueItem)))); } else { queue.MarkWaiting(); } } if (!wasItemDequeued && !dispatches.Any()) { if (stopReason != null) { break; } if (queues.All(x => x.IsIdle)) { Log("All queues idle, triggering idle task"); idled.SetResult(null); idled = new TaskCompletionSource <object>(); } // Wait for either a new item to be enqueued (waiter) or for the cancellation token to be triggered Log("Round finished, waiting"); waiting.SetResult(null); waiting = new TaskCompletionSource <object>(); waiter.WaitOne(); } } Debug.Assert(stopped != null); stopReason = null; stopped.SetResult(null); }
private void WorkOnEventDoneQueue() { if (_onCycleEventQueue.Any()) { return; } OnCycleDoneDelegate item; while (_onCycleEventQueue.TryDequeue(out item)) { item(); } }
/// <summary> /// Получить список страниц с их текстом в base64. /// </summary> /// <param name="pdfPath">Путь к файлу.</param> /// <param name="bookId">ИД книги, к которой пренадлежит страница.</param> /// <param name="progressAction">Устанавливает прогресс загрузки книги.</param> /// <returns>Список страниц.</returns> public static IEnumerable <Page> GetTextLayerWithPages(string pdfPath, Guid bookId, Action <double> progressAction) { BitMiracle.Docotic.LicenseManager.AddLicenseData("5P66M-TH5YU-KMPKP-ZU4U3-SSBP0"); var pages = new ConcurrentQueue <Page>(); var reader = new PdfReader(pdfPath); var numberOfPages = reader.NumberOfPages; var exceptions = new ConcurrentQueue <Exception>(); Parallel.For(1, numberOfPages + 1, new ParallelOptions { MaxDegreeOfParallelism = Environment.ProcessorCount * 4 }, i => { try { string text; lock (Lock) { text = PdfTextExtractor.GetTextFromPage(reader, i, new LocationTextExtractionStrategy()).Normalize(); } if (string.IsNullOrEmpty(text)) { using var pdfDocument = new PdfDocument(pdfPath); var page = pdfDocument.Pages[i - 1]; page.Rotation = PdfRotation.None; var recognizedPageText = RecognizePageText(page); pages.Enqueue(new Page(i, bookId, GetStringInBase64(recognizedPageText))); } else { pages.Enqueue(new Page(i, bookId, GetStringInBase64(text.Trim()))); } progressAction?.Invoke((double)1 / numberOfPages); } catch (Exception e) { exceptions.Enqueue(e); } }); if (exceptions.Any()) { throw new AggregateException(exceptions); } return(pages); }
/// <summary> /// Call initialize for all plugins /// </summary> /// <returns>return the list of failed to init plugins or null for none</returns> public static void InitializePlugins(IPublicAPI api) { API = api; var failedPlugins = new ConcurrentQueue <PluginPair>(); Parallel.ForEach(AllPlugins, pair => { try { var milliseconds = Stopwatch.Debug($"|PluginManager.InitializePlugins|Init method time cost for <{pair.Metadata.Name}>", () => { pair.Plugin.Init(new PluginInitContext { CurrentPluginMetadata = pair.Metadata, API = API }); }); pair.Metadata.InitTime += milliseconds; Log.Info($"|PluginManager.InitializePlugins|Total init cost for <{pair.Metadata.Name}> is <{pair.Metadata.InitTime}ms>"); } catch (Exception e) { Log.Exception(nameof(PluginManager), $"Fail to Init plugin: {pair.Metadata.Name}", e); pair.Metadata.Disabled = true; failedPlugins.Enqueue(pair); } }); _contextMenuPlugins = GetPluginsForInterface <IContextMenu>(); foreach (var plugin in AllPlugins) { if (IsGlobalPlugin(plugin.Metadata)) { GlobalPlugins.Add(plugin); } // Plugins may have multiple ActionKeywords, eg. WebSearch plugin.Metadata.ActionKeywords .Where(x => x != Query.GlobalPluginWildcardSign) .ToList() .ForEach(x => NonGlobalPlugins[x] = plugin); } if (failedPlugins.Any()) { var failed = string.Join(",", failedPlugins.Select(x => x.Metadata.Name)); API.ShowMsg($"Fail to Init Plugins", $"Plugins: {failed} - fail to load and would be disabled, please contact plugin creator for help", "", false); } }
private async Task SendResponse() { if (Responses.Any() && Responses.TryDequeue(out Response response)) { using (TcpClient client = new TcpClient(response.TargetIP, portNumber)) { var serialized = new JavaScriptSerializer().Serialize(response); var encoded = Encoding.ASCII.GetBytes(serialized); NetworkStream stream = client.GetStream(); await stream.WriteAsync(encoded, 0, encoded.Length); } } }
public virtual TModel Update(int id, TModel entity) { var exceptions = new ConcurrentQueue <Exception>(); Validate(id, exceptions); if (exceptions.Any()) { throw new AggregateException(exceptions); } Context.Entry(entity).State = EntityState.Modified; Context.Set <TModel>().Attach(entity); return(entity); }
public void Subscriber(Action <Message> handle) { messageQueue = new ConcurrentQueue <Message>(); retryQueue = new ConcurrentQueue <Message>(); queueTask = Task.Factory.StartNew(() => { while (true) { Thread.Sleep(1); while (messageQueue.Any()) { if (messageQueue.TryDequeue(out Message message)) { try { handle(message); } catch (DomainException ex) { } catch (Exception ex) { //TODO:日志 //retryQueue.Enqueue(message); throw; } } } for (var i = 0; i < retryQueue.Count; i++) { try { if (retryQueue.TryPeek(out Message message)) { handle(message); retryQueue.TryDequeue(out message); } } catch { //TODO:日志 } } } }); }
public async Task <TimeSpan> SubmitRequestsAsync(int numRequests, int tps) { var delayBetweenRequests = TimeSpan.FromMilliseconds(1000.0 / tps); var requestTimes = new ConcurrentQueue <TimeSpan>(); var responseTasks = new List <Task>(); for (int i = 0; i < numRequests; i++) { var stopwatch = Stopwatch.StartNew(); var request = new TestRequest { RequestNumber = i, StartTime = DateTimeOffset.UtcNow }; Debug.WriteLine("Submitting request {0}", i); var responseTask = serviceClient.SubmitRequestAsync(request, requestTimeout); Task continueWith = responseTask.ContinueWith(task => { var testResponse = task.Result; var now = DateTimeOffset.UtcNow; var processingTime = now - testResponse.StartTime; Debug.WriteLine("Request {0} took {1}", testResponse.RequestNumber, processingTime); requestTimes.Enqueue(processingTime); }); responseTasks.Add(continueWith); responseTasks.Add(responseTask); stopwatch.Stop(); var delayTime = delayBetweenRequests - stopwatch.Elapsed; if (delayTime > TimeSpan.Zero) { await Task.Delay(delayTime); } } await Task.WhenAll(responseTasks.ToArray()); if (!requestTimes.Any()) { throw new Exception("Failed to retrieve any results. Are you sure your queue names are configured correctly?"); } double averageMs = requestTimes.Select(t => t.TotalMilliseconds).Average(); return(TimeSpan.FromMilliseconds(averageMs)); }
/// <summary> /// 是否发送信息到zipkin服务器 /// </summary> /// <returns></returns> private bool ShouldSendQueuedSpansOverWire() { //return 有追踪信息 &&(信息数量超过成批提交的数量 || 任务已取消 || 超过MAX_NUMBER_OF_POLLS次没有新信息) return (#if NET20 || NET35 serializableSpans.Count > 0 && (serializableSpans.Count >= maxBatchSize #else serializableSpans.Any() && (serializableSpans.Count() >= maxBatchSize #endif || spanProcessorTaskFactory.IsTaskCancelled() || subsequentPollCount > MAX_NUMBER_OF_POLLS)); }
/// <summary> /// 释放 /// </summary> /// <param name="obj">释放的对象</param> public static void Release(this Object obj) { if (obj == null) { return; } if (_connQueue.Count < _maxSize) { if (_connQueue.Any(p => p == obj)) { //抛出此异常,表明此对象池实现不正确 throw new Exception("同一个对象被同时使用"); } _connQueue.Enqueue(obj); } }
/// <summary> /// 处理队列数据 /// </summary> /// <param name="count">数量</param> private void RunConcurrentQueue(int count) { if (currendQueue.Any() && currendQueue.Count >= count) { for (int i = 0; i < currendQueue.Count; i++) { JokeData info = null; currendQueue.TryDequeue(out info); var update = Db.Updateable(info).UpdateColumns(s => new { s.Title, s.Contents, s.PointsCount, s.ZanCount, s.UpdateBy, s.UpdateDate }).WhereColumns(it => new { it.Title }); if (update.ExecuteCommand() <= 0) { Db.Insertable(info).ExecuteReturnBigIdentity(); } } } }
private void StartToInvokeFunction() { var cts = new CancellationToken(); Task.Factory.StartNew(async() => { while (ActionQueue.Any()) { ActionQueue.TryDequeue(out Action action); await Task.Factory.StartNew(action, cts); await Task.Delay(100, cts); } _runingAction = false; }, cts); TaskCompleted?.Invoke(this, EventArgs.Empty); }
public string FlushToString() { var values = new List <decimal>(); while (_queue.Any() && _queue.TryDequeue(out decimal val)) { values.Add(val); } if (values.Any()) { return(string.Join("+", values) + _unit); } return(string.Empty); }
private void WriteTask() { if (!IsWrite) { Task.Factory.StartNew(() => { IsWrite = true; while (!IsDisposed) { //处理输出过长(截断到一半) UIConsoleCut(); if (Lines.Any() && Lines.Count > 0) { try { StringBuilder sb = new StringBuilder(); for (int i = 0; i < Lines.Count; i++) { if (Lines.TryDequeue(out string s)) { ConsoleLine++; //显示行号 if (CBNumber.Checked) { sb.Append($"【{ConsoleLine}】"); } //显示时间 if (CBTime.Checked) { sb.Append($"【{DateTimeConvert.StandardString(DateTime.Now)}】"); } sb.Append(s); sb.Append(Environment.NewLine); } } if (sb.Length > 0) { UIConsole(sb.ToString()); } } catch { } } Thread.Sleep(WriteInterval); } }); } }
private static bool SaveItems() { var items = new List <Saveable>(); var cannotSave = new List <Saveable>(); while (ChangedItems.Count > 0) { if (ChangedItems.TryDequeue(out var saveable)) { if (saveable.CanSave) { items.Add(saveable); } else { cannotSave.Add(saveable); } } } if (items.Any()) { foreach (var saveable in items.GroupBy(item => item.SaveGroupId, item => item)) { if (saveable.Key == null || saveable.Count() == 1) { foreach (var saveable1 in saveable) { saveable1.Save(); } } else { saveable.First().SaveGroup(saveable); } } foreach (var savable in cannotSave) { AddItemToChanges(savable); } } _lastSave = DateTime.Now; return(!ChangedItems.Any()); }
private async Task MonthStatsCheckerLoop() { var token = _cancellationTokenSource.Token; _lastBatchMonthStatsEnqueued = DateTime.Now; while (!token.IsCancellationRequested) { if (_monthStatsQueue.TryDequeue(out var stock)) { try { if (await GetMonthStats(stock)) { await ReportStatsCheckerProgress(); } else { await Task.Delay(500); EnqueueStockForMonthStatsIfExpired(stock); } } catch (Exception ex) { _logger.LogError(ex, "Ошибка при загрузке истории"); EnqueueStockForMonthStatsIfExpired(stock); } } if (DateTime.Now.Subtract(_lastBatchMonthStatsEnqueued).TotalSeconds > 31) { _mainModel.Stocks .Select(s => s.Value) .Where(s => !_monthStatsQueue.Contains(s) && s.MonthStatsExpired && s.Price > 0) .OrderByDescending(s => Math.Abs(s.DayChange)) .Take(30).ToList().ForEach(s => _monthStatsQueue.Enqueue(s)); _lastBatchMonthStatsEnqueued = DateTime.Now; } if (!_monthStatsQueue.Any()) { await Task.Delay(100); } } }
private void ProcessConfigFiles(ProjectMetadata projectMetadata, SourceInformation projectSourceInformation) { _logger.Trace("Entering"); var configFiles = _allItems .Where( x => x.SourcePath.StartsWith( projectSourceInformation.SourcePath, StringComparison.CurrentCultureIgnoreCase) && x.SourcePath.ToLower().EndsWith(".config", StringComparison.CurrentCultureIgnoreCase) && !x.SourcePath.ContainsInsensitive("packages.config") ); var exceptions = new ConcurrentQueue <Exception>(); foreach (var configFile in configFiles) { try { var filename = Path.GetFileName(configFile.SourcePath); var extension = Path.GetExtension(configFile.SourcePath); var sourceTool = GetSourceTool(); var sw = Stopwatch.StartNew(); var content = sourceTool.GetItemContent(configFile); sw.Stop(); _logger.Trace($"GetItemContent took {sw.Elapsed}"); ProcessConfigFile(projectMetadata, filename, extension, content); } catch (Exception e) { OnExceptionThrown(new ExceptionEventArgs(configFile, e)); exceptions.Enqueue(e); } } if (exceptions.Any()) { throw new AggregateException( $"Some config file processing failed for {projectSourceInformation}.", exceptions); } }
private async Task RunnerAsync(int id) { while (_queue.Any()) { if (_queue.TryDequeue(out var item)) { try { await HandleItemAsync(item, id); } catch (Exception error) { await HandleErrorAsync(item, error); } } } }
public virtual void ValidateState(bool isValid = true) { if (!NodeQueue.Any()) { throw new StateException("At least one node must be added to the router."); } if (Rules.IsFallbackEnabled == true && FallbackNodes == null) { throw new StateException("Fallback is enabled; a fallback list must be provided."); } if (Rules.IsFallbackEnabled != true && FallbackNodes != null) { throw new StateException("Cannot define a fallback if fallback is not enabled in the rules."); } }
private async Task SyncNextSite() { while (_siteQueue.Any()) { if (TokenSource.IsCancellationRequested) { return; } if (!_siteQueue.TryDequeue(out var site)) { return; } Logger.Debug($"Site: {site.Id} dequeued"); await SyncSite(site); } }
public static void ExecuteAsParallel(this IEnumerable<Action> actions) { var _exceptions = new ConcurrentQueue<Exception>(); System.Threading.CancellationToken cts = default(System.Threading.CancellationToken); try { Parallel.ForEach<Action>(actions, new ParallelOptions() { CancellationToken = cts }, a => a.Invoke()); } catch (AggregateException agex) { agex.InnerExceptions.ToList().ForEach(_exceptions.Enqueue); } if (_exceptions.Any()) throw new ApplicationException(string.Format("Error: {0}", string.Join("\r\nError: ", _exceptions.Select(e => e.Message)))); }
public void Subscriber(Action <Message> handle) { queueTask = Task.Factory.StartNew(() => { while (true) { Thread.Sleep(1); while (messageQueue.Any()) { if (messageQueue.TryDequeue(out Message message)) { handle(message); } } } }); }
private void NotifyIcon_BalloonClosing(object sender, RoutedEventArgs e) { lock (SyncRoot) { if (_notificationQueue.Any()) { Notification notification; if (_notificationQueue.TryDequeue(out notification)) { _displayQueue.Add(notification); } } else { BalloonVisible = false; } } }
public async Task Run(int name) { SpinWait spin = new SpinWait(); while (WorkQueue.Any()) { ModulusClass item; while (!WorkQueue.TryDequeue(out item)) { spin.SpinOnce(); } LogHelper.Info($"{item.Key}:{item.Value}"); Thread.Sleep(500); } LogHelper.Info($"完成{name}"); }
public static Tuple<IEnumerable<ViewDefinitionCompiledArgs>, IEnumerable<CycleCompletedArgs>> RunToCompletion(IViewExecutionOptions options) { using (var remoteViewClient = Context.ViewProcessor.CreateClient()) { var cycles = new ConcurrentQueue<CycleCompletedArgs>(); var compiles = new ConcurrentQueue<ViewDefinitionCompiledArgs>(); using (var manualResetEvent = new ManualResetEvent(false)) { var listener = new EventViewResultListener(); listener.ViewDefinitionCompiled += (sender, e) => compiles.Enqueue(e); listener.CycleCompleted += (sender, e) => cycles.Enqueue(e); listener.ProcessCompleted += (sender, e) => manualResetEvent.Set(); remoteViewClient.SetResultListener(listener); remoteViewClient.AttachToViewProcess(ViewName, options); manualResetEvent.WaitOne(); } Assert.InRange(compiles.Count, cycles.Any() ? 1 : 0, cycles.Count + 1); Assert.True(remoteViewClient.IsCompleted); return new Tuple<IEnumerable<ViewDefinitionCompiledArgs>, IEnumerable<CycleCompletedArgs>>(compiles, cycles); } }
public static void InsertData(DataSet dataSet) { if (dataSet == null) throw new ArgumentNullException("DataSet param is null"); try { //ConcurrentDictionary<string, object> cache = new ConcurrentDictionary<string, object>(); ICacheManager cache = new MemoryCacheManager(); var exceptions = new ConcurrentQueue<Exception>(); List<Task> tasks = new List<Task>(); Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); using (var container = new ModelsContainer()) { foreach (DataTable dt in dataSet.Tables) { //Task task = Task.Factory.StartNew(() => //{ try { Console.WriteLine("----------Table name is : {0}---------", dt.TableName); int cursor = 0; foreach (DataRow dr in dt.Rows) { Record record = new Record(); string brandsName = dr[0].ToString(); var brands = cache.Get<Brands>(brandsName, () => { return new Brands() { Name = brandsName }; }); record.Brands = brands; string modelsName = dr[1].ToString(); var models = cache.Get<Models>(modelsName, () => { return new Models() { Name = modelsName }; }); record.Models = models; record.City = dr[2].ToString(); string dv = dr[3].ToString().Replace(".", ""); string d = string.Format("{0}-{1}-01", dv.Substring(0, 4), dv.Substring(4, 2)).Trim(); var buyYear = cache.Get<BuyYear>(d, () => { return new BuyYear() { Time = Convert.ToDateTime(d) }; }); record.BuyYear = buyYear; d = string.Format("{0}-01-01", dr[4].ToString()); record.Both = DateTime.Parse(d); bool g = dr[5].ToString().Equals("男") ? true : false; record.Gender = Convert.ToBoolean(g); record.Address = dr[6].ToString(); record.Zip = dr[7].ToString(); container.Set<Record>().Add(record); Console.WriteLine("address {0}, cursor = {1}, threadId = {2}", record.Address, cursor, Thread.CurrentThread.ManagedThreadId); cursor++; if (cursor == 100) { cursor = 0; container.SaveChanges(); } } } catch (Exception ex) { exceptions.Enqueue(ex); } //}); //tasks.Add(task); container.SaveChanges(); } } //Task.WaitAll(tasks.ToArray()); stopwatch.Stop(); TimeSpan ts = stopwatch.Elapsed; string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}", ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds / 10); Console.WriteLine("RunTime " + elapsedTime); if (exceptions.Any()) { Console.WriteLine("Parallel have exceptions, count = {0}", exceptions.Count()); } } catch (Exception ex) { string msg = ex.OutputMessage(); Console.WriteLine("{0}", msg); } }
public async Task<TimeSpan> SubmitRequestsAsync(int numRequests, int tps) { var delayBetweenRequests = TimeSpan.FromMilliseconds(1000.0/tps); var requestTimes = new ConcurrentQueue<TimeSpan>(); var responseTasks = new List<Task>(); for (int i = 0; i < numRequests; i++) { var stopwatch = Stopwatch.StartNew(); var request = new TestRequest { RequestNumber = i, StartTime = DateTimeOffset.UtcNow }; Debug.WriteLine("Submitting request {0}", i); var responseTask = serviceClient.SubmitRequestAsync(request, requestTimeout); Task continueWith = responseTask.ContinueWith(task => { var testResponse = task.Result; var now = DateTimeOffset.UtcNow; var processingTime = now - testResponse.StartTime; Debug.WriteLine("Request {0} took {1}", testResponse.RequestNumber, processingTime); requestTimes.Enqueue(processingTime); }); responseTasks.Add(continueWith); responseTasks.Add(responseTask); stopwatch.Stop(); var delayTime = delayBetweenRequests - stopwatch.Elapsed; if (delayTime > TimeSpan.Zero) { await Task.Delay(delayTime); } } await Task.WhenAll(responseTasks.ToArray()); if (!requestTimes.Any()) { throw new Exception("Failed to retrieve any results. Are you sure your queue names are configured correctly?"); } double averageMs = requestTimes.Select(t => t.TotalMilliseconds).Average(); return TimeSpan.FromMilliseconds(averageMs); }
List<Combination> ProcessCombinations(List<Combination> combinations, int threadsCount) { combinations = combinations.Take(CombinationsToProcess).ToList(); ProcessedCombinationsResult existingResult = null; object existingObjResult = null; if (Core.MsCache.TryGet(CacheKey, ref existingObjResult)) existingResult = (ProcessedCombinationsResult)existingObjResult; List<Combination> existingCaseSet = null; if (existingResult != null && existingResult.ThreadCount == threadsCount) { existingCaseSet = existingResult.ProcessedCombinations; } else { Core.MsCache.TryRemove(CacheKey); } if (existingCaseSet != null) return existingCaseSet; // Use ConcurrentQueue to enable safe enqueueing from multiple threads. var exceptions = new ConcurrentQueue<Exception>(); var fbClient = FogBugzGateway.GetClientForParallel(); Func<object, int> action = (object obj) => { Combination combo = obj as Combination; //System.Diagnostics.Debug.WriteLine("{0} Thread: {1} Project: {2}, Milestone: {3}, StartAction", // DateTime.Now.ToString("hh:mm:ss"), Thread.CurrentThread.ManagedThreadId, // combo.ProjectName, combo.MilestoneName); var cases = FogBugzGateway.GetCases(combo.ProjectId.Value, combo.MilestoneId.Value, null, 1, fbClient); Core.MsCache.Set(ListProgressStatusKey + "_" + _cacheKey, new ProgressStatusInfo { Value = 33, Label = String.Format("Checking {0} {1} for tasks {2} of {3}", combo.ProjectName, combo.MilestoneName, combinations.IndexOf(combo) + 1, combinations.Count) }); combo.HasCases = cases.Any(c => !c.DateClosed.HasValue); //System.Diagnostics.Debug.WriteLine("{0} Thread: {1} Project: {2}, Milestone: {3}, HasCases={4}", // DateTime.Now.ToString("hh:mm:ss"), Thread.CurrentThread.ManagedThreadId, // combo.ProjectName, combo.MilestoneName, combo.HasCases); return 0; }; var factory = new TaskFactory(); int step = 0; var combinationsPerStep = threadsCount; while (true) { //Console.WriteLine("ProcessCominations Step " + step); var stepCombinations = combinations.Skip(step * combinationsPerStep).Take(combinationsPerStep).ToArray(); if (!stepCombinations.Any()) break; var tasks = new Task<int>[stepCombinations.Count()]; for (int i = 0; i < stepCombinations.Count(); i++) { tasks[i] = factory.StartNew(action, stepCombinations[i]); } //Exceptions thrown by tasks will be propagated to the main thread //while it waits for the tasks. The actual exceptions will be wrapped in AggregateException. Task.WaitAll(tasks); if (combinations.Count(c => c.HasCases) >= CombinationsToFind || stepCombinations.Count() < combinationsPerStep) break; step++; } if (exceptions.Any()) { throw new AggregateException(exceptions); } // return legal combinations var result = combinations.Where(c => c.HasCases) //combinations with at least one task //.Where(c => (c.MilestoneEndDate ?? DateTime.Now) > DateTime.Now) //moved to process method .OrderBy(c => c.MilestoneEndDate) //Sort by Milestone End Date in future .Take(CombinationsToFind) //Get top 20 .ToList(); Core.MsCache.Set(CacheKey, new ProcessedCombinationsResult { ProcessedCombinations = result, ThreadCount = threadsCount }, new TimeSpan(1, 0, 0)); return result; }
async Task SendOutgoingMessages(ConcurrentQueue<OutgoingMessage> outgoingMessages, ITransactionContext context) { if (!outgoingMessages.Any()) return; var client = GetClientFromTransactionContext(context); var messagesByDestination = outgoingMessages .GroupBy(m => m.DestinationAddress) .ToList(); await Task.WhenAll( messagesByDestination .Select(async batch => { var entries = batch .Select(message => { var transportMessage = message.TransportMessage; var headers = transportMessage.Headers; return new SendMessageBatchRequestEntry { Id = headers[Headers.MessageId], MessageBody = GetBody(transportMessage.Body), MessageAttributes = CreateAttributesFromHeaders(headers), DelaySeconds = GetDelaySeconds(headers) }; }) .ToList(); var destinationUrl = GetDestinationQueueUrlByName(batch.Key, context); var request = new SendMessageBatchRequest(destinationUrl, entries); var response = await client.SendMessageBatchAsync(request); if (response.Failed.Any()) { var failed = response.Failed.Select(f => new AmazonSQSException($"Failed {f.Message} with Id={f.Id}, Code={f.Code}, SenderFault={f.SenderFault}")); throw new AggregateException(failed); } }) ); }
public async Task<TimeSpan> SubmitRequestsAsync(int numRequests, int tps) { var delayBetweenRequests = TimeSpan.FromMilliseconds(1000.0 / tps); var requestTimes = new ConcurrentQueue<TimeSpan>(); var responseTasks = new List<Task>(); using (var client = new HttpClient()) { client.BaseAddress = serviceUri; client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); for (int i = 0; i < numRequests; i++) { var stopwatch = Stopwatch.StartNew(); var request = new TestRequest { RequestNumber = i, StartTime = DateTimeOffset.UtcNow }; Debug.WriteLine("Submitting request {0}", i); // New code: Task<HttpResponseMessage> responseTask = client.PostAsJsonAsync("api/service/", request); Task continueWith = responseTask.ContinueWith( async task => { var response = task.Result; if (response.IsSuccessStatusCode) { var testResponse = await response.Content.ReadAsAsync<TestResponse>(); var now = DateTimeOffset.UtcNow; var processingTime = now - testResponse.StartTime; Debug.WriteLine("Request {0} took {1}", testResponse.RequestNumber, processingTime); requestTimes.Enqueue(processingTime); } }); responseTasks.Add(continueWith); responseTasks.Add(responseTask); stopwatch.Stop(); var delayTime = delayBetweenRequests - stopwatch.Elapsed; if (delayTime > TimeSpan.Zero) { await Task.Delay(delayTime); } } await Task.WhenAll(responseTasks.ToArray()); } if (!requestTimes.Any()) { throw new Exception("Failed to retrieve any results. Are you sure your queue names are configured correctly?"); } double averageMs = requestTimes.Select(t => t.TotalMilliseconds).Average(); return TimeSpan.FromMilliseconds(averageMs); }