Skip to content

Instantly share code, notes, and snippets.

@plentysmart
Last active August 29, 2015 14:09
Show Gist options
  • Save plentysmart/93775424af5bafe7c254 to your computer and use it in GitHub Desktop.
Save plentysmart/93775424af5bafe7c254 to your computer and use it in GitHub Desktop.
BatchInsert - timestamp test
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Auth;
using Microsoft.WindowsAzure.Storage.Table;
using Xunit;
namespace AzureTableBatchOperation
{
public class CustomerEntity : TableEntity
{
public CustomerEntity(string lastName, string id)
{
this.PartitionKey = lastName;
this.RowKey = id;
}
public CustomerEntity() { }
public string LargeContent { get; set; }
}
public class BatchInsertTest
{
private CloudTable table;
private static Random random = new Random((int)DateTime.Now.Ticks);
public BatchInsertTest()
{
var storageAccount =
new CloudStorageAccount(
new StorageCredentials("account",
"key"),
true);
//var storageAccount = CloudStorageAccount.DevelopmentStorageAccount;
var tableClient = storageAccount.CreateCloudTableClient();
var tableName = "TestCustomerTable" + random.Next();
var customertable = tableClient.GetTableReference(tableName);
if (customertable.Exists())
{
customertable.Delete();
Thread.Sleep(100000);
}
this.table = tableClient.GetTableReference(tableName);
this.table.Create();
}
[Fact]
public void BatchInsert_100Items_AllShouldHaveTheSameTimestamp()
{
var batchSize = 100;
var contentSize = 30*1024;
TestBatchInsert(batchSize, contentSize);
}
[Fact]
public void BatchInsert_FewItemsWithSmallContent_AllShouldHaveTheSameTimestamp()
{
var batchSize = 3;
var contentSize = 1;
TestBatchInsert(batchSize, contentSize);
}
private void TestBatchInsert(int batchSize, int contentSize)
{
TableBatchOperation batchOperation = new TableBatchOperation();
for (int i = 0; i < batchSize; i++)
{
var customer = new CustomerEntity("Smith", Guid.NewGuid().ToString());
customer.LargeContent = RandomString(contentSize); // 64kb limit
batchOperation.Insert(customer);
}
table.ExecuteBatch(batchOperation);
var query = new TableQuery<CustomerEntity>();
var result = table.ExecuteQuery(query).ToList();
Assert.Equal(batchSize, result.Count());
var firstTimestamp = result.First().Timestamp;
var distinctTimestamps = result.Select(x => x.Timestamp).Distinct();
foreach (var distinctTimestamp in distinctTimestamps)
{
Debug.WriteLine(distinctTimestamp.Ticks);
}
Assert.True(result.TrueForAll(x => x.Timestamp == firstTimestamp));
table.DeleteIfExists();
}
private string RandomString(int size)
{
StringBuilder builder = new StringBuilder();
char ch;
for (int i = 0; i < size; i++)
{
ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));
builder.Append(ch);
}
return builder.ToString();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment