Test the MSMQ sending speed

Source: Internet
Author: User
Tags msmq

In some "Small and Medium" applications with relatively high concurrency, if a large amount of data is inserted in the short term, using MSMQ is a good choice (petshop is doing this ), do you want to know how many records can be sent in one second by MSMQ?

 

 

1 Using System;
2 Using System. diagnostics;
3 Using System. messaging;
4 Using System. Collections. Generic;
5
6 Namespace Msglimit
7 {
8 Class Program
9 {
10 Static   Void Main ( String [] ARGs)
11 {
12
13 Messagequeue queue =   New Messagequeue ( " Jimmyibm \ private $ \ msg " );
14
15 Stopwatch stopwatch =   New Stopwatch ();
16
17 Stopwatch. Start ();
18
19 Const   Int Max_number =   5000 ;
20
21 For ( Int I =   1 ; I <= Max_number; I ++ )
22 {
23 Message msg =   New Message ();
24 // Set some test values here.
25 MSG. Label = I. tostring ();
26 MSG. Body = I. tostring (). padleft ( 8 , ' 0 ' );
27 // MSG. Recoverable = true; // Set message restorability (that is, after the server is restarted, the message is still there, but enabling this option will double the sending time, because the "recoverable" mechanism is to generate a text file on the server hard disk first, i/O operation with one more file write operation)
28 Queue. Send (MSG );
29 }
30
31 Stopwatch. Stop ();
32
33 Console. writeline ( " {0} messages are sent successfully. The total time consumed is {1} seconds. The average number of messages sent per second is {2! " , Max_number, stopwatch. elapsedmilliseconds /   1000 , Max_number / (Stopwatch. elapsedmilliseconds /   1000 ));
34
35 Int J =   0 ;
36
37 Stopwatch. Reset ();
38 Stopwatch. Start ();
39
40 // List <message> listmsg = new list <message> ();
41 While ( True )
42 {
43 Try
44 {
45 Message msg = Queue. Receive ( New Timespan ( 0 , 0 , 0 , 0 , 1 ));
46
47 // You can do some things, such as batch database entry for every 50 records (this is much faster than inserting data directly into the database one by one)
48 // Listmsg. Add (MSG );
49 // If (listmsg. Count> = 50)
50 // {
51 // // Import data in batches using transactions
52 // Listmsg. Clear (); // Clear after the operation is complete
53 // }
54 J ++ ;
55 System. Threading. thread. Sleep ( 1 );
56
57 }
58 Catch
59 {
60 Stopwatch. Stop ();
61 Console. writeline ( " The receipt of {0} records is complete. It takes {1} seconds! " , J. tostring (), stopwatch. elapsedmilliseconds /   1000 );
62 Break ;
63 }
64 }
65
66 Console. Readline ();
67 }
68 }
69 }
70
71

 

The result of running on my IBM t60 is about 2500 records per second (that is to say, 2500 orders can be smoothly placed in one second, and it should be enough in Small and Medium Sized Shopping Systems)

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.