Esempio n. 1
0
        // This is a separate method for target=DEBUG purposes.
        // In release builds, the runtime is smart enough to figure out
        //   that the contexts are unreachable and should be collected but in
        //   debug builds... well, it's not very smart.
        object LeaksCollectedAndRePooled_Initialize(ConnectionMultiplexer conn, int threadCount)
        {
            var profiler = new TestProfiler3();

            conn.RegisterProfiler(profiler);

            var perThreadContexts = new List <object>();

            for (var i = 0; i < threadCount; i++)
            {
                perThreadContexts.Add(new object());
            }

            var threads = new List <Thread>();

            var results = new IEnumerable <IProfiledCommand> [threadCount];

            for (var i = 0; i < threadCount; i++)
            {
                var ix     = i;
                var thread =
                    new Thread(
                        delegate()
                {
                    var ctx = perThreadContexts[ix];
                    profiler.RegisterContext(ctx);

                    conn.BeginProfiling(ctx);
                    var db = conn.GetDatabase(ix);

                    var allTasks = new List <Task>();

                    for (var j = 0; j < 1000; j++)
                    {
                        allTasks.Add(db.StringGetAsync("hello" + ix));
                        allTasks.Add(db.StringSetAsync("hello" + ix, "world" + ix));
                    }

                    Task.WaitAll(allTasks.ToArray());

                    // intentionally leaking!
                }
                        );

                threads.Add(thread);
            }

            threads.ForEach(t => t.Start());
            threads.ForEach(t => t.Join());

            var anyContext = profiler.AnyContext();

            profiler.Reset();

            return(anyContext);
        }
Esempio n. 2
0
        public void ProfilingMD_Ex2()
        {
            using (var c = Create())
            {
                ConnectionMultiplexer conn = c;
                var profiler = new ToyProfiler();

                conn.RegisterProfiler(profiler);

                var threads = new List <Thread>();

                var perThreadTimings = new ConcurrentDictionary <Thread, List <IProfiledCommand> >();

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

                    var thread =
                        new Thread(
                            delegate()
                    {
                        var threadTasks = new List <Task>();

                        conn.BeginProfiling(Thread.CurrentThread);

                        for (var j = 0; j < 1000; j++)
                        {
                            var task = db.StringSetAsync("" + j, "" + j);
                            threadTasks.Add(task);
                        }

                        Task.WaitAll(threadTasks.ToArray());

                        perThreadTimings[Thread.CurrentThread] = conn.FinishProfiling(Thread.CurrentThread).ToList();
                    }
                            );

                    profiler.Contexts[thread] = thread;

                    threads.Add(thread);
                }

                threads.ForEach(thread => thread.Start());
                threads.ForEach(thread => thread.Join());

                Assert.AreEqual(16, perThreadTimings.Count);
                Assert.IsTrue(perThreadTimings.All(kv => kv.Value.Count == 1000));
            }
        }
Esempio n. 3
0
        public void ProfilingMD_Ex1()
        {
            using (var c = Create())
            {
                ConnectionMultiplexer conn = c;
                var profiler         = new ToyProfiler();
                var thisGroupContext = new object();

                conn.RegisterProfiler(profiler);

                var threads = new List <Thread>();

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

                    var thread =
                        new Thread(
                            delegate()
                    {
                        var threadTasks = new List <Task>();

                        for (var j = 0; j < 1000; j++)
                        {
                            var task = db.StringSetAsync("" + j, "" + j);
                            threadTasks.Add(task);
                        }

                        Task.WaitAll(threadTasks.ToArray());
                    }
                            );

                    profiler.Contexts[thread] = thisGroupContext;

                    threads.Add(thread);
                }

                conn.BeginProfiling(thisGroupContext);

                threads.ForEach(thread => thread.Start());
                threads.ForEach(thread => thread.Join());

                IEnumerable <IProfiledCommand> timings = conn.FinishProfiling(thisGroupContext);

                Assert.AreEqual(16000, timings.Count());
            }
        }
