Add statistics for `Live` usage

This commit is contained in:
Dean Herbert 2022-01-26 12:42:24 +09:00
parent 56b06f34f0
commit c7947b3489
1 changed files with 17 additions and 1 deletions

View File

@ -4,6 +4,7 @@
using System;
using System.Diagnostics;
using osu.Framework.Development;
using osu.Framework.Statistics;
using Realms;
#nullable enable
@ -14,7 +15,7 @@ namespace osu.Game.Database
/// Provides a method of working with realm objects over longer application lifetimes.
/// </summary>
/// <typeparam name="T">The underlying object type.</typeparam>
public class RealmLive<T> : ILive<T> where T : RealmObject, IHasGuidPrimaryKey
public class RealmLive<T> : RealmLive, ILive<T> where T : RealmObject, IHasGuidPrimaryKey
{
public Guid ID { get; }
@ -65,6 +66,7 @@ public void PerformRead(Action<T> perform)
}
perform(retrieveFromID(r, ID));
USAGE_ASYNC.Value++;
});
}
@ -86,6 +88,7 @@ public TReturn PerformRead<TReturn>(Func<T, TReturn> perform)
return realm.Run(r =>
{
var returnData = perform(retrieveFromID(r, ID));
USAGE_ASYNC.Value++;
if (returnData is RealmObjectBase realmObject && realmObject.IsManaged)
throw new InvalidOperationException(@$"Managed realm objects should not exit the scope of {nameof(PerformRead)}.");
@ -108,6 +111,7 @@ public void PerformWrite(Action<T> perform)
var transaction = t.Realm.BeginWrite();
perform(t);
transaction.Commit();
WRITES.Value++;
});
}
@ -131,10 +135,14 @@ private void ensureDataIsFromUpdateThread()
Debug.Assert(ThreadSafety.IsUpdateThread);
if (dataIsFromUpdateThread && !data.Realm.IsClosed)
{
USAGE_UPDATE_IMMEDIATE.Value++;
return;
}
dataIsFromUpdateThread = true;
data = retrieveFromID(realm.Realm, ID);
USAGE_UPDATE_REFETCH.Value++;
}
private T retrieveFromID(Realm realm, Guid id)
@ -157,4 +165,12 @@ private T retrieveFromID(Realm realm, Guid id)
public override string ToString() => PerformRead(i => i.ToString());
}
public class RealmLive
{
protected static readonly GlobalStatistic<int> WRITES = GlobalStatistics.Get<int>(@"Realm", @"Live writes");
protected static readonly GlobalStatistic<int> USAGE_UPDATE_IMMEDIATE = GlobalStatistics.Get<int>(@"Realm", @"Live update read (fast)");
protected static readonly GlobalStatistic<int> USAGE_UPDATE_REFETCH = GlobalStatistics.Get<int>(@"Realm", @"Live update read (slow)");
protected static readonly GlobalStatistic<int> USAGE_ASYNC = GlobalStatistics.Get<int>(@"Realm", @"Live async read");
}
}