Source: Https://github.com/EZLippi/WebBenchkey global variables:Speed success failed number of failed bytes bytes received benchtime execution time (seconds)build_request () function: generates the request string. A) request the first line, such as: Get/mall http/1.0
- Copy the, Options, or TRACE to request[] according to the type of request method in the command line arguments.
- In the request, add a space next.
- If the command line parameter URL does not contain "://", then it is not a valid URL, error, exit.
- If the URL length exceeds 1500 characters, it is too long to exit.
- If the URL does not begin with "http://", an error occurs.
- If no proxy is set, the
- Take the hostname and port number from the URL, and populate the host and ProxyPort separately
- Fetch path section, add to request
- If you use HTTP 1.0 or 1.1, add "http/1.0" or "http/1.1" to the request. (Note leading spaces)
- If you use HTTP 0.9, you do not need to add this Protocol section.
- Line break: Add "\ r \ n" to request
II) Request subsequent lines:
- If the HTTP version is 1.0 and above, add: "User-agent:webbench 1.5\r\n" to request. Note that there is a line break.
- If you are not using a proxy and the version is above 1.0, add the host header ("Host:" + host) to the request and wrap it.
- If you do not use a proxy and force refresh, add the cache control (not cached) to the request: "pragma:no-cache\r\n".
- If the version is 1.1 and above, add: "connection:close\r\n".
- If the version is 1.0 and above, then add a blank line: "\ r \ n".
- End.
Bench () function: Create individual child processes to send HTTP requests and collect statistics.
- Detects if the destination server (host or proxy in the URL) can connect: Use Connect ().
- Create a pair of pipes: Use pipe ().
- Create a child process with the specified number of clients: Using fork ().
- In a child process:
- Call Benchcore () to perform the test.
- Write three data to the pipeline: speed, number of failures, number of bytes.
- End.
[In parent process]
- Cyclic clients times, repeated execution:
- Reads three items of data from the pipeline into the next subprocess: speed, number of failures, number of bytes.
- If the number of read-in items is less than 3, no data is readable, or the remaining child processes have ended prematurely, jumping out of the loop.
- Total three data
- Print statistical results:
- Speed: Divided into two items
- Total Requests/time = (speed+failed)/, units: times/minutes
- Total Bytes/time, Units: Bytes/sec
- Number of successes: speed
- Number of failures: failed
- End.
the Benchcore () function: sends an HTTP request repeatedly (in a child process).
- Use SIGALRM to install timers: The length of time is from the command line arguments.
- Loop until timer Expires:
- Connect to the server (or proxy server), get FD; If the connection fails, the number of failures failed increased by 1, jumping to the next cycle
- Writes the request string to the FD, and if the write fails, failed increases by 1, closes the FD, jumps to the next cycle
- If the http/0.9 protocol is used, stop sending: Use Shutdown ();
- To wait for the server to respond, the
- The response data is read repeatedly from FD until 0 bytes are read (for reading), and the accumulated bytes are counted to bytes.
- If an error occurs, failed increases by 1, closes the FD, jumps to the next cycle
- Close FD. If an error occurs, the failed increases by 1.
- Cumulative number of successes speed.
- End.
Learn about the Linux API/C library functions:Getopt, Getopt_long () command-line parameter resolution, see "Using getopt () Processing command line Arguments" STRSTR () searching strings for another string STRCHR () searching for a single character from the string header SRRRCHR () Searches for a single character strncasecmp () string comparison from the end of the string, case insensitive strcspn (S1, S2) returns n, indicating that n characters from the S1 string header are not in string S2. For example STRCSPN ("My 1984", "0123456789") = = 3. Fdopen, fprintf, fscanf, setvbuf, and other streaming library I/O functions. Fork, pipe, and process-related functions. The difference between shutdown () and close (). Question: 1, why sleep () can let the child process faster?
/* fork children */ for (i = 0 ; i < clients; i++ = fork (); if (PID <= (pid_t) 0 /* child process or error */ sleep ( 1 ); /* make children faster */ break
Miscellaneous:Host name Maximum length: 64 command line maximum length: 2048/* definition file:/usr/include/asm/param.h */#define MAXHOSTNAMELEN/* max length of hostname */#define Command_line_size 2048
Webbench source code Analysis and experience