Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 231 lines (199 sloc) 5.724 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using ICSharpCode.SharpZipLib.BZip2;
using ICSharpCode.SharpZipLib.Tar;
using NLog;
using Raven.Abstractions.Indexing;
using Raven.Client.Document;
using Raven.Client.Indexes;
using Raven.Database.Extensions;

namespace Raven.Performance
{
public class Tester
{
private readonly string dataLocation;
private readonly DocumentStore store;
public List<long> MemoryUsage { get; set; }
public List<KeyValuePair<string, double>> LatencyTimes { get; set; }
public List<KeyValuePair<string, long>> LatencyInDocuments { get; set; }

private const int BatchSize = 512;
private readonly Logger logger = LogManager.GetLogger("log");

private volatile bool doneImporting;
private Process process;

public Tester(string dataLocation, Process process)
{
this.process = process;
this.dataLocation = dataLocation;

MemoryUsage = new List<long>();
LatencyTimes = new List<KeyValuePair<string, double>>();
LatencyInDocuments = new List<KeyValuePair<string, long>>();

store = new DocumentStore
{
Url = "http://localhost:8080",
DefaultDatabase = "HibernatingRhinos.TestsDatabase"
};
store.Initialize();

}

public void ClearDatabase(string serverLocation)
{
var databaseLocation = Path.Combine(serverLocation, "Tenants", "HibernatingRhinos.TestsDatabase");
IOExtensions.DeleteDirectory(databaseLocation);
}

public void AddData()
{
MemoryUsage.Clear();
doneImporting = false;
var session = store.OpenSession();
var count = 0;

logger.Info("Testing RavenDB Log");

var sp = ParseDisks(diskToAdd =>
{
session.Store(diskToAdd);
count += 1;
if (count < BatchSize)
return;

session.SaveChanges();
session = store.OpenSession();
count = 0;
});

session.SaveChanges();

logger.Info(" ");
logger.Info("Done in {0}", sp.Elapsed);
doneImporting = true;

}

private Stopwatch ParseDisks(Action<Disk> addToBatch)
{
int i = 0;
var parser = new Parser();
var buffer = new byte[1024 * 1024];// more than big enough for all files

var sp = Stopwatch.StartNew();

using (var bz2 = new BZip2InputStream(File.Open(dataLocation, FileMode.Open)))
using (var tar = new TarInputStream(bz2))
{
TarEntry entry;
while ((entry = tar.GetNextEntry()) != null)
{
if (entry.Size == 0 || entry.Name == "README" || entry.Name == "COPYING")
continue;
var readSoFar = 0;
while (true)
{
var read = tar.Read(buffer, readSoFar, ((int)entry.Size) - readSoFar);
if (read == 0)
break;

readSoFar += read;
}
// we do it in this fashion to have the stream reader detect the BOM / unicode / other stuff
// so we can read the values properly
var fileText = new StreamReader(new MemoryStream(buffer, 0, readSoFar)).ReadToEnd();
try
{
var disk = parser.Parse(fileText);
addToBatch(disk);
if (i++ % BatchSize == 0)
{
process.Refresh();
MemoryUsage.Add(process.WorkingSet64);
logger.Info("\r{0} {1:#,#} {2} ", entry.Name, i, sp.Elapsed);
}
}
catch (Exception e)
{
logger.Error("");
logger.Error(entry.Name);
logger.Error(e);
return sp;
}
}
}
return sp;
}

public void CreateAllIndexes()
{
CreateIndexEntityName();
CreateIndexSearch();
CreateIndexSimple();
}

public void CreateIndexSimple()
{
store.DatabaseCommands.PutIndex("Disks/Simple", new IndexDefinition
{
Map =
@"
from disk in docs.Disks
select new
{
disk.Artist,
disk.Title
}"
});
}

public void CreateIndexSearch()
{
store.DatabaseCommands.PutIndex("Disks/Search", new IndexDefinition
{
Map =
@"
from disk in docs.Disks
select new
{
Query = new[] { disk.Artist, disk.Title },
disk.Tracks,
DisId = disk.DiskIds
}",
Indexes =
{
{"Query", FieldIndexing.Analyzed},
{"Tracks", FieldIndexing.Analyzed}
}
});
}

public void CreateIndexEntityName()
{
new RavenDocumentsByEntityName().Execute(store);
}

public void WaitForIndexesToBecomeNonStale()
{
MemoryUsage.Clear();
while (true)
{
process.Refresh();
MemoryUsage.Add(process.WorkingSet64);

var statistics = store.DatabaseCommands.GetStatistics();

if (statistics.StaleIndexes.Length == 0 && doneImporting)
return;

foreach (var staleIndex in statistics.StaleIndexes)
{
var indexStats = statistics.Indexes.Single(x => x.Name == staleIndex);
var latencyInTime = (DateTime.UtcNow - indexStats.LastIndexedTimestamp).TotalMilliseconds;
LatencyTimes.Add(new KeyValuePair<string, double>(staleIndex, latencyInTime));

var latencyInDocuments = statistics.CountOfDocuments - indexStats.IndexingAttempts;
LatencyInDocuments.Add(new KeyValuePair<string, long>(staleIndex, latencyInDocuments));

logger.Debug("Stale index {0} - {1:#,#}/{2:#,#} - latency: {3:#,#}, {4:#,#}ms", indexStats.Name, indexStats.IndexingAttempts, statistics.CountOfDocuments,
latencyInDocuments,
latencyInTime);
}

Thread.Sleep(1000);
}
}

public Task AddDataAsync()
{
doneImporting = false;
return Task.Factory.StartNew(AddData);
}

public IEnumerable<Tuple<string, IEnumerable<double>, IEnumerable<long>>> Latencies()
{
foreach (var item in LatencyTimes.GroupBy(x => x.Key))
{
yield return
Tuple.Create(item.Key, item.Select(x => x.Value),
LatencyInDocuments.Where(pair => pair.Key == item.Key).Select(x => x.Value));
}
}
}
}
Something went wrong with that request. Please try again.