Or WebService project testing:
Simulate as many concurrency as possible to test the performance of WebService. I used a multi-threaded method to simulate it. First, I will introduce the idea: to simulate 1000 concurrent data records per second, a slightly larger calculation method: set the maximum number of threads that can be processed to 1000. When the number of threads exceeds 1000, suspend it for 1 ms. The data has a rough calculation (the processing time is not counted, and the ideal state). 1 public class threadtest
2 {
3 private int poolflag = 0; // tag
4 private const int amountthread = 2000; // total number of threads
5 private int maxthread = 1000; // maximum number of executable threads
6 private mutex muxconsole = new mutex ();
7
8 Public void teststart (INT ithread)
9 {
10 if (ithread> 0)
11 {
12 maxthread = ithread;
13}
14 For (INT I = 0; I <amountthread; I ++)
15 {
16 // create a specified number of threads
17 // The thread calls the run method.
18 // start the thread
19 thread TRD = new thread (New threadstart (run ));
20
21 TRD. Start ();
22}
23}
24
25 public void run ()
26 {
27 muxconsole. waitone (); // block the queue
28 interlocked. increment (ref poolflag); // tag + 1
29 If (poolflag! = Maxthread) // determines whether it is equal to the upper limit
30 {
31 getservice (); // processing method, requesting WebService
32
33 muxconsole. releasemutex (); // If the thread does not reach the maximum number of executable threads, activate the thread to bring the subsequent threads in.
34}
35 else
36 {
37 thread. Sleep (1000/maxthread); // simulation execution, sleep time, and ideal state calculation can control the number of simulated requests per second
38}
39
40 // Mark-1
41 interlocked. decrement (ref poolflag );
42 }}
The following is a reference to others' experiences: C # provides the mutex and interlocked thread-related classes in the threading namespace ~!
Mutex provides two instance methods: witeone and releasemutex ~
Witeone is used to "block the current thread and provide atomic operations on the thread"
That is to say, when a thread encounters witeone, if there is no thread in witeone to operate, the thread goes in and operates.
When there is a thread in it, all the threads to this end need to wait in queue for the thread to complete the execution ~
The end mark for controlling such operations is to use the releasemutex method!
Just like witeone is a lock ~ Releasemutex is a key.
When 10 people see this door, the first person who arrives at the door will see no one in the room, then he will go in and lock the door at the same time ~
The person behind will naturally wait at the door. When this person finishes the task in the room, he will open the door with the key!
After going out, the lock will be handed over to the second comrade in the queue at the door. The second comrade will perform the same operation.
If the first comrade does not give the right to use the task to the second person after executing the task, he will quit directly.
The room is naturally empty, and the door is locked ~ Don't worry ~ The door will automatically open, as long as the previous person is no longer in the room ~
Next, let's talk about interlocked. The official explanation is "increment or decrease an atomic operation on a variable and save it"
The concept of atomic operations is that there is only one thread operating on this variable ~ Operations not allowed by other threads
When an atomic operation is performed on a variable, the variable is locked, and other threads cannot access the variable. They can only wait for the variable to be unlocked.
My summary:
The getservice () method uses new to request web services. In this way, only concurrent "requests" can be implemented for multiple Web Services, the function of "connecting" and "request" multiple times cannot be simulated.
Find the method for creating an HTTP connection:
Webrequest myrequest = webrequest. Create ("http: // mywebservicedomain ");
Getservice ();
Webresponse myrespone = myrequest. getresponse ();
Myrespone. Close ();
No test results yet. Try again tomorrow. Haha