public async Task Manage_OneTrans_BeginInvoke()
        {
            await GeneratePerfRangesForRealm(async (cache, size) =>
            {
                var toWrite = PerfHelper.GenerateRandomDatabaseContents(size);

                var st = new Stopwatch();
                st.Start();

                await Task.Run(() =>
                {
                    using (var realmThread = new RealmThread(cache.Config))
                    {
                        realmThread.BeginTransaction();
                        realmThread.BeginInvoke((threadSafeRealm) =>
                        {
                            foreach (var kvp in toWrite)
                            {
                                var obj   = new KeyValueRecord();
                                obj.Key   = kvp.Key;
                                obj.Value = kvp.Value;
                                threadSafeRealm.Add(obj);
                            }
                        });
                        realmThread.CommitTransaction();
                    }
                });

                st.Stop();
                return(st.ElapsedMilliseconds);
            });
        }
        public async Task Manage_OneTrans_InvokeAsync()
        {
            await GeneratePerfRangesForRealm(async (cache, size) =>
            {
                var toWrite = PerfHelper.GenerateRandomDatabaseContents(size);

                var st = new Stopwatch();
                st.Start();

                using (var realmThread = new RealmThread(cache.Config))
                {
                    await realmThread.InvokeAsync(async(threadSafeRealm) =>
                    {
                        // Mock something using an await in this lamba
                        await Task.FromResult(true);

                        threadSafeRealm.Write(() =>
                        {
                            foreach (var kvp in toWrite)
                            {
                                var obj   = new KeyValueRecord();
                                obj.Key   = kvp.Key;
                                obj.Value = kvp.Value;
                                threadSafeRealm.Add(obj);
                            }
                        });
                    });
                }

                st.Stop();
                return(st.ElapsedMilliseconds);
            });
        }
Exemple #3
0
        async Task <Realms.Realm> GenerateRealmDB(string path)
        {
            path = path ?? IntegrationTestHelper.GetIntegrationTestRootDirectory();

            var giantDbSize = PerfHelper.GetPerfRanges().Last();
            var config      = new RealmConfiguration(Path.Combine(path, "realm.db"))
            {
                ObjectClasses = new Type[] { typeof(KeyValueRecord) },
                ReadOnly      = false
            };
            var cache = Realms.Realm.GetInstance(config);

            var keys = cache.All <KeyValueRecord>().Count();

            if (keys == giantDbSize)
            {
                return(cache);
            }

            await cache.WriteAsync(r =>
            {
                r.RemoveAll();
            });

            await GenerateRealmDB(cache, giantDbSize);

            return(cache);
        }
Exemple #4
0
        protected static async Task <List <string> > GenerateRealmDB(Realms.Realm targetCache, int size)
        {
            var ret = new List <string>();

            // Write out in groups of 4096
            while (size > 0)
            {
                var toWriteSize = Math.Min(4096, size);
                var toWrite     = PerfHelper.GenerateRandomDatabaseContents(toWriteSize);

                await targetCache.WriteAsync((Realms.Realm realm) =>
                {
                    foreach (var item in toWrite)
                    {
                        var c   = new KeyValueRecord();
                        c.Key   = item.Key;
                        c.Value = item.Value;
                        realm.Manage <KeyValueRecord>(c);                        // update: false
                    }
                });

                foreach (var k in toWrite.Keys)
                {
                    ret.Add(k);
                }
                size -= toWrite.Count;
            }
            return(ret);
        }
Exemple #5
0
        protected async Task GeneratePerfRangesForRealm(Func <Realms.Realm, int, Task <long> > block)
        {
            results = new Dictionary <int, long>();
            dbName  = default(string);

            var dirPath = default(string);

            using (Utility.WithEmptyDirectory(out dirPath))
                using (var cache = RealmThread.GetInstance(Path.Combine(dirPath, "realm.db")))
                {
                    using (var realmThread = new RealmThread(cache.Config))
                    {
                        realmThread.Invoke((obj) =>
                        {
                            obj.Write(() => { obj.RemoveAll(); });
                        });
                    }
                    dbName = "Realm";

                    foreach (var size in PerfHelper.GetPerfRanges())
                    {
                        results[size] = await block(cache, size);
                    }
                }
        }
