Summary After webbench1.5 source code reading, after webbench1.5 source code reading
Webbench Introduction
Webbench is a simple tool written in C language for website stress testing. It can simulate up to 30000 concurrent connections for testing.
You can install and use webbench on your own, or go through this article.
Webbench Execution Process
Command Line Analysis --> build an HTTP request package --> Create a specified number of worker processes --> each worker process continuously sends requests/receives responses to the test website during the test time.
Detailed analysis
1. Command Line parsing.
The getopt library is used here. The getopt library is a common command line Parsing Library in C. It has the following common global variables, struct, and functions.
/* Common global variables */
Extern char* optarg; // point to the acquired option
Extern int optind; // number of options that have been parsed
Extern int opterr; // error code
Extern int optopt;
/* Structure: The main structure when parsing options, to indicate which options are specified */
Struct option {
Const char* name;
Int has_arg;
Int* flag;
Int val;
};
/* Several macros for has_arg */
#define no_argument 0
#define require_argument 1
#define optional_argument 2
/* Several commonly used functions */
Int getopt();
Int getopt_long(int __argc, char* const* __argv, const char* __shortopts, const struct option* __longopts, int* __longind);
Int getopt_long_only(int __argc, char* const* __argv, const char* __shortopts, const struct option* __longopts, int* __longind);
2. Construct an HTTP request package.
The Code calls the build_request function to construct the HTTP request package. Common HTTP Request Packet construction rules:
Request Method URL protocol version \ r \ n
Header field name: value \ r \ n
....
Header field name: value \ r \ n
\ R \ n
\ R \ n
Request data
Request Method: GET, POST, HEAD, PUT, DELETE, TRACE, CONNECT, OPTIONS
Protocol Version: HTTP/1.0 HTTP/1.1
Header: User-agent, Host, Pragma, Connection, etc.
3. Use fork to create a specified number of worker processes, and use pipe to establish communication between the master process and the worker process, so that the master process can collect test information about the sub-process.
/* Use fork to create a multi-process code snippet */
Static int bench(void)
{
...
For (i = 0; i < clients; i++)
{
Pid = fork();
If (pid <= 0) // If this is a child process or fork fails, exit this loop
{
Sleep(1);
Break;
}
}
...
}
/* fork function declaration */
#include <unistd.h>
#include <sys/types.h>
Pid_t fork(void); // pid_t is defined in sys/types.h
/* pipe function declaration */
#include <unistd.h>
Int pipe(int pipefd[2]);
/* fdopen function declaration */
#include <stdio.h>
FILE *fdopen(int fd, const char *mode); // Open the pipe file
4. Each process simulates a client, creates a socket, connects to the test web, and uses alarm to regularly test the duration. During this period, the client continuously sends requests to the web and receives responses.
Note the following points:
A) the socket on the client requires two steps to connect to the web: Create a socket (using the socket () function) and connect to the web (using the connect () function ).
B) alarm timing duration. The input parameter can only be in seconds. When the scheduled time is reached, the system sends a SIGALRM signal to the process where alarm is located. Therefore, before calling alarm, you need to register a signal function sigaction or signal (sigaction is used in the Code) and bind SIGALRM to the callback function. When the system sends SIGALRM, will call the corresponding callback function (alarm_handler is used in the Code) for processing.
The Declaration of the above functions is as follows:
/* socket, connect function declaration */
#include <sys/types.h>
#include <sys/socket.h>
Int socket(int domain, int type, int protocol);
Int connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen);
/* alarm function declaration */
#include <unistd.h>
Unsigned int alarm(unsigned int seconds);
/* sigaction function declaration */
#include <signal.h>
Int sigaction(int signum, const struct sigaction *act, struct sigation *oldact);
Struct sigaction {
Void (*sa_handler) (int);
Void (*sa_sigaction) (int, siginfo_t*, void*);
Sigset_t sa_mask;
Int sa_flag;
Void (*sa_restorer) (void);
};
Complete!
23:39:46