Hibernating Rhinos Ltd
Oren Eini l CEO l Mobile: + 972-52-548-6969
Office: +972-4-622-7811 l Fax: +972-153-4-622-7811
--
You received this message because you are subscribed to the Google Groups "RavenDB - 2nd generation document database" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ravendb+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Hibernating Rhinos Ltd
Oren Eini l CEO l Mobile: + 972-52-548-6969
Office: +972-4-622-7811 l Fax: +972-153-4-622-7811
--
using Raven.Client.Documents;
using Raven.Client.Documents.Indexes;
using Raven.Client.Documents.Queries.Facets;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Threading.Tasks;
namespace RavenDB4RCTests
{
public class TestFacetPerformance
{
public const string NUM_FORMAT = "########0.###";
public static Dictionary<int, double> Ranges = new Dictionary<int, double>
{
{ 0, 0 },
{ 1, 1 },
{ 2, 2 },
{ 3, 3 },
{ 4, 5 },
{ 5, 10 },
{ 6, 20 },
{ 7, 30 },
{ 8, 50 },
{ 9, 100 },
{ 10, 200 },
{ 11, 300 },
{ 12, 500 },
{ 13, 1000 },
{ 14, 2000 },
{ 15, 3000 },
{ 16, 5000 },
{ 17, 10000 },
{ 18, 20000 },
{ 19, 30000 },
{ 20, 50000 },
{ 21, 100000 },
{ 22, 200000 },
{ 23, 300000 },
{ 24, 500000 },
{ 25, 1000000 },
{ 26, 2000000 },
{ 27, 3000000 },
{ 28, 5000000 },
{ 29, 10000000 },
{ 30, 20000000 },
{ 31, double.MaxValue },
};
public static async Task TestFacetPerformanceTest()
{
var documentStore = new DocumentStore
{
Urls = new[] { "http://4.live-test.ravendb.net" },
Database = "TestFacetPerformance"
};
documentStore.Initialize();
new DocIndex().Execute(documentStore);
await InitFacetDoc(documentStore);
if (await ShouldInitData(documentStore))
{
await InitializeData(documentStore);
}
using (var session = documentStore.OpenAsyncSession())
{
var sw = new Stopwatch();
sw.Start();
var query = session.Query<DocView, DocIndex>()
.AggregateBy(x => x.Value1Category)
.CountOn(x => x.Id);
var results = await query.ToListAsync();
sw.Stop();
Console.WriteLine("AggregateBy: " + sw.Elapsed.TotalMilliseconds + " ms");
}
using (var session = documentStore.OpenAsyncSession())
{
var sw = new Stopwatch();
sw.Start();
var results = await session.Query<DocView, DocIndex>()
.ToFacetsAsync("facet/docs");
sw.Stop();
Console.WriteLine("Facets: " + sw.Elapsed.TotalMilliseconds + " ms");
}
using (var session = documentStore.OpenAsyncSession())
{
var sw = new Stopwatch();
sw.Start();
var query = session.Query<DocView, DocIndex>()
.Where(x => x.Value1 > 500000 && x.Value1 < 1000000)
.AggregateBy(x => x.Value1Category)
.CountOn(x => x.Id);
var results = await query.ToListAsync();
sw.Stop();
Console.WriteLine("Filtered AggregateBy: " + sw.Elapsed.TotalMilliseconds + " ms");
}
using (var session = documentStore.OpenAsyncSession())
{
var sw = new Stopwatch();
sw.Start();
var results = await session.Query<DocView, DocIndex>()
.Where(x => x.Value1 > 500000 && x.Value1 < 1000000)
.ToFacetsAsync("facet/docs");
sw.Stop();
Console.WriteLine("Filtered Facets: " + sw.Elapsed.TotalMilliseconds + " ms");
}
Console.WriteLine("Press Enter.");
Console.ReadLine();
}
private static async Task<bool> ShouldInitData(DocumentStore documentStore)
{
using (var session = documentStore.OpenAsyncSession())
{
var doc = await session.LoadAsync<Doc>("doc/1");
return doc == null;
}
}
private static async Task InitializeData(DocumentStore documentStore)
{
var start = 0;
var batches = 1000;
Console.WriteLine("Generating data.");
var rng = new Random();
for (int batchNo = start; batchNo < start + batches; batchNo++)
{
Console.WriteLine($"{DateTime.Now.ToLongTimeString()}: Generating batch {batchNo + 1}/{batches}");
using (var session = documentStore.OpenAsyncSession())
{
for (int i = 1; i <= 1000; i++)
{
var numVals = Enumerable.Range(1, 5).ToDictionary(x => "Value" + x, _ => rng.NextDouble() < 0.9 ? (rng.NextDouble() * 100000000) : (double?)null);
await session.StoreAsync(new Doc
{
Id = "doc/" + (batchNo * 1000 + i),
NumVals = numVals,
NumCategories = numVals.ToDictionary(x => x.Key, x => CategorizeValue(x.Value)),
});
}
await session.SaveChangesAsync();
}
}
Console.WriteLine("Data generated.");
}
private static int? CategorizeValue(double? value)
{
if (value == null)
{
return null;
}
return Ranges.First(x => value < x.Value).Key;
}
private static async Task InitFacetDoc(DocumentStore documentStore)
{
using (var session = documentStore.OpenAsyncSession())
{
var doc = new FacetSetup
{
Id = "facet/docs",
Facets = new List<Facet>
{
new Facet
{
Name = "Value1_D_Range",
Mode = FacetMode.Ranges,
Ranges = new[] { "[NULL TO " + Ranges.First().Value.ToString(NUM_FORMAT, CultureInfo.InvariantCulture) + "]" }
.Concat(
Ranges.Values.Take(Ranges.Count - 2)
.Zip(Ranges.Values.Skip(1).Take(Ranges.Count - 1),
(from, to) => $"[{from.ToString(NUM_FORMAT, CultureInfo.InvariantCulture)} TO {to.ToString(NUM_FORMAT, CultureInfo.InvariantCulture)}]")
)
.Concat(new[] { "[" + Ranges.Take(Ranges.Count - 1).Last().Value.ToString(NUM_FORMAT) + " TO NULL]" })
.ToList(),
},
},
};
await session.StoreAsync(doc);
await session.SaveChangesAsync();
}
}
public class Doc
{
public string Id { get; set; }
public Dictionary<string, double?> NumVals { get; set; }
public Dictionary<string, int?> NumCategories { get; set; }
}
public class DocView
{
public string Id { get; set; }
public double? Value1 { get; set; }
public int? Value1Category { get; set; }
public double? Value2 { get; set; }
public int? Value2Category { get; set; }
public double? Value3 { get; set; }
public int? Value3Category { get; set; }
public double? Value4 { get; set; }
public int? Value4Category { get; set; }
public double? Value5 { get; set; }
public int? Value5Category { get; set; }
}
public class DocIndex : AbstractIndexCreationTask<Doc, DocView>
{
public DocIndex()
{
Map = docs =>
from doc in docs
select new
{
doc.Id,
Value1 = doc.NumVals["Value1"],
Value1Category = doc.NumCategories["Value1"],
Value2 = doc.NumVals["Value2"],
Value2Category = doc.NumCategories["Value2"],
Value3 = doc.NumVals["Value3"],
Value3Category = doc.NumCategories["Value3"],
Value4 = doc.NumVals["Value4"],
Value4Category = doc.NumCategories["Value4"],
Value5 = doc.NumVals["Value5"],
Value5Category = doc.NumCategories["Value5"],
};
StoreAllFields(FieldStorage.Yes);
}
}
}
}
To unsubscribe from this group and stop receiving emails from it, send an email to ravendb+u...@googlegroups.com.
Hibernating Rhinos Ltd
Oren Eini l CEO l Mobile: + 972-52-548-6969
Office: +972-4-622-7811 l Fax: +972-153-4-622-7811
--
You received this message because you are subscribed to the Google Groups "RavenDB - 2nd generation document database" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ravendb+unsubscribe@googlegroups.com.
Hibernating Rhinos Ltd
Oren Eini l CEO l Mobile: + 972-52-548-6969
Office: +972-4-622-7811 l Fax: +972-153-4-622-7811
--