Exemple #6
0
        protected static async Task <List <string> > GenerateRealmDB(Realms.Realm targetCache, int size)
        {
            var ret = new List <string>();

            // Write out in groups of 4096
            while (size > 0)
            {
                var toWriteSize = Math.Min(4096, size);
                var toWrite     = PerfHelper.GenerateRandomDatabaseContents(toWriteSize);

                using (var rt = new RealmThread(targetCache.Config))
                {
                    await rt.InvokeAsync(async(Realms.Realm arg) =>
                    {
                        await arg.WriteAsync((r) =>
                        {
                            foreach (var item in toWrite)
                            {
                                var c = new KeyValueRecord {
                                    Key = item.Key, Value = item.Value
                                };
                                r.Add(c);                                 // update: false
                            }
                        });
                    });
                }
                foreach (var k in toWrite.Keys)
                {
                    ret.Add(k);
                }
                size -= toWrite.Count;
            }
            return(ret);
        }
Exemple #7
0
        public async Task GeneratePerfRangesForBlock2(Func <Realms.Realm, int, List <string>, Task <long> > block)
        {
            results = new Dictionary <int, long>();
            dbName  = default(string);

            var dirPath = default(string);

            using (Utility.WithEmptyDirectory(out dirPath))
                using (var cache = await GenerateRealmDB(dirPath))
                {
                    List <string> keys = null;
                    using (var realmThread = new RealmThread(cache.Config))
                    {
                        realmThread.Invoke((r) =>
                        {
                            r.Write(() => {
                                keys = r.All <KeyValueRecord>().ToList().Select(x => x.Key).ToList();
                            });
                        });
                    }
                    dbName = dbName ?? cache.GetType().Name;

                    foreach (var size in PerfHelper.GetPerfRanges())
                    {
                        results[size] = await block(cache, size, keys.ToList());
                    }
                }
        }
        public async Task Manage_updateTrue_OneTrans()
        {
            await GeneratePerfRangesForRealm(async (cache, size) =>
            {
                var toWrite = PerfHelper.GenerateRandomDatabaseContents(size);

                var st = new Stopwatch();
                st.Start();

                using (var realmThread = new RealmThread(cache.Config))
                {
                    realmThread.Invoke((realm) =>
                    {
                        realm.Write(() =>
                        {
                            foreach (var kvp in toWrite)
                            {
                                var c = new KeyValueRecord {
                                    Key = kvp.Key, Value = kvp.Value
                                };
                                realm.Add(c, update: true);
                            }
                        });
                    });
                }

                st.Stop();
                await Task.Delay(1);                 // cheap hack
                return(st.ElapsedMilliseconds);
            });
        }
