3
考慮而觀察到的代表傳入的請求......使用測量響應時間觀測
interface IRequest
{
string Id { get; }
}
IObservable<IRequest> requests;
和可觀b和反應......
IObservable<string> responses;
我需要測量才能產生時間響應。我目前的解決方案如下所示。這工作,但我不知道這是否可以簡化或實施更精簡。我還擔心實際響應時間([GEN]
)與觀測響應時間([OBS]
)之間的偏差,如果generatedResponseTime
設置爲低於50ms,則響應時間變得非常重要。
class Program
{
public class Request
{
public string Id { get; set; }
}
static void Main(string[] args)
{
var generatedResponseTime = 100;
var responseSub = new Subject<string>();
IObservable<Request> requests = Observable.Interval(TimeSpan.FromMilliseconds(1000))
.Select(x=> new Request { Id = x.ToString("d") });
IObservable<string> responses = responseSub.AsObservable();
var responseTime = requests
.Where(x => !string.IsNullOrEmpty(x.Id))
.Select(req => new { Id = req.Id, Start = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() })
.SelectMany(req => responses
.Where(reqId => reqId == req.Id)
.Select(_ => new { RequestId = req.Id, ResponseTime = (int)(DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() - req.Start) })
.Take(1))
.Publish()
.RefCount();
requests.ObserveOn(TaskPoolScheduler.Default)
.Subscribe(req =>
{
Console.WriteLine($"[GEN] {req.Id} = {generatedResponseTime} ms");
Thread.Sleep(generatedResponseTime);
responseSub.OnNext(req.Id);
});
responseTime.Subscribe(x => Console.WriteLine($"[OBS] {x.RequestId} = {x.ResponseTime} ms"));
Console.ReadLine();
}
}
您能否爲此提供[mcve]?我可以複製,粘貼和運行以查看代碼的工作方式? – Enigmativity
這是我見過的最好的[mcve]之一。 +1! – Enigmativity
爲什麼你有'.Publish().RecCount()'? – Enigmativity