Skip to content

Instantly share code, notes, and snippets.

@peppy
Created December 4, 2019 07:21
Show Gist options
  • Save peppy/e558a50bbe5869ac79f2442f496d6325 to your computer and use it in GitHub Desktop.
Save peppy/e558a50bbe5869ac79f2442f496d6325 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
using Realms;
namespace RealmPerformanceTest
{
internal static class Program
{
static void Main(string[] args)
{
BenchmarkRunner.Run<RealmTests>();
}
}
[MemoryDiagnoser]
public class RealmTests
{
private static readonly Realm realm;
private static readonly TestRealmModel prefetched_realm_model;
private static readonly TestModel precreated_model;
private static readonly TestRealmModel unmanaged_realm_model;
static RealmTests()
{
var config = new RealmConfiguration("/Volumes/UNTITLED/local.realm");
Realm.DeleteRealm(config);
realm = Realm.GetInstance(config);
realm.Write(() => realm.Add(new TestRealmModel("key")));
performWrites(prefetched_realm_model = realm.All<TestRealmModel>().First());
performWrites(unmanaged_realm_model = new TestRealmModel("key"));
performWrites(precreated_model = new TestModel());
for (int i = 0; i < 10; i++)
{
const int insert_count = 1000;
Console.WriteLine($"Inserting rows {i * insert_count}...");
realm.Write(() =>
{
for (int j = 0; j < insert_count; j++)
realm.Add(new TestRealmModel(Guid.NewGuid().ToString()));
});
}
}
[Benchmark]
public void TestUnmanagedRealmWithConstruction() => perfomRepeatedIO(() => new TestRealmModel());
[Benchmark]
public void TestUnmanagedRealm() => perfomRepeatedIO(() => unmanaged_realm_model);
[Benchmark]
public void TestManagedRealm() => perfomRepeatedIO(() => prefetched_realm_model);
[Benchmark]
public void TestManagedRealmWithFetch() => perfomRepeatedIO(() => realm.Find<TestRealmModel>("key"));
[Benchmark]
public void TestBasicWithConstruction() => perfomRepeatedIO(() => new TestModel());
[Benchmark]
public void TestBasic() => perfomRepeatedIO(() => precreated_model);
private void perfomRepeatedIO(Func<IModel> fetchModel)
=> performReads(fetchModel());
private void performReads(IModel model)
{
string testRead = $"{model.TestString}{model.TestInt}{model.TestDouble}{model.Children.Count}";
}
private static void performWrites(IModel model)
{
Transaction write = null;
bool transaction = model is RealmObject ro && ro.IsManaged;
if (transaction)
write = realm.BeginWrite();
model.TestString = Guid.NewGuid().ToString();
model.TestInt = (int)Math.PI;
model.TestDouble = Math.PI;
if (transaction)
write?.Commit();
}
}
class TestRealmModel : RealmObject, IModel
{
public TestRealmModel(string key)
{
ID = key;
}
public TestRealmModel()
{
}
[PrimaryKey] public string ID { get; set; }
public string TestString { get; set; }
public int TestInt { get; set; }
public double TestDouble { get; set; }
// ReSharper disable once UnassignedGetOnlyAutoProperty
public IList<TestRealmModel> Children { get; }
}
class TestModel : IModel
{
public string ID { get; set; } = "key";
public string TestString { get; set; }
public int TestInt { get; set; }
public double TestDouble { get; set; }
public IList<TestRealmModel> Children { get; } = new List<TestRealmModel>();
}
interface IModel
{
string ID { get; set; }
string TestString { get; set; }
int TestInt { get; set; }
double TestDouble { get; set; }
IList<TestRealmModel> Children { get; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment