c# - BookSleeve - 设置哈希时性能不佳

标签 c# redis booksleeve

我正在更新我的网络服务以使用最新的 BookSleeve图书馆,1.3.38。以前我用的是 1.1.0.7

在做一些基准测试时,我注意到使用新版本的 BookSleeve 在 Redis 中设置哈希比旧版本慢很多倍。请考虑以下 C# 基准测试代码:

public void TestRedisHashes()
{
  int numItems = 1000; // number of hash items to set in redis 
  int numFields = 30; // number of fields in each redis hash
  RedisConnection redis = new RedisConnection("10.0.0.01", 6379);
  redis.Open();

  // wait until the connection is open
  while (!redis.State.Equals(BookSleeve.RedisConnectionBase.ConnectionState.Open)) { }

  Stopwatch timer = new Stopwatch();
  timer.Start();
  for (int i = 0; i < numItems; i++)
  {
    string key = "test_" + i.ToString();

    for (int j = 0; j < numFields; j++)
    {
      // set a value for each field in the hash
      redis.Hashes.Set(0, key, "field_" + j.ToString(), "testdata");
    }
    redis.Keys.Expire(0, key, 30); // 30 second ttl
  }
  timer.Stop();

  Console.WriteLine("Elapsed time for hash writes: {0} ms", timer.ElapsedMilliseconds);
}

BookSleeve 1.1.0.7 将 1000 个哈希值设置到 Redis 2.6 大约需要 20 毫秒,而 1.3.38 大约需要 400 毫秒。这慢了 20 倍!我测试过的 BookSleeve 1.3.38 的所有其他部分与旧版本一样快或更快。我还使用 Redis 2.4 尝试了相同的测试,并将所有内容包装在事务中。在这两种情况下,我都获得了相似的表现。

有没有人注意到这样的事情?我一定是做错了什么……我是否使用新版本的 BookSleeve 正确设置了哈希值?这是执行即发即弃命令的正确方法吗?我看过了the unit tests作为如何使用哈希的示例,但无法找到我所做的不同之处。在这种情况下,最新版本是否可能更慢?

最佳答案

要实际测试整体速度,您需要添加等待最后一条消息被处理的代码,例如:

  Task last = null;
  for (int i = 0; i < numItems; i++)
  {
    string key = "test_" + i.ToString();

    for (int j = 0; j < numFields; j++)
    {
      // set a value for each field in the hash
      redis.Hashes.Set(0, key, "field_" + j.ToString(), "testdata");
    }
    last = redis.Keys.Expire(0, key, 30); // 30 second ttl
  }
  redis.Wait(last);

否则,您所计时的只是调用Set/Expire 的速度。在这种情况下,这可能很重要。您会看到,在 1.1.0.7 中,所有消息都立即放入队列中,然后一个单独的专用写入器线程获取该消息并将其写入流中。在 1.3.38 中,专用的编写器线程消失了(出于各种原因)。因此,如果套接字可用,调用线程将写入底层流(如果套接字正在使用中,则有一种机制来处理它)。更重要的是,在您针对 1.1.0.7 的原始测试中,可能实际上还没有发生任何有用的工作 - 无法保证工作在套接字附近的任何地方,等等。

在大多数情况下,这不会导致任何开销(并且在摊销时开销会减少),但是:在您的情况下,您可能有效 缓冲区运行不足——在 1.1.0.7 中,你会很快填充缓冲区真的,并且工作线程可能总是会发现更多等待消息——所以它不会刷新流直到结束;在 1.3.38 中,它可能会在 消息之间刷新。所以:让我们解决这个问题:

Task last = null;
redis.SuspendFlush();
try {
  for (int i = 0; i < numItems; i++)
  {
    string key = "test_" + i.ToString();

    for (int j = 0; j < numFields; j++)
    {
      // set a value for each field in the hash
      redis.Hashes.Set(0, key, "field_" + j.ToString(), "testdata");
    }
    last = redis.Keys.Expire(0, key, 30); // 30 second ttl
  }
}
finally {
  redis.ResumeFlush();
}
redis.Wait(last);

SuspendFlush()/ResumeFlush() 对是在单个线程上调用大量操作以避免任何额外操作的理想选择冲洗。要复制智能感知注释:

//
// Summary:
// Temporarily suspends eager-flushing (flushing if the write-queue becomes
// empty briefly). Buffer-based flushing will still occur when the data is full.
// This is useful if you are performing a large number of operations in close
// duration, and want to avoid packet fragmentation. Note that you MUST call
// ResumeFlush at the end of the operation - preferably using Try/Finally so
// that flushing is resumed even upon error. This method is thread-safe; any
// number of callers can suspend/resume flushing concurrently - eager flushing
// will resume fully when all callers have called ResumeFlush.
//
// Remarks:
// Note that some operations (transaction conditions, etc) require flushing
// - this will still occur even if the buffer is only part full.

请注意,在大多数高吞吐量场景中,多个操作来自多个线程:在这些场景中,来自并发线程的任何工作将自动以最小化线程数量的方式排队。

关于c# - BookSleeve - 设置哈希时性能不佳,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18690140/

相关文章:

java - Redis:获取JedisConnectionException:连接到远程服务器时无法从池中获取资源

python - redis.exceptions.ConnectionError : Error 97 connecting to localhost:6379. 协议(protocol)不支持的地址族

redis - 有人能准确解释 Booksleeve 和 Redis 如何协同工作以及它在 SignalR 应用程序中的应用吗?

c# - 在 Azure VM 中使用 Booksleeve Redis 客户端时出现 Redis 连接错误

c# - 在 windows azure 中使用 Redis 实现进程外缓存

c# - 在 WPF MVVM 中使用 ICollectionView 更新 DataGrid

c# - 如果我需要自定义 getter/setter,我可以省略字段创建吗?

c# - 线程、事件和 GUI

c# - ChannelFactory.Open VS IClientChannel.Open

python - 如何在多线程程序中更高效地使用redis-py?