private void WriteAlot_Async(string testName, int numPartitions, int iterations, int batchSize) { output.WriteLine("Iterations={0}, Batch={1}, Partitions={2}", iterations, batchSize, numPartitions); List <Task> promises = new List <Task>(); Stopwatch sw = Stopwatch.StartNew(); for (int i = 0; i < iterations; i++) { string partitionKey = PartitionKey; if (numPartitions > 1) { partitionKey += (i % numPartitions); } string rowKey = i.ToString(CultureInfo.InvariantCulture); UnitTestDynamoDBTableData dataObject = new UnitTestDynamoDBTableData(); dataObject.PartitionKey = partitionKey; dataObject.RowKey = rowKey; dataObject.StringData = rowKey; var promise = manager.UpsertEntryAsync(UnitTestDynamoDBStorage.INSTANCE_TABLE_NAME, DynamoDBStorageTests.GetKeys(dataObject), DynamoDBStorageTests.GetValues(dataObject)); promises.Add(promise); if ((i % batchSize) == 0 && i > 0) { Task.WhenAll(promises); promises.Clear(); output.WriteLine("{0} has written {1} rows in {2} at {3} RPS", testName, i, sw.Elapsed, i / sw.Elapsed.TotalSeconds); } } Task.WhenAll(promises); sw.Stop(); output.WriteLine("{0} completed. Wrote {1} entries to {2} partition(s) in {3} at {4} RPS", testName, iterations, numPartitions, sw.Elapsed, iterations / sw.Elapsed.TotalSeconds); }
public async Task DynamoDBDataManager_UpsertItemAsync() { var expression = "attribute_not_exists(PartitionKey) AND attribute_not_exists(RowKey)"; var toPersist = GenerateNewData(); toPersist.StringData = "Create"; await manager.PutEntryAsync(UnitTestDynamoDBStorage.INSTANCE_TABLE_NAME, GetValues(toPersist, true), expression); toPersist.StringData = "Replaced"; await manager.UpsertEntryAsync(UnitTestDynamoDBStorage.INSTANCE_TABLE_NAME, GetKeys(toPersist), GetValues(toPersist)); var persisted = await manager.ReadSingleEntryAsync(UnitTestDynamoDBStorage.INSTANCE_TABLE_NAME, GetKeys(toPersist), response => new UnitTestDynamoDBTableData(response)); Assert.Equal(persisted.StringData, "Replaced"); Assert.True(persisted.ETag == 0); //Yes, ETag didn't changed cause we didn't persisted.StringData = "Updated"; var persistedEtag = persisted.ETag; expression = $"ETag = :OldETag"; persisted.ETag++; //Increase ETag var expValues = new Dictionary <string, AttributeValue> { { ":OldETag", new AttributeValue { N = persistedEtag.ToString() } } }; await manager.UpsertEntryAsync(UnitTestDynamoDBStorage.INSTANCE_TABLE_NAME, GetKeys(persisted), GetValues(persisted), expression, expValues); persisted = await manager.ReadSingleEntryAsync(UnitTestDynamoDBStorage.INSTANCE_TABLE_NAME, GetKeys(toPersist), response => new UnitTestDynamoDBTableData(response)); Assert.Equal(persisted.StringData, "Updated"); Assert.NotEqual(persistedEtag, persisted.ETag); //Now ETag changed cause we did it await Assert.ThrowsAsync <ConditionalCheckFailedException>(async() => { await manager.UpsertEntryAsync(UnitTestDynamoDBStorage.INSTANCE_TABLE_NAME, GetKeys(toPersist), GetValues(toPersist), expression, expValues); }); }