Exemple #9
0
        async Task <Realms.Realm> GenerateRealmDB(string path)
        {
            path = path ?? IntegrationTestHelper.GetIntegrationTestRootDirectory();

            var giantDbSize = PerfHelper.GetPerfRanges().Last();

            var config = new RealmConfiguration(Path.Combine(path, "perf.realm"))
            {
                ObjectClasses = new Type[] { typeof(KeyValueRecord) },
                IsReadOnly    = false
            };

            var cache = RealmThread.GetInstance(config);

            var keys = cache.All <KeyValueRecord>().Count();

            if (keys == giantDbSize)
            {
                return(cache);
            }

            using (var realmThread = new RealmThread(cache.Config))
            {
                realmThread.Invoke((obj) =>
                {
                    obj.Write(() => { obj.RemoveAll(); });
                });
            }

            await GenerateRealmDB(cache, giantDbSize);

            return(cache);
        }
        public async Task Write_Read_Parallel_On_Different_Threads()
        {
            await GeneratePerfRangesForRealm(async (cache, size) =>
            {
                await cache.WriteAsync((obj) =>
                {
                    obj.RemoveAll();
                });
                var toWrite = PerfHelper.GenerateRandomDatabaseContents(size);

                var st = new Stopwatch();
                st.Start();

                await Task.Run(() =>
                {
                    using (var blockingQueue = new BlockingCollection <string>())
                        using (var realmThreadWrite = new RealmThread(cache.Config))
                            using (var realmThreadRead = new RealmThread(cache.Config))
                            {
                                Parallel.Invoke(() =>
                                {
                                    realmThreadWrite.Invoke(threadSafeWriteRealm =>
                                    {
                                        foreach (var kvp in toWrite)
                                        {
                                            // Individual record write transactions so the other RealmThread can read asap
                                            threadSafeWriteRealm.Write(() =>
                                            {
                                                var obj   = threadSafeWriteRealm.CreateObject(typeof(KeyValueRecord).Name);
                                                obj.Key   = kvp.Key;
                                                obj.Value = kvp.Value;
                                            });
                                            blockingQueue.Add(kvp.Key);
                                        }
                                        blockingQueue.CompleteAdding();
                                    });
                                },
                                                () =>
                                {
                                    realmThreadRead.Invoke((threadSafeReadRealm) =>
                                    {
                                        foreach (var key in blockingQueue.GetConsumingEnumerable())
                                        {
                                            // Refresh() is automatically called at the beginning of each BeginInvoke,
                                            // so if we are within the RealmPump block and need to see the latest changes
                                            // from other Realm instances, call Refresh manually
                                            threadSafeReadRealm.Refresh();
                                            var record = threadSafeReadRealm.ObjectForPrimaryKey <KeyValueRecord>(key);
                                            Assert.NotNull(record);
                                            Assert.Equal(key, record.Key);
                                        }
                                    });
                                });
                            }
                });
                st.Stop();
                return(st.ElapsedMilliseconds);
            });
        }
        protected async Task GeneratePerfRangesForRealm(Func <Realms.Realm, int, Task <long> > block)
        {
            results = new Dictionary <int, long>();
            dbName  = default(string);
            var dirPath = default(string);

            using (Utility.WithEmptyDirectory(out dirPath))
                using (var cache = RealmThread.GetInstance(Path.Combine(dirPath, "perf.realm")))
                {
                    dbName = "Realm";

                    foreach (var size in PerfHelper.GetPerfRanges())
                    {
                        results[size] = await block(cache, size);
                    }
                }
        }
Exemple #12
0
        public async Task CreateObject_OneTrans()
        {
            await GeneratePerfRangesForRealm(async (cache, size) =>
            {
                var toWrite = PerfHelper.GenerateRandomDatabaseContents(size);

                var st = new Stopwatch();
                st.Start();

                await cache.WriteAsync((r) =>
                {
                    foreach (var kvp in toWrite)
                    {
                        var c   = r.CreateObject(typeof(KeyValueRecord).Name);
                        c.Key   = kvp.Key;
                        c.Value = kvp.Value;
                    }
                });

                st.Stop();
                return(st.ElapsedMilliseconds);
            });
        }
Exemple #13
0
        public async Task Manage_updateTrue_OneTrans()
        {
            await GeneratePerfRangesForRealm(async (cache, size) =>
            {
                var toWrite = PerfHelper.GenerateRandomDatabaseContents(size);

                var st = new Stopwatch();
                st.Start();

                await cache.WriteAsync((r) =>
                {
                    foreach (var kvp in toWrite)
                    {
                        var c   = new KeyValueRecord();
                        c.Key   = kvp.Key;
                        c.Value = kvp.Value;
                        r.Manage(c, update: true);
                    }
                });

                st.Stop();
                return(st.ElapsedMilliseconds);
            });
        }
Exemple #14
0
        public async Task GeneratePerfRangesForBlock2(Func <Realms.Realm, int, List <string>, Task <long> > block)
        {
            results = new Dictionary <int, long>();
            dbName  = default(string);

            var dirPath = default(string);

            using (Utility.WithEmptyDirectory(out dirPath))
                using (var cache = await GenerateRealmDB(dirPath))
                {
                    List <string> keys = null;
                    await cache.WriteAsync(r =>             // This is just a cheap way to avoid "Realm accessed from incorrect thread"
                    {
                        keys = r.All <KeyValueRecord>().ToList().Select(x => x.Key).ToList();
                    });

                    dbName = dbName ?? cache.GetType().Name;

                    foreach (var size in PerfHelper.GetPerfRanges())
                    {
                        results[size] = await block(cache, size, keys.ToList());
                    }
                }
        }