Esempio n. 4
0
        // This is a separate method for target=DEBUG purposes.
        // In release builds, the runtime is smart enough to figure out
        //   that the contexts are unreachable and should be collected but in
        //   debug builds... well, it's not very smart.
        object LeaksCollectedAndRePooled_Initialize(ConnectionMultiplexer conn, int threadCount)
        {
            var profiler = new TestProfiler3();
            conn.RegisterProfiler(profiler);

            var perThreadContexts = new List<object>();
            for (var i = 0; i < threadCount; i++)
            {
                perThreadContexts.Add(new object());
            }

            var threads = new List<Thread>();

            var results = new IEnumerable<IProfiledCommand>[threadCount];

            for (var i = 0; i < threadCount; i++)
            {
                var ix = i;
                var thread =
                    new Thread(
                        delegate()
                        {
                            var ctx = perThreadContexts[ix];
                            profiler.RegisterContext(ctx);

                            conn.BeginProfiling(ctx);
                            var db = conn.GetDatabase(ix);

                            var allTasks = new List<Task>();

                            for (var j = 0; j < 1000; j++)
                            {
                                allTasks.Add(db.StringGetAsync("hello" + ix));
                                allTasks.Add(db.StringSetAsync("hello" + ix, "world" + ix));
                            }

                            Task.WaitAll(allTasks.ToArray());

                            // intentionally leaking!
                        }
                    );

                threads.Add(thread);
            }

            threads.ForEach(t => t.Start());
            threads.ForEach(t => t.Join());

            var anyContext = profiler.AnyContext();
            profiler.Reset();

            return anyContext;
        }
Esempio n. 5
0
        static void Main(string[] args)
        {
            ConnectionMultiplexer conn = ConnectionMultiplexer.Connect("localhost,allowAdmin=true");
            var server = conn.GetServer("localhost:6379");

            //擦除该服务器上的所有数据库的数据
            server.FlushAllDatabases();
            var profiler = new ToyProfiler();

            conn.RegisterProfiler(profiler);
            var threads = new List <Thread>();

            #region V1
            //var thisGroupContext = new object();
            //for (var i = 0; i < 16; i++)
            //{
            //    var db = conn.GetDatabase(i);

            //    var thread =
            //        new Thread(
            //            delegate ()
            //            {
            //                var threadTasks = new List<Task>();

            //                for (var j = 0; j < 1000; j++)
            //                {
            //                    var task = db.StringSetAsync("" + j, "" + j);
            //                    threadTasks.Add(task);
            //                }

            //                Task.WaitAll(threadTasks.ToArray());
            //            }
            //        );

            //    profiler.Contexts[thread] = thisGroupContext;

            //    threads.Add(thread);
            //}

            //conn.BeginProfiling(thisGroupContext);

            //threads.ForEach(thread => thread.Start());
            //threads.ForEach(thread => thread.Join());

            //IEnumerable<IProfiledCommand> timings = conn.FinishProfiling(thisGroupContext);
            #endregion

            var perThreadTimings = new ConcurrentDictionary <Thread, List <IProfiledCommand> >();

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

                var thread =
                    new Thread(
                        delegate()
                {
                    var threadTasks = new List <Task>();

                    conn.BeginProfiling(Thread.CurrentThread);

                    for (var j = 0; j < 1000; j++)
                    {
                        var task = db.StringSetAsync("" + j, "" + j);
                        threadTasks.Add(task);
                    }

                    Task.WaitAll(threadTasks.ToArray());

                    perThreadTimings[Thread.CurrentThread] = conn.FinishProfiling(Thread.CurrentThread).ToList();
                }
                        );

                profiler.Contexts[thread] = thread;

                threads.Add(thread);
            }

            threads.ForEach(thread => thread.Start());
            threads.ForEach(thread => thread.Join());
            ReadLine();
        }