性能分析

StackExchange.Redis公开了一些方法和类型来启用性能分析。 由于其异步和多路复用 表现分析是一个有点复杂的主题。

接口

分析接口由 IProfiler, ConnectionMultiplexer.RegisterProfiler(IProfiler)ConnectionMultiplexer.BeginProfiling(object)ConnectionMultiplexer.FinishProfiling(object)IProfiledCommand 组合而成。

你可以用一个 ConnectionMultiplexer 实例注册一个 IProfiler ,它不能被改变。你可以通过调用 BeginProfiling(object) 来开始分析一个给定的上下文对象(例如,线程,Http请求等等),调用 FinishProfiling(object) 来完成。 FinishProfiling(object) 返回一个 IProfiledCommand 集合的对象,它包含了通过 (Begin|Finish)Profiling 调用和给定上下文配置好的 ConnectionMultiplexer 对象发送到 redis 的所有命令的时间信息。

应该使用什么“上下文”对象是应用程序来确定的。

可用时间

StackExchange.Redis显示有关以下内容的信息:

  • 涉及到的redis服务器
  • 正在查询的redis数据库
  • redis命令运行
  • 用于路由命令的标志
  • 命令的初始创建时间
  • 使命令进入队列所需的时间
  • 命令入队后,发送命令需要多长时间
  • 发送命令后,从redis接收响应需要多长时间
  • 收到响应后处理响应所需的时间
  • 如果命令是响应集群 ASK 或 MOVED 响应而发送的
    • 如果是,原始命令是什么

TimeSpan 有较高的精度,如果运行时支持。 DateTime 准确度如 DateTime.UtcNow

选择上下文

由于StackExchange.Redis的异步接口,分析需要外部协助将相关的命令组合在一起。 这是 通过提供上下文对象,当你开始和结束profiling(通过 BeginProfiling(object)FinishProfiling(object) 方法),当一个命令被发送(通过 IProfiler 接口的 GetContext() 方法)实现的。

一个将许多不同线程发出的命令关联在一起的 toy 示例:

  1. class ToyProfiler : IProfiler
  2. {
  3. public ConcurrentDictionary<Thread, object> Contexts = new ConcurrentDictionary<Thread, object>();
  4. public object GetContext()
  5. {
  6. object ctx;
  7. if(!Contexts.TryGetValue(Thread.CurrentThread, out ctx)) ctx = null;
  8. return ctx;
  9. }
  10. }
  11. // ...
  12. ConnectionMultiplexer conn = /* initialization */;
  13. var profiler = new ToyProfiler();
  14. var thisGroupContext = new object();
  15. conn.RegisterProfiler(profiler);
  16. var threads = new List<Thread>();
  17. for (var i = 0; i < 16; i++)
  18. {
  19. var db = conn.GetDatabase(i);
  20. var thread =
  21. new Thread(
  22. delegate()
  23. {
  24. var threadTasks = new List<Task>();
  25. for (var j = 0; j < 1000; j++)
  26. {
  27. var task = db.StringSetAsync("" + j, "" + j);
  28. threadTasks.Add(task);
  29. }
  30. Task.WaitAll(threadTasks.ToArray());
  31. }
  32. );
  33. profiler.Contexts[thread] = thisGroupContext;
  34. threads.Add(thread);
  35. }
  36. conn.BeginProfiling(thisGroupContext);
  37. threads.ForEach(thread => thread.Start());
  38. threads.ForEach(thread => thread.Join());
  39. IEnumerable<IProfiledCommand> timings = conn.FinishProfiling(thisGroupContext);

最后,timings 将包含16,000个 IProfiledCommand 对象 - 每个发送给redis的命令对应一个对象。

如果相反,你像下面这样做:

  1. ConnectionMultiplexer conn = /* initialization */;
  2. var profiler = new ToyProfiler();
  3. conn.RegisterProfiler(profiler);
  4. var threads = new List<Thread>();
  5. var perThreadTimings = new ConcurrentDictionary<Thread, List<IProfiledCommand>>();
  6. for (var i = 0; i < 16; i++)
  7. {
  8. var db = conn.GetDatabase(i);
  9. var thread =
  10. new Thread(
  11. delegate()
  12. {
  13. var threadTasks = new List<Task>();
  14. conn.BeginProfiling(Thread.CurrentThread);
  15. for (var j = 0; j < 1000; j++)
  16. {
  17. var task = db.StringSetAsync("" + j, "" + j);
  18. threadTasks.Add(task);
  19. }
  20. Task.WaitAll(threadTasks.ToArray());
  21. perThreadTimings[Thread.CurrentThread] = conn.FinishProfiling(Thread.CurrentThread).ToList();
  22. }
  23. );
  24. profiler.Contexts[thread] = thread;
  25. threads.Add(thread);
  26. }
  27. threads.ForEach(thread => thread.Start());
  28. threads.ForEach(thread => thread.Join());

perThreadTimings 最终会有1000个 IProfilingCommand 的16个,键由 Thread 发出。

不再看玩具示例,这里是如何在一个MVC5应用程序中配置 StackExchange.Redis。

首先针对你的 ConnectionMultiplexer 对象注册以下 IProfiler

  1. public class RedisProfiler : IProfiler
  2. {
  3. const string RequestContextKey = "RequestProfilingContext";
  4. public object GetContext()
  5. {
  6. var ctx = HttpContext.Current;
  7. if (ctx == null) return null;
  8. return ctx.Items[RequestContextKey];
  9. }
  10. public object CreateContextForCurrentRequest()
  11. {
  12. var ctx = HttpContext.Current;
  13. if (ctx == null) return null;
  14. object ret;
  15. ctx.Items[RequestContextKey] = ret = new object();
  16. return ret;
  17. }
  18. }

然后,将以下内容添加到 Global.asax.cs 文件:

  1. protected void Application_BeginRequest()
  2. {
  3. var ctxObj = RedisProfiler.CreateContextForCurrentRequest();
  4. if (ctxObj != null)
  5. {
  6. RedisConnection.BeginProfiling(ctxObj);
  7. }
  8. }
  9. protected void Application_EndRequest()
  10. {
  11. var ctxObj = RedisProfiler.GetContext();
  12. if (ctxObj != null)
  13. {
  14. var timings = RedisConnection.FinishProfiling(ctxObj);
  15. // do what you will with `timings` here
  16. }
  17. }

这个实现将所有redis命令(包括 async / await -ed 命令)与触发它们的http请求分组。

查看原文

返回主页