Exemplo n.º 1
0
        public void SimpleProfiling()
        {
            using (var conn = Create())
            {
                var profiler = new ProfilingSession();
                var key      = Me();
                var db       = conn.GetDatabase();
                db.KeyDelete(key, CommandFlags.FireAndForget);

                conn.RegisterProfiler(() => profiler);
                db.StringSet(key, "world");
                var val = db.StringGet(key);
                Assert.Equal("world", val);

                var msgs = profiler.FinishProfiling().Where(m => m.Command == "GET" || m.Command == "SET");
                foreach (var msg in msgs)
                {
                    Log("Profiler Message: " + Environment.NewLine + msg.ToString());
                }
                Log("Checking GET...");
                Assert.Contains(msgs, m => m.Command == "GET");
                Log("Checking SET...");
                Assert.Contains(msgs, m => m.Command == "SET");
                Assert.Equal(2, msgs.Count(m => m.RetransmissionOf is null));

                var arr = msgs.Where(m => m.RetransmissionOf is null).ToArray();
                Assert.Equal("SET", arr[0].Command);
                Assert.Equal("GET", arr[1].Command);
            }
        }
Exemplo n.º 2
0
        public void ManyThreads()
        {
            using (var conn = Create())
            {
                var session = new ProfilingSession();
                var prefix  = Me();

                conn.RegisterProfiler(() => session);

                var       threads  = new List <Thread>();
                const int CountPer = 100;
                for (var i = 1; i <= 16; i++)
                {
                    var db = conn.GetDatabase(i);

                    threads.Add(new Thread(() =>
                    {
                        var threadTasks = new List <Task>();

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

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

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

                var allVals  = session.FinishProfiling();
                var relevant = allVals.Where(cmd => cmd.Db > 0).ToList();

                var kinds = relevant.Select(cmd => cmd.Command).Distinct().ToList();
                foreach (var k in kinds)
                {
                    Log("Kind Seen: " + k);
                }
                Assert.True(kinds.Count <= 2);
                Assert.Contains("SET", kinds);
                if (kinds.Count == 2 && !kinds.Contains("SELECT") && !kinds.Contains("GET"))
                {
                    Assert.True(false, "Non-SET, Non-SELECT, Non-GET command seen");
                }

                Assert.Equal(16 * CountPer, relevant.Count);
                Assert.Equal(16, relevant.Select(cmd => cmd.Db).Distinct().Count());

                for (var i = 1; i <= 16; i++)
                {
                    var setsInDb = relevant.Count(cmd => cmd.Db == i);
                    Assert.Equal(CountPer, setsInDb);
                }
            }
        }
Exemplo n.º 3
0
        public void Simple()
        {
            using (var conn = Create())
            {
                var key = Me();

                var session = new ProfilingSession();

                conn.RegisterProfiler(() => session);

                var dbId = TestConfig.GetDedicatedDB();
                var db   = conn.GetDatabase(dbId);
                db.StringSet(key, "world");
                var result = db.ScriptEvaluate(LuaScript.Prepare("return redis.call('get', @key)"), new { key = (RedisKey)key });
                Assert.Equal("world", result.AsString());
                var val = db.StringGet(key);
                Assert.Equal("world", (string)val);
                var s = (string)db.Execute("ECHO", "fii");
                Assert.Equal("fii", s);

                var cmds = session.FinishProfiling();
                var i    = 0;
                foreach (var cmd in cmds)
                {
                    Log("Command {0} (DB: {1}): {2}", i++, cmd.Db, cmd.ToString().Replace("\n", ", "));
                }

                var all = string.Join(",", cmds.Select(x => x.Command));
                Assert.Equal("SET,EVAL,GET,ECHO", all);
                Log("Checking for SET");
                var set = cmds.SingleOrDefault(cmd => cmd.Command == "SET");
                Assert.NotNull(set);
                Log("Checking for GET");
                var get = cmds.SingleOrDefault(cmd => cmd.Command == "GET");
                Assert.NotNull(get);
                Log("Checking for EVAL");
                var eval = cmds.SingleOrDefault(cmd => cmd.Command == "EVAL");
                Assert.NotNull(eval);
                Log("Checking for ECHO");
                var echo = cmds.SingleOrDefault(cmd => cmd.Command == "ECHO");
                Assert.NotNull(echo);

                Assert.Equal(4, cmds.Count());

                Assert.True(set.CommandCreated <= eval.CommandCreated);
                Assert.True(eval.CommandCreated <= get.CommandCreated);

                AssertProfiledCommandValues(set, conn, dbId);

                AssertProfiledCommandValues(get, conn, dbId);

                AssertProfiledCommandValues(eval, conn, dbId);

                AssertProfiledCommandValues(echo, conn, dbId);
            }
        }
Exemplo n.º 4
0
        protected ProfiledCommandEnumerable Log(ProfilingSession session)
        {
            var profile = session.FinishProfiling();

            foreach (var command in profile)
            {
                Writer.WriteLineNoTime(command.ToString());
            }
            return(profile);
        }
Exemplo n.º 5
0
        private void EndProfilingSession(object sender, ProfilingSession session)
        {
            IExecutionSegment executionSegment = sender as Span;
            string            segmentType;

            if (executionSegment is null)
            {
                executionSegment = sender as Transaction;
                if (executionSegment is null)
                {
                    return;
                }

                segmentType = "transaction";
            }
            else
            {
                segmentType = "span";
            }

            try
            {
                // Remove the session. Use session passed to EndProfilingSession rather than the removed session in the event
                // there was an issue in adding or removing the session
                if (!_executionSegmentSessions.TryRemove(executionSegment.Id, out _))
                {
                    _logger.Value.Debug()?.Log(
                        "could not remove profiling session from tracked sessions for {ExecutionSegment} {Id}",
                        segmentType, executionSegment.Id);
                }

                var profiledCommands = session.FinishProfiling();
                _logger.Value.Trace()?.Log(
                    "Finished profiling session for {ExecutionSegment}. Collected {ProfiledCommandCount} commands",
                    executionSegment, profiledCommands.Count());

                foreach (var profiledCommand in profiledCommands)
                {
                    ProcessCommand(profiledCommand, executionSegment);
                }

                _logger.Value.Trace()?.Log(
                    "End profiling session for {ExecutionSegment} {Id}",
                    segmentType, executionSegment.Id);
            }
            catch (Exception e)
            {
                _logger.Value.Error()?.LogException(e,
                                                    "Exception ending profiling session for {ExecutionSegment} {Id}",
                                                    segmentType, executionSegment.Id);
            }
        }
Exemplo n.º 6
0
        public void LowAllocationEnumerable()
        {
            const int OuterLoop = 1000;

            using (var conn = Create())
            {
                var session = new ProfilingSession();
                conn.RegisterProfiler(() => session);

                var prefix = Me();
                var db     = conn.GetDatabase(1);

                var allTasks = new List <Task <string> >();

                foreach (var i in Enumerable.Range(0, OuterLoop))
                {
                    var t =
                        db.StringSetAsync(prefix + i, "bar" + i)
                        .ContinueWith(
                            async _ => (string)(await db.StringGetAsync(prefix + i).ForAwait())
                            );

                    var finalResult = t.Unwrap();
                    allTasks.Add(finalResult);
                }

                conn.WaitAll(allTasks.ToArray());

                var res = session.FinishProfiling();
                Assert.True(res.GetType().IsValueType);

                using (var e = res.GetEnumerator())
                {
                    Assert.True(e.GetType().IsValueType);

                    Assert.True(e.MoveNext());
                    var i = e.Current;

                    e.Reset();
                    Assert.True(e.MoveNext());
                    var j = e.Current;

                    Assert.True(object.ReferenceEquals(i, j));
                }

                Assert.Equal(OuterLoop, res.Count(r => r.Command == "GET" && r.Db > 0));
                Assert.Equal(OuterLoop, res.Count(r => r.Command == "SET" && r.Db > 0));
                Assert.Equal(OuterLoop * 2, res.Count(r => r.Db > 0));
            }
        }
Exemplo n.º 7
0
        public void ProfilingMD_Ex1()
        {
            using (var c = Create())
            {
                IConnectionMultiplexer conn = c;
                var session = new ProfilingSession();
                var prefix  = Me();

                conn.RegisterProfiler(() => session);

                var threads = new List <Thread>();

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

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

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

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

                    threads.Add(thread);
                }

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

                IEnumerable <IProfiledCommand> timings = session.FinishProfiling();

                Assert.Equal(16000, timings.Count());
            }
        }