本文整理汇总了C#中ConcurrentQueue.GroupBy方法的典型用法代码示例。如果您正苦于以下问题:C# ConcurrentQueue.GroupBy方法的具体用法?C# ConcurrentQueue.GroupBy怎么用?C# ConcurrentQueue.GroupBy使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ConcurrentQueue
的用法示例。
在下文中一共展示了ConcurrentQueue.GroupBy方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: RunTest
public void RunTest(bool enableSnapshotting, int numberOfCommandsToProcess)
{
var handleTimes = new ConcurrentQueue<DispatchStats>();
var viewManager = CreateViewManager();
var commandProcessor = CreateCommandProcessor(enableSnapshotting, viewManager, handleTimes);
var stopwatch = Stopwatch.StartNew();
var lastResult = Enumerable.Range(0, numberOfCommandsToProcess)
.Select(i => commandProcessor.ProcessCommand(new IncrementRoot("bimse!")))
.Last();
viewManager.WaitUntilProcessed(lastResult, TimeSpan.FromMinutes(2)).Wait();
Console.WriteLine();
Console.WriteLine("Processing {0} commands took {1:0.0} s in total", numberOfCommandsToProcess, stopwatch.Elapsed.TotalSeconds);
Console.WriteLine();
var maxTime = handleTimes.Max(t => t.Elapsed);
var statsLines = string.Join(Environment.NewLine, handleTimes
.GroupBy(l => RoundToSeconds(l, TimeSpan.FromSeconds(10)))
.Select(g => new DispatchStats(g.Key, TimeSpan.FromSeconds(g.Average(e => e.Elapsed.TotalSeconds))))
.Select(time =>
{
var timeString = time.Elapsed.TotalSeconds.ToString("0.00").PadLeft(8);
var bar = new string('=', (int)(100.0 * (time.Elapsed.TotalSeconds / maxTime.TotalSeconds)));
return string.Concat(timeString, ": ", bar);
}));
Console.WriteLine(statsLines);
Console.WriteLine("0.00 - {0:0.00} s", maxTime.TotalSeconds);
}
示例2: should_generate_unique_ids_from_multiple_threads
public void should_generate_unique_ids_from_multiple_threads()
{
var messageIds = new ConcurrentQueue<MessageId>();
Action taskAction = () =>
{
for (var i = 0; i < 100000; ++i)
{
messageIds.Enqueue(MessageId.NextId());
}
};
var task1 = Task.Factory.StartNew(taskAction);
var task2 = Task.Factory.StartNew(taskAction);
Task.WaitAll(task1, task2);
var duplicatedMessageIds = messageIds.GroupBy(x => x.Value).Where(x => x.Count() != 1).ToList();
duplicatedMessageIds.ShouldBeEmpty();
}
示例3: RunEventReplayingTest
public void RunEventReplayingTest(bool enableSnapshotting, int numberOfEventsToGenerate)
{
SaveEvents(numberOfEventsToGenerate, "bimse!");
var handleTimes = new ConcurrentQueue<DispatchStats>();
var viewManager = CreateViewManager();
CreateCommandProcessor(enableSnapshotting, viewManager, handleTimes);
var stopwatch = Stopwatch.StartNew();
var lastResult = GetLastResult();
Console.WriteLine("Waiting for views to catch up to {0}", lastResult);
viewManager.WaitUntilProcessed(lastResult, TimeSpan.FromMinutes(5)).Wait();
Console.WriteLine();
Console.WriteLine("Processing {0} events took {1:0.0} s in total", numberOfEventsToGenerate, stopwatch.Elapsed.TotalSeconds);
Console.WriteLine();
var stats = handleTimes
.GroupBy(l => RoundToSeconds(l, TimeSpan.FromSeconds(1)))
.Select(g => new DispatchStats(g.Key, TimeSpan.FromSeconds(g.Average(e => e.Elapsed.TotalSeconds))))
.ToList();
var maxTime = stats.Any() ? stats.Max(t => t.Elapsed) : TimeSpan.Zero;
var statsLines = string.Join(Environment.NewLine, stats
.Select(time =>
{
var timeString = time.Elapsed.TotalSeconds.ToString("0.00").PadLeft(8);
var bar = GetBar(time, maxTime);
return string.Concat(timeString, ": ", bar);
}));
Console.WriteLine(statsLines);
Console.WriteLine("0.00 - {0:0.00} s", maxTime.TotalSeconds);
}
示例4: CanDistributeWorkAmongAddedWorkers
public void CanDistributeWorkAmongAddedWorkers(int numberOfMessages, int numberOfWorkerEndpoints)
{
var workerQueueNames = Enumerable
.Range(1, numberOfWorkerEndpoints)
.Select(workerNumber => string.Format("test.loadbalancer.worker.{0:00}", workerNumber))
.ToList();
Console.WriteLine(@"Load balancer test running - will send {0} messages to load balancer configured with endpoints:
{1}
",
numberOfMessages, string.Join(Environment.NewLine, workerQueueNames.Select(name => " " + name)));
var workDone = new ConcurrentQueue<WorkDone>();
foreach (var queueName in workerQueueNames)
{
StartWorkerBus(queueName, workDone);
service.AddDestinationQueue(queueName);
queuesToReset.Add(queueName);
}
service.Start();
var sender = Configure.With(TrackDisposable(new BuiltinContainerAdapter()))
.MessageOwnership(o => o.Use(this))
.Transport(t => t.UseMsmqInOneWayClientMode())
.CreateBus().Start();
var messagesToSend = Enumerable.Range(0, numberOfMessages)
.Select(id => new Work {MessageId = id})
.ToList();
messagesToSend.ForEach(sender.Send);
var waitStartTime = DateTime.UtcNow;
while (waitStartTime.ElapsedUntilNow() < TimeSpan.FromSeconds(5 + (numberOfMessages/100)))
{
Thread.Sleep(100);
if (workDone.Count >= numberOfMessages) break;
}
Thread.Sleep(2.Seconds());
workDone.Count.ShouldBe(numberOfMessages);
workDone.Select(w => w.MessageId).OrderBy(w => w)
.ShouldBe(Enumerable.Range(0, numberOfMessages));
var groupedByWorkers = workDone.GroupBy(w => w.WorkerQueueName);
Console.WriteLine(@"Messages were processed like this:
{0}", string.Join(Environment.NewLine, groupedByWorkers.Select(g => string.Format(" " + g.Key + ": " + new string('*', g.Count())))));
groupedByWorkers.Count().ShouldBe(numberOfWorkerEndpoints);
}
示例5: SendOutgoingMessages
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);
}
})
);
}
示例6: ShouldSupportUsingOneGeneratorFromMultipleThreads
public void ShouldSupportUsingOneGeneratorFromMultipleThreads()
{
// Arrange
var account = CloudStorageAccount.DevelopmentStorageAccount;
using (var testScope = new TestScope(account))
{
var store = new BlobOptimisticDataStore(account, testScope.ContainerName);
var generator = new UniqueIdGenerator(store) { BatchSize = 1000 };
const int testLength = 10000;
// Act
var generatedIds = new ConcurrentQueue<long>();
var threadIds = new ConcurrentQueue<int>();
var scopeName = testScope.IdScopeName;
Parallel.For(
0,
testLength,
new ParallelOptions { MaxDegreeOfParallelism = 10 },
i =>
{
generatedIds.Enqueue(generator.NextId(scopeName));
threadIds.Enqueue(Thread.CurrentThread.ManagedThreadId);
});
// Assert we generated the right count of ids
Assert.AreEqual(testLength, generatedIds.Count);
// Assert there were no duplicates
Assert.IsFalse(generatedIds.GroupBy(n => n).Where(g => g.Count() != 1).Any());
// Assert we used multiple threads
var uniqueThreadsUsed = threadIds.Distinct().Count();
if (uniqueThreadsUsed == 1)
Assert.Inconclusive("The test failed to actually utilize multiple threads");
}
}