gpt4 book ai didi

c# - StackExchange.Redis 异步调用比同步调用慢

转载 作者:行者123 更新时间:2023-11-30 16:56:29 28 4
gpt4 key购买 nike

我知道异步方法的目的不是提高性能,但我发现 StackExchange.Redis 上的异步方法比同步方法花费的时间很多

public static async Task<bool> GetScoresFromSetAsync(int score, string name)
{
string redisConnection = ConfigurationManager.AppSettings["RedisAccount"].ToString();
ConnectionMultiplexer connection = ConnectionMultiplexer.Connect(redisConnection);
IDatabase _cache = connection.GetDatabase();

List<string> scores = new List<string>();

var resultAsync = await _cache.SortedSetRangeByScoreAsync(name, score, score);
var result = _cache.SortedSetRangeByScore(name score, score);
return true;
}

异步调用大约需要 5000 毫秒,而非异步调用平均需要大约 30 毫秒。我的 redis 托管在 azure 上。有什么想法吗?

编辑:我在这里谈论的是单个请求。 SortedSetRangeByScore API 调用在 30 毫秒内返回,而 SortedSetRangeByScoreAsync API 调用在 5000 毫秒内返回。

最佳答案

想知道您如何测量延迟以进行比较?我尝试使用以下代码对其进行测量,结果发现 SE.Redis 异步与同步所花费的时间非常接近。我希望这会有所帮助。

我的客户端代码在 Azure Iaas VM 上运行并连接到同一区域中的 Azure Redis 缓存。

测量排序集长度为 10000、迭代次数为 10000 的同步与异步

平均 1.41190622 毫秒完成 10000 个同步调用

平均 1.43989741 毫秒完成 10000 个异步调用

测量排序集长度为 100000、迭代 1 的同步与异步

1 次同步调用平均用时 0.9513 毫秒

1 次异步调用平均耗时 1.1436 毫秒

using StackExchange.Redis;

using System;

using System.Collections.Generic;

using System.Diagnostics;

using System.Threading;

using System.Threading.Tasks;

namespace RedisLatency

{

class Program

{

private const string host = "myazurecache.redis.cache.windows.net";

private const string password = "password";

private static int sortedsetlength;

private static int iterations;

private static IDatabase _cache;

static void Main(string[] args)

{

sortedsetlength = Int32.Parse(args[0]);

iterations = Int32.Parse(args[1]);

CreateMultiplexer(host,password);

PopulateTestData();

RunTestSync();

RunTestAsync();

}

private static void CreateMultiplexer(string host, string password)

{

Console.WriteLine("Measuring sync vs async for sorted set length {0}, iteration {1}", sortedsetlength,iterations);

ConfigurationOptions configoptions = new ConfigurationOptions();

configoptions.EndPoints.Add(host);

configoptions.Password = password;

configoptions.Ssl = true;

ConnectionMultiplexer connection = ConnectionMultiplexer.Connect(configoptions);

_cache = connection.GetDatabase();
}

private static void PopulateTestData()

{

for (int i = 0; i < sortedsetlength; i++)

{
_cache.SortedSetAdd("testsorted", "user" + i, i);
}
}

static void RunTestSync()

{

for (int warmup = 0; warmup < 100; warmup++)

{

MeasureSync();

}

Stopwatch sw = Stopwatch.StartNew();

for (int i = 0; i < iterations;i++ )

{
MeasureSync();
}

sw.Stop();

Console.WriteLine("{0} sync calls completed in average {1} ms", iterations, sw.Elapsed.TotalMilliseconds/iterations);
}

async static void RunTestAsync()

{

//warm up

for (int warmup = 0; warmup < 100; warmup++)
{
MeasureAsync().Wait();
}

Stopwatch sw = Stopwatch.StartNew();

for (int i = 0; i < iterations; i++)

{

MeasureAsync().Wait();

}

sw.Stop();

Console.WriteLine("{0} async calls completed in average {1} ms", iterations, sw.Elapsed.TotalMilliseconds/iterations);

}

static public void MeasureSync()

{

var result = _cache.SortedSetRangeByScore("testset", 1.0, sortedsetlength / 1.0);

}

async static public Task MeasureAsync()
{
var result = await _cache.SortedSetRangeByScoreAsync("testset", 1.0, sortedsetlength / 1.0);
}
}

}

关于c# - StackExchange.Redis 异步调用比同步调用慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27981299/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com