Example #1
0
        public void TestAddGetManyParallel(
            [Values(1, 2, 3, 4)] int writeStreams,
            [Values(2, 3, 4, 10)] int readStreams,
            [Values(LightningDbSyncMode.Fsync, LightningDbSyncMode.Async, LightningDbSyncMode.NoSync)] LightningDbSyncMode asyncStore,
            [Values(1, 10, 100, 1000, 10000, 100000)] int iterations)
        {
            var config = Config;

            config.SyncMode = asyncStore;

            using (var client = new GrpcTestClient(config))
            {
                var valueLength = 1024;

                var sw = new Stopwatch();
                sw.Start();
                var kvsAll = BuildKeyValuePairs(iterations * writeStreams, valueLength);
                sw.Stop();
                Console.WriteLine($"Preparing data elapsed: {sw.Elapsed}");

                var swAll = new Stopwatch();
                swAll.Start();
                Parallel.For(0, writeStreams, new ParallelOptions {
                    MaxDegreeOfParallelism = writeStreams
                }, i =>
                {
                    var kvs = kvsAll.Skip(i * iterations).Take(iterations).ToDictionary(kv => kv.Key, kv => kv.Value);

                    using (var writeClient = new LightClient(new Channel($"127.0.0.1:{client.ReplicaConfig.Port}", ChannelCredentials.Insecure), new ClientConfig
                    {
                        UseStreaming = true,
                        Compression = None,
                        HashedWith = Md5
                    }))
                    {
                        sw = new Stopwatch();
                        sw.Start();
                        var addRet = writeClient.TryAdd(kvs.Keys, (k, s) =>
                        {
                            var buffer = kvs[k].Item2;
                            s.Write(buffer, 0, buffer.Length);
                        }, DateTimeOffset.Now.AddSeconds(500));
                        sw.Stop();
                        Console.WriteLine($"Write elapsed: {sw.Elapsed}");

                        Assert.AreEqual(kvs.Count, addRet.Count);
                        Assert.IsTrue(addRet.All(k => kvs.ContainsKey(k)));
                    }
                });
                swAll.Stop();
                Console.WriteLine($"Write Elapsed total: {swAll.Elapsed}");
                Console.WriteLine($"Write Elapsed per thread: {swAll.Elapsed / writeStreams}");


                swAll = new Stopwatch();
                swAll.Start();
                Parallel.For(0, readStreams, new ParallelOptions {
                    MaxDegreeOfParallelism = readStreams
                }, i =>
                {
                    var kvs = kvsAll.Skip((i % writeStreams) * iterations).Take(iterations).ToDictionary(kv => kv.Key, kv => kv.Value);

                    using (var readClient = new LightClient(new Channel($"127.0.0.1:{client.ReplicaConfig.Port}", ChannelCredentials.Insecure), new ClientConfig
                    {
                        UseStreaming = true,
                        Compression = None,
                        HashedWith = Md5
                    }))
                    {
                        var getDict = new Dictionary <string, byte[]>();
                        sw          = new Stopwatch();
                        sw.Start();
                        var getRet = readClient.TryGet(kvs.Keys, (k, s) =>
                        {
                            var readBuffer = new byte[valueLength];
                            var readBytes  = s.Read(readBuffer, 0, readBuffer.Length);
                            getDict[k]     = readBuffer.Take(readBytes).ToArray();
                        });
                        sw.Stop();
                        Console.WriteLine($"Read elapsed: {sw.Elapsed}");

                        Assert.AreEqual(kvs.Keys.Count, getRet.Count);
                        Assert.IsTrue(getRet.All(k => kvs.ContainsKey(k)));
                        Assert.AreEqual(kvs.Keys.Count, getDict.Count);
                        foreach (var kv in getDict)
                        {
                            Assert.IsTrue(kvs.ContainsKey(kv.Key));
                            CollectionAssert.AreEqual(kvs[kv.Key].Item2, kv.Value);
                        }
                    }
                });
                swAll.Stop();
                Console.WriteLine($"Read Elapsed total: {swAll.Elapsed}");
                Console.WriteLine($"Read Elapsed per thread: {swAll.Elapsed / readStreams}");
            }
        }
Example #2
0
        public static void TestAddGetManyParallel(
            string host,
            uint port,
            int writeStreams,
            int readStreams,
            int keysCount)
        {
            var valueLength = 1024;

            var sw = new Stopwatch();

            sw.Start();
            var kvsAll = BuildKeyValuePairs(keysCount * writeStreams, valueLength);

            sw.Stop();
            Console.WriteLine($"Preparing data elapsed: {sw.Elapsed}");

            var swAll = new Stopwatch();

            swAll.Start();
            Parallel.For(0, writeStreams, new ParallelOptions {
                MaxDegreeOfParallelism = writeStreams
            }, i =>
            {
                var kvs = kvsAll.Skip(i * keysCount).Take(keysCount).ToDictionary(kv => kv.Key, kv => kv.Value);

                using (var writeClient = new LightClient(new Channel($"{host}:{port}", ChannelCredentials.Insecure), new ClientConfig
                {
                    UseStreaming = true,
                    Compression = None,
                    HashedWith = Md5
                }))
                {
                    sw = new Stopwatch();
                    sw.Start();
                    var addRet = writeClient.TryAdd(kvs.Keys, (k, s) =>
                    {
                        var buffer = kvs[k].Item2;
                        s.Write(buffer, 0, buffer.Length);
                    }, DateTimeOffset.Now.AddSeconds(500));
                    sw.Stop();
                    Console.WriteLine($"Write elapsed: {sw.Elapsed}");
                }
            });
            swAll.Stop();
            Console.WriteLine($"Write Elapsed total: {swAll.Elapsed}");
            Console.WriteLine($"Write Elapsed per thread: {swAll.Elapsed / writeStreams}");


            swAll = new Stopwatch();
            swAll.Start();
            Parallel.For(0, readStreams, new ParallelOptions {
                MaxDegreeOfParallelism = readStreams
            }, i =>
            {
                var kvs = kvsAll.Skip((i % writeStreams) * keysCount).Take(keysCount).ToDictionary(kv => kv.Key, kv => kv.Value);

                using (var readClient = new LightClient(new Channel($"{host}:{port}", ChannelCredentials.Insecure), new ClientConfig
                {
                    UseStreaming = true,
                    Compression = None,
                    HashedWith = Md5
                }))
                {
                    var getDict = new Dictionary <string, byte[]>();
                    sw          = new Stopwatch();
                    sw.Start();
                    var getRet = readClient.TryGet(kvs.Keys, (k, s) =>
                    {
                        var readBuffer = new byte[valueLength];
                        var readBytes  = s.Read(readBuffer, 0, readBuffer.Length);
                        getDict[k]     = readBuffer.Take(readBytes).ToArray();
                    });
                    sw.Stop();
                    Console.WriteLine($"Read elapsed: {sw.Elapsed}");
                }
            });
            swAll.Stop();
            Console.WriteLine($"Read Elapsed total: {swAll.Elapsed}");
            Console.WriteLine($"Read Elapsed per thread: {swAll.Elapsed / readStreams}");
        }