Created
June 20, 2019 21:58
-
-
Save dnickless/7dcd4f8d488c791a478f76e9acba4be2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Threading; | |
using BenchmarkDotNet.Attributes; | |
using BenchmarkDotNet.Running; | |
using MongoDB.Bson; | |
namespace ConsoleApp2 | |
{ | |
class Program | |
{ | |
public static void Main(string[] args) | |
{ | |
var summary = BenchmarkRunner.Run<Test>(); | |
} | |
} | |
[DryJob] | |
[ClrJob, CoreJob] | |
[LegacyJitX86Job, LegacyJitX64Job, RyuJitX64Job, RyuJitX86Job] | |
[RPlotExporter, RankColumn] | |
[GcServer(true)] | |
public class Test | |
{ | |
private static ContactsCollection _cc; | |
private static BsonDocument _ccdoc; | |
[GlobalSetup] | |
public void Setup() | |
{ | |
_cc = new ContactsCollection(); | |
for (int i = 0; i < 1000; i++) | |
{ | |
_cc.Contacts.Add(_cc.Contacts[i % 3]); | |
} | |
_ccdoc = _cc.ToBsonDocument(); | |
} | |
[Benchmark] | |
public void SingleThreaded() | |
{ | |
_ccdoc.ToBson(); | |
} | |
[Benchmark] | |
public void MultiThreaded() | |
{ | |
var processorCount = Environment.ProcessorCount / 2; | |
Thread[] ts = new Thread[processorCount]; | |
for (int i = 0; i < processorCount; i++) | |
{ | |
ts[i] = new Thread(new ThreadStart(delegate | |
{ | |
for (int x = 0; x < 1000; x++) | |
{ | |
_ccdoc.ToBson(); | |
} | |
})); | |
} | |
for (int i = 0; i < processorCount; i++) | |
{ | |
ts[i].Start(); | |
} | |
for (int i = 0; i < processorCount; i++) | |
{ | |
ts[i].Join(); | |
} | |
} | |
} | |
// This will be serialized into a JSON Address object | |
public class Address | |
{ | |
public string Street { get; set; } | |
public string City { get; set; } | |
public string State { get; set; } | |
public string PostalCode = "99999"; // initialize properties to generate sample data | |
public Address() | |
{ | |
// or set properties in default constructor to generate sample data | |
this.Street = "4627 Sunset Ave"; this.City = "San Diego"; this.State = "CA"; this.PostalCode = "92115"; | |
} | |
} | |
// This will be serialized into a JSON Contact object | |
public class Contact | |
{ | |
public int Id { get; set; } | |
public string Name { get; set; } | |
public DateTime? BirthDate { get; set; } | |
public string Phone { get; set; } | |
public Address Address { get; set; } | |
public Contact() | |
{ | |
this.Id = 7113; this.Name = "James Norris"; this.BirthDate = new DateTime(1977, 5, 13); this.Phone = "488-555-1212"; | |
this.Address = new Address(); | |
} | |
} | |
// This will be serialized into a JSON array of Contact objects | |
public class ContactsCollection | |
{ | |
public List<Contact> Contacts { get; set; } | |
public ContactsCollection() | |
{ | |
// initialize array of objects in default constructor to generate sample data | |
this.Contacts = new List<Contact> | |
{ | |
new Contact { Id = 7113, Name = "James Norris", BirthDate = new DateTime(1977, 5, 13), Phone = "488-555-1212", | |
Address = new Address { Street = "4627 Sunset Ave", City = "San Diego", State = "CA", PostalCode = "92115" } }, | |
new Contact { Id = 7114, Name = "Mary Lamb", BirthDate = new DateTime(1974, 10, 21), Phone = "337-555-1212", | |
Address = new Address { Street = "1111 Industrial Way", City = "Dallas", State = "TX", PostalCode = "49245" } }, | |
new Contact { Id = 7115, Name = "Robert Shoemaker", BirthDate = new DateTime(1968, 2, 8), Phone ="643-555-1212", | |
Address = null } | |
}; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment