2022-01-18 17:10:00 +08:00
|
|
|
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
|
|
|
|
// See the LICENCE file in the repository root for full licence text.
|
|
|
|
|
|
|
|
using System.Linq;
|
|
|
|
using System.Threading;
|
|
|
|
using BenchmarkDotNet.Attributes;
|
|
|
|
using osu.Framework.Testing;
|
|
|
|
using osu.Framework.Threading;
|
|
|
|
using osu.Game.Beatmaps;
|
|
|
|
using osu.Game.Database;
|
|
|
|
using osu.Game.Rulesets.Osu;
|
|
|
|
using osu.Game.Tests.Resources;
|
|
|
|
|
|
|
|
namespace osu.Game.Benchmarks
|
|
|
|
{
|
|
|
|
public class BenchmarkRealmReads : BenchmarkTest
|
|
|
|
{
|
|
|
|
private TemporaryNativeStorage storage;
|
2022-01-24 18:59:58 +08:00
|
|
|
private RealmAccess realm;
|
2022-01-18 17:10:00 +08:00
|
|
|
private UpdateThread updateThread;
|
|
|
|
|
|
|
|
[Params(1, 100, 1000)]
|
|
|
|
public int ReadsPerFetch { get; set; }
|
|
|
|
|
|
|
|
public override void SetUp()
|
|
|
|
{
|
|
|
|
storage = new TemporaryNativeStorage("realm-benchmark");
|
|
|
|
storage.DeleteDirectory(string.Empty);
|
|
|
|
|
2022-03-30 12:34:48 +08:00
|
|
|
realm = new RealmAccess(storage, OsuGameBase.CLIENT_DATABASE_FILENAME);
|
2022-01-18 17:10:00 +08:00
|
|
|
|
2022-01-25 12:04:05 +08:00
|
|
|
realm.Run(r =>
|
2022-01-22 11:12:13 +08:00
|
|
|
{
|
|
|
|
realm.Write(c => c.Add(TestResources.CreateTestBeatmapSetInfo(rulesets: new[] { new OsuRuleset().RulesetInfo })));
|
|
|
|
});
|
2022-01-18 17:10:00 +08:00
|
|
|
|
|
|
|
updateThread = new UpdateThread(() => { }, null);
|
|
|
|
updateThread.Start();
|
|
|
|
}
|
|
|
|
|
|
|
|
[Benchmark]
|
|
|
|
public void BenchmarkDirectPropertyRead()
|
|
|
|
{
|
2022-01-25 12:04:05 +08:00
|
|
|
realm.Run(r =>
|
2022-01-18 17:10:00 +08:00
|
|
|
{
|
2022-01-25 12:04:05 +08:00
|
|
|
var beatmapSet = r.All<BeatmapSetInfo>().First();
|
2022-01-18 17:10:00 +08:00
|
|
|
|
|
|
|
for (int i = 0; i < ReadsPerFetch; i++)
|
|
|
|
{
|
|
|
|
string _ = beatmapSet.Beatmaps.First().Hash;
|
|
|
|
}
|
2022-01-22 11:12:13 +08:00
|
|
|
});
|
2022-01-18 17:10:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
[Benchmark]
|
|
|
|
public void BenchmarkDirectPropertyReadUpdateThread()
|
|
|
|
{
|
|
|
|
var done = new ManualResetEventSlim();
|
|
|
|
|
|
|
|
updateThread.Scheduler.Add(() =>
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2022-01-24 18:59:58 +08:00
|
|
|
var beatmapSet = realm.Realm.All<BeatmapSetInfo>().First();
|
2022-01-18 17:10:00 +08:00
|
|
|
|
|
|
|
for (int i = 0; i < ReadsPerFetch; i++)
|
|
|
|
{
|
|
|
|
string _ = beatmapSet.Beatmaps.First().Hash;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
finally
|
|
|
|
{
|
|
|
|
done.Set();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
done.Wait();
|
|
|
|
}
|
|
|
|
|
|
|
|
[Benchmark]
|
|
|
|
public void BenchmarkRealmLivePropertyRead()
|
|
|
|
{
|
2022-01-24 18:59:58 +08:00
|
|
|
realm.Run(r =>
|
2022-01-18 17:10:00 +08:00
|
|
|
{
|
2022-01-24 18:59:58 +08:00
|
|
|
var beatmapSet = r.All<BeatmapSetInfo>().First().ToLive(realm);
|
2022-01-18 17:10:00 +08:00
|
|
|
|
|
|
|
for (int i = 0; i < ReadsPerFetch; i++)
|
|
|
|
{
|
|
|
|
string _ = beatmapSet.PerformRead(b => b.Beatmaps.First().Hash);
|
|
|
|
}
|
2022-01-22 11:12:13 +08:00
|
|
|
});
|
2022-01-18 17:10:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
[Benchmark]
|
|
|
|
public void BenchmarkRealmLivePropertyReadUpdateThread()
|
|
|
|
{
|
|
|
|
var done = new ManualResetEventSlim();
|
|
|
|
|
|
|
|
updateThread.Scheduler.Add(() =>
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2022-01-24 18:59:58 +08:00
|
|
|
var beatmapSet = realm.Realm.All<BeatmapSetInfo>().First().ToLive(realm);
|
2022-01-18 17:10:00 +08:00
|
|
|
|
|
|
|
for (int i = 0; i < ReadsPerFetch; i++)
|
|
|
|
{
|
|
|
|
string _ = beatmapSet.PerformRead(b => b.Beatmaps.First().Hash);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
finally
|
|
|
|
{
|
|
|
|
done.Set();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
done.Wait();
|
|
|
|
}
|
|
|
|
|
|
|
|
[Benchmark]
|
|
|
|
public void BenchmarkDetachedPropertyRead()
|
|
|
|
{
|
2022-01-25 12:04:05 +08:00
|
|
|
realm.Run(r =>
|
2022-01-18 17:10:00 +08:00
|
|
|
{
|
2022-01-25 12:04:05 +08:00
|
|
|
var beatmapSet = r.All<BeatmapSetInfo>().First().Detach();
|
2022-01-18 17:10:00 +08:00
|
|
|
|
|
|
|
for (int i = 0; i < ReadsPerFetch; i++)
|
|
|
|
{
|
|
|
|
string _ = beatmapSet.Beatmaps.First().Hash;
|
|
|
|
}
|
2022-01-22 11:12:13 +08:00
|
|
|
});
|
2022-01-18 17:10:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
[GlobalCleanup]
|
|
|
|
public void Cleanup()
|
|
|
|
{
|
2022-01-24 18:59:58 +08:00
|
|
|
realm?.Dispose();
|
2022-01-18 17:10:00 +08:00
|
|
|
storage?.Dispose();
|
|
|
|
updateThread?.Exit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|