Example #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));
                }
            }
        }
Example #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);
                }
            }
        }
Example #3
0
        public static void TestProfiling()
        {
            var profiler = new AsyncLocalProfiler();

            _redis.RegisterProfiler(profiler.GetSession);
            //var commands = profiler.GetSession().FinishProfiling();
            //Console.WriteLine(string.Join(",", commands.Select(p => p.ElapsedTime)));

            var toyProfiler = new ToyProfiler();

            //var sharedSession = new ProfilingSession();
            _redis.RegisterProfiler(() => toyProfiler.PerThreadSession);
            var threads          = new List <Thread>();
            var perThreadTimings = new ConcurrentDictionary <Thread, List <IProfiledCommand> >();

            for (int i = 0; i < 16; i++)
            {
                var db     = _redis.GetDatabase(i);
                var thread = new Thread(
                    delegate()
                {
                    //set each thread to share a session
                    //toyProfiler.PerThreadSession = sharedSession;
                    var threadTasks = new List <Task>();
                    toyProfiler.PerThreadSession = new ProfilingSession();
                    for (int j = 0; j < 1000; j++)
                    {
                        var task = db.StringSetAsync("" + j, "" + j);
                        threadTasks.Add(task);
                    }
                    Task.WaitAll(threadTasks.ToArray());
                    perThreadTimings[Thread.CurrentThread] = toyProfiler.PerThreadSession.FinishProfiling().ToList();
                });
                threads.Add(thread);
            }
            threads.ForEach(thread => thread.Start());
            threads.ForEach(thread => thread.Join());
            //var timings = sharedSession.FinishProfiling();
            Console.WriteLine(perThreadTimings.Count);
        }