Skip to content

Instantly share code, notes, and snippets.

@krcourville
Last active August 29, 2015 14:15
Show Gist options
  • Select an option

  • Save krcourville/8f997e93351902e32e0d to your computer and use it in GitHub Desktop.

Select an option

Save krcourville/8f997e93351902e32e0d to your computer and use it in GitHub Desktop.
A Linqpad script used to test insert performance for various scenarios
const int iterations = 10;
const int inserts = 1000000;
const string mongodbname = "perftest";
private SqlConnection sqlConnection;
private MongoDatabase mongoDb;
private List<Result> allresults = new List<Result>();
#region setup-teardown
private void Setup(){
sqlConnection = new SqlConnection(Connection.ConnectionString);
sqlConnection.Open();
var tables = new[]{
"GuidComb",
"GuidSequentialId",
"GuidNewId",
"Identity"
};
foreach (var table in tables)
{
var sql = string.Format("TRUNCATE TABLE [{0}]",table);
var cmd = sqlConnection.CreateCommand();
cmd.CommandText = sql;
cmd.ExecuteNonQuery();
}
var client = new MongoClient();
var server = client.GetServer();
server.DropDatabase(mongodbname);
mongoDb = server.GetDatabase(mongodbname);
}
private void Teardown(){
sqlConnection.Close();
sqlConnection.Dispose();
}
#endregion
void Main()
{
Setup();
TimeIt("GuidNewIdInsertTest", GuidNewIdInsertTest);
TimeIt("GuidSequentialIdTest", GuidSequentialIdTest);
TimeIt("GuidIdentityTest", GuidIdentityTest);
TimeIt("GuidCombTest",GuidCombTest);
TimeIt("MongoGuidIdTest",MongoGuidIdTest);
TimeIt("MongoGuidCombTest",MongoGuidCombTest);
TimeIt("MongoObjectIdTest", MongoObjectIdTest);
TimeIt("MongoObjectIdServerTest",MongoObjectIdServerTest);
allresults.Dump("ALLRESULTS");
Teardown();
}
#region Utils
class Result
{
public string Name { get; set; }
public int Iteration { get; set; }
public double Time { get; set; }
}
private void TimeIt(string name, Action action){
name.Dump("BEGIN");
for (int i = 0; i < iterations; i++)
{
var sw = new Stopwatch();
sw.Start();
action();
sw.Stop();
// sw.ElapsedMilliseconds.Dump(String.Format("RESULT: {0}, ITERATION: {1}", name,i));
allresults.Add( new Result{
Iteration = i,
Name = name,
Time = sw.ElapsedMilliseconds
});
}
}
#endregion
#region SQL Tests
private void GuidCombTest(){
for (int i = 0; i < inserts; i++)
{
var cmd = sqlConnection.CreateCommand();
cmd.CommandText = "INSERT [GuidComb] ([Id], [Value]) VALUES(@id, 'test')";
cmd.Parameters.AddWithValue("@id", GenerateComb());
cmd.ExecuteNonQuery();
}
}
private void GuidNewIdInsertTest(){
SqlTest("GuidNewId");
}
private void GuidSequentialIdTest(){
SqlTest("GuidSequentialId");
}
private void GuidIdentityTest(){
SqlTest("Identity");
}
private void SqlTest(string tablename){
for (int i = 0; i < inserts; i++)
{
var cmd = sqlConnection.CreateCommand();
cmd.CommandText = String.Format("INSERT [{0}] ([Value]) VALUES ('test')", tablename);
cmd.ExecuteNonQuery();
}
}
private Guid GenerateComb()
{
byte[] guidArray = Guid.NewGuid().ToByteArray();
DateTime baseDate = new DateTime(1900, 1, 1);
DateTime now = DateTime.Now;
// Get the days and milliseconds which will be used to build the byte string
TimeSpan days = new TimeSpan(now.Ticks - baseDate.Ticks);
TimeSpan msecs = now.TimeOfDay;
// Convert to a byte array
// Note that SQL Server is accurate to 1/300th of a millisecond so we divide by 3.333333
byte[] daysArray = BitConverter.GetBytes(days.Days);
byte[] msecsArray = BitConverter.GetBytes((long) (msecs.TotalMilliseconds / 3.333333));
// Reverse the bytes to match SQL Servers ordering
Array.Reverse(daysArray);
Array.Reverse(msecsArray);
// Copy the bytes into the guid
Array.Copy(daysArray, daysArray.Length - 2, guidArray, guidArray.Length - 6, 2);
Array.Copy(msecsArray, msecsArray.Length - 4, guidArray, guidArray.Length - 4, 4);
return new Guid(guidArray);
}
#endregion
#region Mongo Tests
void MongoObjectIdServerTest(){
var collection = mongoDb.GetCollection("object_id_server");
var docs = Enumerable.Range(0, inserts)
.Select (e => new BsonDocument{
{"value","test"}
});
collection.InsertBatch(docs);
}
void MongoObjectIdTest(){
var collection = mongoDb.GetCollection("object_id");
var docs = Enumerable.Range(0, inserts)
.Select (e => new BsonDocument{
{"_id", ObjectId.GenerateNewId()},
{"value","test"}
});
collection.InsertBatch(docs);
}
void MongoGuidIdTest(){
var collection = mongoDb.GetCollection("guid_id");
var docs = Enumerable.Range(0, inserts)
.Select (e => new BsonDocument{
{"_id", Guid.NewGuid().ToString()},
{"value","test"}
});
collection.InsertBatch(docs);
}
void MongoGuidCombTest(){
var collection = mongoDb.GetCollection("comb_id");
var docs = Enumerable.Range(0, inserts)
.Select (e => new BsonDocument{
{"_id", GenerateComb().ToString()},
{"value","test"}
});
collection.InsertBatch(docs);
}
#endregion
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment