|
using System; |
|
using System.IO; |
|
using System.Linq; |
|
using BenchmarkDotNet.Attributes; |
|
using BenchmarkDotNet.Running; |
|
using Realms; |
|
|
|
namespace FindBenchmark |
|
{ |
|
class Program |
|
{ |
|
static void Main(string[] args) |
|
{ |
|
BenchmarkRunner.Run<FindBenchmarkClass>(); |
|
} |
|
|
|
private static void ReferenceAssembly() |
|
{ |
|
// アセンブリロードに失敗したとは言わせない |
|
Realm.GetInstance() |
|
.All<IntegerObject>() |
|
.Where(x => true) |
|
.ToList(); |
|
} |
|
} |
|
|
|
[InProcess] |
|
public class FindBenchmarkClass |
|
{ |
|
[Params(10, 100, 1000, 10000, 100000, 200000, 400000, 600000, 800000, 1000000)] |
|
public int ElementCount { get; set; } |
|
|
|
private Realm _realm; |
|
private long _integerKey; |
|
private string _stringKey; |
|
|
|
[GlobalSetup] |
|
public void GlobalSetup() |
|
{ |
|
var dbFileDirectory = Environment.GetEnvironmentVariable("BENCHMARK_DB_DIR"); |
|
if (string.IsNullOrEmpty(dbFileDirectory)) |
|
dbFileDirectory = Directory.GetCurrentDirectory(); |
|
var dbFilePath = Path.Join(dbFileDirectory, $"FindBenchmark{this.ElementCount:D}.realm"); |
|
|
|
var writableRealmConfig = new RealmConfiguration(dbFilePath) { ShouldDeleteIfMigrationNeeded = true }; |
|
var written = false; |
|
using (var realm = Realm.GetInstance(writableRealmConfig)) |
|
{ |
|
if (realm.All<IntegerObject>().Count() != this.ElementCount) |
|
{ |
|
realm.Write(() => |
|
{ |
|
realm.RemoveAll<IntegerObject>(); |
|
for (var i = 0; i < this.ElementCount; i++) |
|
{ |
|
realm.Add(new IntegerObject() { Key = i }); |
|
} |
|
}); |
|
|
|
written = true; |
|
} |
|
|
|
if (realm.All<StringObject>().Count() != this.ElementCount) |
|
{ |
|
realm.Write(() => |
|
{ |
|
realm.RemoveAll<StringObject>(); |
|
for (var i = 0; i < this.ElementCount; i++) |
|
{ |
|
realm.Add(new StringObject() { Key = i.ToString("D") }); |
|
} |
|
}); |
|
|
|
written = true; |
|
} |
|
} |
|
|
|
if (written) Realm.Compact(writableRealmConfig); |
|
|
|
this._realm = Realm.GetInstance(new RealmConfiguration(dbFilePath) |
|
{ |
|
IsReadOnly = true, |
|
}); |
|
|
|
// 最後から2番目を検索対象にする |
|
this._integerKey = this._realm.All<IntegerObject>() |
|
.AsEnumerable() |
|
.Reverse() |
|
.ElementAt(1) |
|
.Key; |
|
|
|
this._stringKey = this._realm.All<StringObject>() |
|
.AsEnumerable() |
|
.Reverse() |
|
.ElementAt(1) |
|
.Key; |
|
} |
|
|
|
[GlobalCleanup] |
|
public void GlobalCleanup() |
|
{ |
|
if (this._realm != null) |
|
{ |
|
this._realm.Dispose(); |
|
this._realm = null; |
|
} |
|
} |
|
|
|
[Benchmark] |
|
public IntegerObject FindInteger() |
|
{ |
|
return this._realm.Find<IntegerObject>(this._integerKey); |
|
} |
|
|
|
[Benchmark] |
|
public IntegerObject WhereInteger() |
|
{ |
|
return this._realm.All<IntegerObject>() |
|
.First(x => x.Key == this._integerKey); |
|
} |
|
|
|
[Benchmark] |
|
public StringObject FindString() |
|
{ |
|
return this._realm.Find<StringObject>(this._stringKey); |
|
} |
|
|
|
[Benchmark] |
|
public StringObject WhereString() |
|
{ |
|
return this._realm.All<StringObject>() |
|
.First(x => x.Key == this._stringKey); |
|
} |
|
} |
|
} |