Esempio n. 1
0
        public void ManyContexts()
        {
            using (var conn = Create())
            {
                var profiler = new AsyncLocalProfiler();
                var prefix   = Me();
                conn.RegisterProfiler(profiler.GetSession);

                var tasks = new Task[16];

                var results = new ProfiledCommandEnumerable[tasks.Length];

                for (var i = 0; i < tasks.Length; i++)
                {
                    var ix = i;
                    tasks[ix] = Task.Run(async() =>
                    {
                        var db = conn.GetDatabase(ix);

                        var allTasks = new List <Task>();

                        for (var j = 0; j < 1000; j++)
                        {
                            var g = db.StringGetAsync(prefix + ix);
                            var s = db.StringSetAsync(prefix + ix, "world" + ix);
                            // overlap the g+s, just for fun
                            await g;
                            await s;
                        }

                        results[ix] = profiler.GetSession().FinishProfiling();
                    });
                }
                Task.WhenAll(tasks).Wait();

                for (var i = 0; i < results.Length; i++)
                {
                    var res = results[i];

                    var numGets = res.Count(r => r.Command == "GET");
                    var numSets = res.Count(r => r.Command == "SET");

                    Assert.Equal(1000, numGets);
                    Assert.Equal(1000, numSets);
                    Assert.True(res.All(cmd => cmd.Db == i));
                }
            }
        }
Esempio n. 2
0
        public async Task ProfilingMD_Ex2_Async()
        {
            using (var c = Create())
            {
                IConnectionMultiplexer conn = c;
                var profiler = new AsyncLocalProfiler();
                var prefix   = Me();

                conn.RegisterProfiler(profiler.GetSession);

                var tasks = new List <Task>();

                var perThreadTimings = new ConcurrentBag <List <IProfiledCommand> >();

                for (var i = 0; i < 16; i++)
                {
                    var db = conn.GetDatabase(i);

                    var task = Task.Run(async() =>
                    {
                        for (var j = 0; j < 100; j++)
                        {
                            await db.StringSetAsync(prefix + j, "" + j).ForAwait();
                        }

                        perThreadTimings.Add(profiler.GetSession().FinishProfiling().ToList());
                    });

                    tasks.Add(task);
                }

                var timeout  = Task.Delay(10000);
                var complete = Task.WhenAll(tasks);
                if (timeout == await Task.WhenAny(timeout, complete).ForAwait())
                {
                    throw new TimeoutException();
                }

                Assert.Equal(16, perThreadTimings.Count);
                foreach (var item in perThreadTimings)
                {
                    Assert.Equal(100, item.Count);
                }
            }
        }