public void UpsertBlockOrSkipWithStressAsync() { BlobStorage.PutBlobAsync(ContainerName, BlobName, 0).Wait(); var array = new Task<BlobWithETag<int>>[8]; array = array .AsParallel() .Select(k => BlobStorage.UpsertBlobOrSkipAsync<int>(ContainerName, BlobName, () => 1, i => i + 1)) .ToArray(); Assert.IsFalse(array.Any(x => x.Result == null), "No skips"); var sorted = array.Select(m => m.Result.Blob) .OrderBy(i => i) .ToArray(); for (int i = 0; i < array.Length; i++) { Assert.AreEqual(i + 1, sorted[i], "Concurrency should be resolved, every call should increment by one."); } }
static void Main(string[] args) { var maxPlayers = 6; var rand = new Random(); while (true) { var numOfPlayers = rand.Next(maxPlayers / 2, maxPlayers); Task[] tasks = new Task[numOfPlayers]; for (int j = 0; j < numOfPlayers; j++) { int j1 = j; tasks[j] = Task.Factory.StartNew(() => Target(new ThreadParams() { MaxUsers=numOfPlayers, Seed = j1 * 6, State = (j1 == 0 ? (ThreadState.CreateGame) : ThreadState.JoinGame), UserName = names[j1] })); } while (tasks.Any(t => !t.IsCompleted)) { } //spin wait } }
/// <summary> /// Executes the benchmarks. /// </summary> /// <param name="numOfThreads">The num of threads.</param> /// <param name="benchmarkType">Type of the benchmark.</param> /// <param name="constructorParameters">The constructor parameters.</param> /// <returns></returns> private static BenchmarkBase[] ExecuteBenchmarks(int numOfThreads, Type benchmarkType, params object[] constructorParameters) { BenchmarkBase[] benchmarks = new BenchmarkBase[numOfThreads]; //Create threads Mutex mutex = new Mutex(true, Settings.MUTEX_NAME); #if THREADS Thread[] tasks = new Thread[numOfThreads]; #else Task[] tasks = new Task[numOfThreads]; #endif for (int i = 0; i < numOfThreads; i++) { benchmarks[i] = Activator.CreateInstance(benchmarkType, constructorParameters) as BenchmarkBase; #if THREADS tasks[i] = new Thread(new ThreadStart(benchmarks[i].ExecuteBenchmark)); #else tasks[i] = new Task(new Action(benchmarks[i].ExecuteBenchmark)); #endif tasks[i].Start(); } //Start nechmark execution mutex.ReleaseMutex(); mutex.Dispose(); //Wait for threads to complete execution #if THREADS while (tasks.Any(t => t.IsAlive)) { } #else while (tasks.Any(t => !t.IsCompleted)) { } #endif return benchmarks; }