best way to count how many times per second the method is called
<p>I have dll method that should be "QoSed" - this method should be called maximum 100 times per second.:</p>

<pre><code>    private static extern int ExecTrans(int connectionId);
</code></pre>

<p>This method used only in one place in program so it's ok to qos this place.
I need separate "qos counter" for each <code>connectionId</code>. So <code>ExecTrans(1)</code> and <code>ExecTrans(2)</code> should go to different counters.</p>

<p>At first iteration I would like to count how often method is called (for each <code>connectionId</code>). I.e. I want to have "live statistics". There are two approach:</p>

<pre><code>- 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.
</code></pre>

<p>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.</p>

<p>As for first approach I think I can use something like that (pseude-code, probably <code>stats</code> and <code>curStats</code> should be made thread-safe):</p>

<pre><code>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];
    ......
}
</code></pre>

<p>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.</p>

<p>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.</p>
