Share a second counter

Source: Internet
Author: User

At this time, we need to calculate the number of processes processed in one second. Although. NET provides some timing methods, such as stopwatch computingCodeThe time consumed by the running, but the count per second is not provided. In order to solve this problem easily, a simple second counter is implemented to complete these functions. By the way, this small class is also shared.

Use the second counter

Suppose we now have two thread processing methods, and the method is constantly processing something. We need to calculate the number of processing operations per second for each method.

 
Mcounter = new secondcounter (); mtest1 = mcounter. add ("test1"); mtest2 = mcounter. add ("Test2"); mcounter. tick + = (e) => {console. writeline ("{0 }:{ 1}/S <{2}>", E. name, E. value, E. count) ;}; mcounter. open ();

Create a counter, add the counters test1 and Test2 respectively, and set the counter trigger event. The code above prints information about each counter, including the total number of executions and the number of executions per second. after the related information is set, open the counter. after adding a counter, you can use it in the method:

Static void test1 (object state) {While (true) {mtest1.add (1); system. threading. thread. sleep (1) ;}} static void Test2 (object state) {While (true) {mtest2.add (1); system. threading. thread. sleep (1 );}}

The following two methods are called by different threads to check the Count output:

 
System. Threading. threadpool. queueuserworkitem (test1); system. Threading. threadpool. queueuserworkitem (Test2 );

In this way, we can calculate the approximate number of executions per second for each method. The counter also provides another version of the add (value) method for calculating other values, for example, the byte received by the current socket. we can also calculate the number of database operations per second.

Mcounter = new secondcounter (); mstate = mcounter. add ("Smark. data Query "); mcounter. tick + = (e) => {console. writeline ("{0 }:{ 1}/S <{2}>", E. name, E. value, E. count) ;}; mcounter. open (); system. threading. threadpool. queueuserworkitem (smarkdataquery); system. threading. threadpool. queueuserworkitem (smarkdataquery); system. threading. threadpool. queueuserworkitem (smarkdataquery); system. threading. threadpool. queueuserworkitem (smarkdataquery); static void smarkdataquery (object state) {While (true) {expression exp = new expression (); (Order. orderid = exp. getvalues <int32, order> (Order. orderid, 2, 20 )). list <order> (); mstate. add ();}}

Complete counter code
/// <Summary> // copyright henryfan 2012 // Email: henryfan@msn.com // homepage: http://www.ikende.com // createtime: 21:56:05 // </Summary> public class secondcounter: idisposable {interface icounter {void process ();} public event action <counteritem> tick; private list <counteritem> mitems = new list <counteritem> (); public class counteritem: icounter {private long mcount = 0; private Long mlastcount; Public int value {Get; private set;} public string name {Get; set;} public void add () {system. threading. interlocked. increment (ref mcount);} public Long Count {get {return mcount;} public void add (long value) {system. threading. interlocked. add (ref mcount, value);} void icounter. process () {value = (INT) (mcount-mlastcount); mlastcount = mcount;} public override strin G tostring () {return string. format ("{0 }:\ t {1}/s", name, value) ;}} public counteritem add (string name) {counteritem item = new counteritem (); item. name = Name; mitems. add (item); Return item;} public void open () {mtimer = new system. threading. timer (oncallback, this, 10,980);} private void oncallback (object state) {for (INT I = 0; I <mitems. count; I ++) {(icounter) mitems [I]). process ();} t Ry {If (tick! = NULL) {for (INT I = 0; I <mitems. count; I ++) {tick (mitems [I]) ;}}} catch {}} private system. threading. timer mtimer; private bool misdisposed = false; Public void dispose () {lock (this) {If (! Misdisposed) {If (mtimer! = NULL) mtimer. Dispose ();}}}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.