public async Task ReadTaskAsync() { if (_periodicReadRateCalculator == null) { _periodicReadRateCalculator = PeriodicTaskFactory.Run(CalculateReadRate, TimeSpan.FromSeconds(5), _periodicReadRateCalculatorCts.Token); } var keyCount = 0; lock (_lock) { keyCount = _keys.Count; } Task[] tasks = new Task[keyCount]; var keys = _keys.GetRange(0, _taskPool); while (keys.Count > 0) { keys = _keys.GetRange(0, Math.Min(_keys.Count, _taskPool)); int i = 0; foreach (var key in keys) { tasks[i++] = Task.Factory.StartNew(async() => { var response = await _databaseClient.SendReadRequestAsync(key); if (!response.IsSuccessStatusCode) { Console.WriteLine($"{key} - {response.StatusCode}"); } else { Interlocked.Increment(ref _readCount); } }); } } await Task.WhenAll(tasks); }
public async Task WriteTaskAsync() { if (_periodicWriteRateCalculator == null) { _periodicWriteRateCalculator = PeriodicTaskFactory.Run(CalculateWriteRate, TimeSpan.FromSeconds(5), _periodicWriteRateCalculatorCts.Token); } Task[] tasks = new Task[_taskPool]; var rand = new Random(); for (int i = 0; i < _taskPool; i++) { var key = Guid.NewGuid().ToString(); var value = rand.Next(Int32.MaxValue).ToString(); tasks[i] = Task.Factory.StartNew(async() => { var response = await _databaseClient.SendWriteRequestAsync(key, value); if (!response.IsSuccessStatusCode) { Console.WriteLine($"{key} - {response.StatusCode}"); } else { lock (_lock) { _keys.Add(key); } Interlocked.Increment(ref _writeCount); } }); } await Task.WhenAll(tasks); }