Created
January 28, 2013 07:35
-
-
Save tvjames/4653681 to your computer and use it in GitHub Desktop.
RavenDB_OutOfMemory_TestCase.linq
This file contains hidden or 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
<Query Kind="Program"> | |
<NuGetReference>RavenDB.Client</NuGetReference> | |
<Namespace>Newtonsoft.Json</Namespace> | |
<Namespace>Raven.Client</Namespace> | |
<Namespace>Raven.Client.Document</Namespace> | |
<Namespace>Raven.Client.Indexes</Namespace> | |
</Query> | |
void Main() | |
{ | |
var what = new []{Events.Created, Events.Cancelled, Events.Completed, Events.Actioned, Events.Skipped}; | |
var batchSize = 1024; | |
var numberOfEventsRequired = 3000000; | |
var fromDate = new DateTime(2010, 01, 01); | |
var untilDate = new DateTime(2012, 09, 13); | |
var random = new Random(); | |
var count = 0; | |
using (var documentStore = new DocumentStore() { Url = "http://localhost:8080/" }) { | |
documentStore.Initialize(); | |
while (count < numberOfEventsRequired) { | |
var events = CreateSample(batchSize, fromDate, untilDate, random, what); | |
using (var session = documentStore.OpenSession()) { | |
session.Advanced.MaxNumberOfRequestsPerSession = batchSize * 2; | |
foreach(var e in events) { | |
session.Store(e); | |
} | |
session.SaveChanges(); | |
} | |
count += batchSize; | |
Console.WriteLine("Wrote {0} events", count); | |
} | |
Console.WriteLine("Preparing indexes"); | |
IndexCreation.CreateIndexes(typeof(Events_ByDay).Assembly, documentStore); | |
} | |
} | |
class Events | |
{ | |
public const string Created = "Created"; | |
public const string Cancelled = "Cancelled"; | |
public const string Skipped = "Skipped"; | |
public const string Completed = "Completed"; | |
public const string Actioned = "Actioned"; | |
} | |
class Events_ByDay : AbstractIndexCreationTask<Event, EventAggregate> | |
{ | |
public Events_ByDay() | |
{ | |
this.Map = docs => from doc in docs | |
select new | |
{ | |
doc.A, | |
doc.B, | |
doc.What, | |
When = new DateTime(doc.When.Year, doc.When.Month, doc.When.Day, 0, 0, 0), | |
Value = 1 | |
}; | |
this.Reduce = results => from result in results | |
group result by | |
new { result.A, result.B, result.What, result.When } | |
into g | |
select new EventAggregate | |
{ | |
A = g.Key.A, | |
B = g.Key.B, | |
What = g.Key.What, | |
When = g.Key.When, | |
Value = g.Sum(x => x.Value), | |
}; | |
} | |
} | |
class Events_ByDay_Summary : AbstractIndexCreationTask<Event, SummaryAggregate> | |
{ | |
public Events_ByDay_Summary() | |
{ | |
this.Map = docs => from doc in docs | |
select new SummaryAggregate() | |
{ | |
A = doc.A, | |
B = doc.B, | |
When = new DateTime(doc.When.Year, doc.When.Month, doc.When.Day, 0, 0, 0), | |
Created = doc.What == Events.Created ? doc.Value : 0, | |
Completed = doc.What == Events.Completed ? doc.Value : 0, | |
Cancelled = doc.What == Events.Cancelled ? doc.Value : 0, | |
Skipped = doc.What == Events.Skipped ? doc.Value : 0, | |
Actioned = doc.What == Events.Actioned ? doc.Value : 0 | |
}; | |
this.Reduce = results => from result in results | |
group result by | |
new { result.A, result.B, result.When } | |
into g | |
select new | |
{ | |
A = g.Key.A, | |
B = g.Key.B, | |
When = g.Key.When, | |
Created = g.Sum(x => x.Created), | |
Completed = g.Sum(x => x.Completed), | |
Cancelled = g.Sum(x => x.Cancelled), | |
Skipped = g.Sum(x => x.Skipped), | |
Actioned = g.Sum(x => x.Actioned), | |
}; | |
} | |
} | |
class Events_ByMonth_Summary : AbstractIndexCreationTask<Event, SummaryAggregate> | |
{ | |
public Events_ByMonth_Summary() | |
{ | |
this.Map = docs => from doc in docs | |
select new SummaryAggregate() | |
{ | |
A = doc.A, | |
B = doc.B, | |
When = new DateTime(doc.When.Year, doc.When.Month, 1, 0, 0, 0), | |
Created = doc.What == Events.Created ? doc.Value : 0, | |
Completed = doc.What == Events.Completed ? doc.Value : 0, | |
Cancelled = doc.What == Events.Cancelled ? doc.Value : 0, | |
Skipped = doc.What == Events.Skipped ? doc.Value : 0, | |
Actioned = doc.What == Events.Actioned ? doc.Value : 0 | |
}; | |
this.Reduce = results => from result in results | |
group result by | |
new { result.A, result.B, result.When } | |
into g | |
select new | |
{ | |
A = g.Key.A, | |
B = g.Key.B, | |
When = g.Key.When, | |
Created = g.Sum(x => x.Created), | |
Completed = g.Sum(x => x.Completed), | |
Cancelled = g.Sum(x => x.Cancelled), | |
Skipped = g.Sum(x => x.Skipped), | |
Actioned = g.Sum(x => x.Actioned), | |
}; | |
} | |
} | |
class Events_ByYear_Summary : AbstractIndexCreationTask<Event, SummaryAggregate> | |
{ | |
public Events_ByYear_Summary() | |
{ | |
this.Map = docs => from doc in docs | |
select new SummaryAggregate() | |
{ | |
A = doc.A, | |
B = doc.B, | |
When = new DateTime(doc.When.Year, 1, 1, 0, 0, 0), | |
Created = doc.What == Events.Created ? doc.Value : 0, | |
Completed = doc.What == Events.Completed ? doc.Value : 0, | |
Cancelled = doc.What == Events.Cancelled ? doc.Value : 0, | |
Skipped = doc.What == Events.Skipped ? doc.Value : 0, | |
Actioned = doc.What == Events.Actioned ? doc.Value : 0 | |
}; | |
this.Reduce = results => from result in results | |
group result by | |
new { result.A, result.B, result.When } | |
into g | |
select new | |
{ | |
A = g.Key.A, | |
B = g.Key.B, | |
When = g.Key.When, | |
Created = g.Sum(x => x.Created), | |
Completed = g.Sum(x => x.Completed), | |
Cancelled = g.Sum(x => x.Cancelled), | |
Skipped = g.Sum(x => x.Skipped), | |
Actioned = g.Sum(x => x.Actioned), | |
}; | |
} | |
} | |
IEnumerable<Event> CreateSample(int batchSize, DateTime fromDate, DateTime untilDate, Random random, string[] what) { | |
var difference = Convert.ToInt32((untilDate - fromDate).TotalSeconds); | |
for (var i = 0; i < batchSize; i++ ) { | |
yield return new Event() { | |
ReferenceId = Guid.NewGuid().ToString("N"), | |
ReferenceIdOther = Guid.NewGuid().ToString("N"), | |
ReferenceNumber = random.Next(0, batchSize), | |
What = what[random.Next(0, what.Length)], | |
When = fromDate + TimeSpan.FromSeconds(random.Next(0, difference)), | |
A = "Type One", | |
B = "By Another" | |
}; | |
} | |
} | |
class Event { | |
public string Id {get;set;} | |
public string ReferenceId {get;set;} | |
public string ReferenceIdOther {get;set;} | |
public int ReferenceNumber {get;set;} | |
public string What {get;set;} | |
public DateTime When {get;set;} | |
public int Value {get;set;} | |
public string A {get;set;} | |
public string B {get;set;} | |
} | |
class EventAggregate { | |
public string What {get;set;} | |
public DateTime When {get;set;} | |
public int Value {get;set;} | |
public string A {get;set;} | |
public string B {get;set;} | |
} | |
class SummaryAggregate { | |
public DateTime When { get; set; } | |
public string A {get;set;} | |
public string B {get;set;} | |
public int Created { get; set; } | |
public int Completed { get; set; } | |
public int Cancelled { get; set; } | |
public int Skipped { get; set; } | |
public int Actioned { get; set; } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment