I have dll method that should be "QoSed" - this method should be called maximum 100 times per second.:
private static extern int ExecTrans(int connectionId);
This method used only in one place in program so it's ok to qos this place.
I need separate "qos counter" for each connectionId
. So ExecTrans(1)
and ExecTrans(2)
should go to different counters.
At first iteration I would like to count how often method is called (for each connectionId
). I.e. I want to have "live statistics". There are two approach:
- allow to exceed limitiation for a short period. for example allow "100 transaction from 0 to 1 second, 100 transaction from 1 to 2 seconds and 200 transactions from 0.5 to 1.5 second".
- at any second interval transactions should not exceed 100.
For now I don't care which of these methods to use but I would select one creating less "overhead". I want qos to add as less "extra-work" as possible because it's trading software sensitive to every 0.1 ms.
As for first approach I think I can use something like that (pseude-code, probably stats
and curStats
should be made thread-safe):
private int[] stats // statistic to display to user
private int[] curStats; // statistic currently collection
OnOneSecondElapsed(object source, ElapsedEventArgs args) {
foreach (conId : connIds) {
stats[conId] = curStats[conId];
curStats[conId] = 0;
}
}
myMethod {
......
ExecTrans(conId);
++curStats[conId];
......
}
As for second approach.... is it possible to make a collection where objects life for exactly one second and after a second disappear? Then every time I will add the next object to a collection unless collection contains 100 objects.
What do you think? I'm not familar with C# library files so probably I'm missing some useful classess, probably you can suggest another approach.
A first approach:
ConcurrentQueue<DateTime>
It should be fairly efficient, but:
I may be wrong but I don't think there's a perfect solution. Basically, the more precise the system is, the more overhead there is. You have to adjust the parameters depending on your needs.
There is a tool called a profiler that does exactly what you are looking for. You run it with your code and it will tell you exactly how much time it spent in each method and how many times each method was called. Here is an old thread about C# profilers. If you're a professional developer you may have already have a company license to a profiler.
In case someone needs to measure rather than throttling... here's a naive approach which gives you a rough estimate:
class A{
private int _calls;
private Stopwatch _sw;
public A(){
_calls = 0;
_sw = new Stopwatch();
_sw.Start();
}
public void MethodToMeasure(){
//Do stuff
_calls++;
if(sw.ElapsedMilliseconds > 1000){
_sw.Stop();
//Save or print _calls here before it's zeroed
_calls = 0;
_sw.Restart();
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With