1

在将数据保存到 Redis 缓存时,我的性能非常差。

设想 :

1)利用Redis缓存服务(微软Azure提供)。
2) 在 Azure 上创建的虚拟机中运行代码。
3)VM和缓存服务都创建在同一个位置

代码片段:

    public void MyCustomFunction()
    {
        Stopwatch totalTime = Stopwatch.StartNew();

        RedisEndpoint config = new RedisEndpoint();
        config.Ssl = true;
        config.Host = "redis.redis.cache.windows.net";
        config.Password = Form1.Password;
        config.Port = 6380;
        RedisClient client = new RedisClient(config);

        int j = 0;

        for (int i = 0; i < 500; i++)
        {
            var currentStopWatchTime = Stopwatch.StartNew();
            var msgClient = client.As<Message>();

            List<string> dataToUpload = ClientData.GetRandomData();
            string myCachedItem_1 = dataToUpload[1].ToString();

            Random ran = new Random();
            string newKey = string.Empty;
            newKey = Guid.NewGuid().ToString();

            Message newItem = new Message
            {
                Id = msgClient.GetNextSequence(), // Size : Long variable
                //Id = (long)ran.Next(),
                Key = j.ToString(),             // Size: Int32 variable
                Value = newKey,                 // Size : Guid string variable
                Description = myCachedItem_1    // Size : 5 KB
            };

            string listName = ran.Next(1, 6).ToString();
            msgClient.Lists[listName].Add(newItem);
            //msgClient.Store(newItem);

            Console.WriteLine("Loop Count : " + j++ + " , Total no. of items in List : " + listName + " are : " + msgClient.Lists[listName].Count);

            Console.WriteLine("Current Time: " + currentStopWatchTime.ElapsedMilliseconds + " Total time:" + totalTime.ElapsedMilliseconds);

            Console.WriteLine("Cache saved");
        }
    }

性能(保存时):

注意:(所有时间以毫秒为单位)

循环次数:0,总次数。列表中的项目数:2 是:1 当前时间:310 总时间:342 缓存保存循环计数:1,总数。列表中的项目数:3 是:1 当前时间:6 总时间:349 缓存保存循环计数:2,总数。列表中的项目数:5 是:1 当前时间:3 总时间:353 缓存保存循环计数:3,总数。列表中的项目数:5 是:2 当前时间:3 总时间:356 缓存保存循环计数:4,总数。列表中的项目数:5 是:3 当前时间:3 总时间:360 缓存已保存

. . . . .

循环次数:330,总次数。列表中的项目数:4 是:69 当前时间:2 总时间:7057 缓存保存
循环计数:331,总数。列表中的项目数:4 是:70 当前时间:3 总时间:7061 缓存保存
循环计数:332,总数。列表中的项目数:4 是:71 当前时间:2 总时间:7064 缓存已保存

性能(获取时)

清单 : 1 项目数量 : 110 时间 : 57

清单 : 2 项目数量 : 90 时间 : 45

清单 : 3 件数 : 51 时间 : 23

清单 : 4 件数 : 75 时间 : 32

清单 : 5 件数 : 63 时间 : 33

4

1 回答 1

0

如果您正在批量处理,您应该考虑减少同步网络请求的数量以减少延迟,这将成为与网络服务通信时的主要性能问题。

对于此示例,您在调用时正在读取:

msgClient.GetNextSequence();

并在您制作时写下:

msgClient.Lists[listName].Add(newItem);

在单个线程中总共有 1000 个同步请求/回复网络请求,其中每个操作都是依赖的,并且必须在发送下一个操作之前完成,这就是为什么网络延迟将成为性能问题的主要来源,你应该看优化。

批处理请求

如果您正在处理批处理请求,则可以通过在单个请求中获取所有 id 并使用AddRange()批处理操作存储它们来减少读取和写入次数,从而大大优化这一点,例如:

var redisMessages = Redis.As<Message>();
const int batchSize = 500;

//fetch next 500 sequence of ids in a single request
var nextIds = redisMessages.GetNextSequence(batchSize); 

var msgBatch = batchSize.Times(i => 
    new Message {
        Id = nextIds - (batchSize - i) + 1,
        Key = i.ToString(), 
        Value = Guid.NewGuid().ToString(),
        Description = "Description"
    });

//Store all messages in a single multi operation request
redisMessages.Lists[listName].AddRange(msgBatch);

这会将 1000 个 redis 操作压缩为 2 个操作。

如果您需要,您可以通过以下方式获取所有消息:

var allMsgs = redisMessages.Lists[listName].GetAll();

GetRange(startingFrom,endingAt)或使用API 的特定范围。

于 2015-03-29T19:57:15.933 回答