Webbench source code Analysis and experience

Source: Internet
Author: User

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
    1. Copy the, Options, or TRACE to request[] according to the type of request method in the command line arguments.
    2. In the request, add a space next.
    3. If the command line parameter URL does not contain "://", then it is not a valid URL, error, exit.
    4. If the URL length exceeds 1500 characters, it is too long to exit.
    5. If the URL does not begin with "http://", an error occurs.
    6. If no proxy is set, the
      1. Take the hostname and port number from the URL, and populate the host and ProxyPort separately
      2. Fetch path section, add to request
    7. If you use HTTP 1.0 or 1.1, add "http/1.0" or "http/1.1" to the request. (Note leading spaces)
      1. If you use HTTP 0.9, you do not need to add this Protocol section.
    8. Line break: Add "\ r \ n" to request
II) Request subsequent lines:
    1. 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.
    2. 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.
    3. If you do not use a proxy and force refresh, add the cache control (not cached) to the request: "pragma:no-cache\r\n".
    4. If the version is 1.1 and above, add: "connection:close\r\n".
    5. If the version is 1.0 and above, then add a blank line: "\ r \ n".
    6. End.
Bench () function: Create individual child processes to send HTTP requests and collect statistics.
    1. Detects if the destination server (host or proxy in the URL) can connect: Use Connect ().
    2. Create a pair of pipes: Use pipe ().
    3. Create a child process with the specified number of clients: Using fork ().
    4. In a child process:
      1. Call Benchcore () to perform the test.
      2. Write three data to the pipeline: speed, number of failures, number of bytes.
      3. End.
[In parent process]
    1. Cyclic clients times, repeated execution:
      1. Reads three items of data from the pipeline into the next subprocess: speed, number of failures, number of bytes.
      2. 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.
      3. Total three data
    2. Print statistical results:
      1. Speed: Divided into two items
        1. Total Requests/time = (speed+failed)/, units: times/minutes
        2. Total Bytes/time, Units: Bytes/sec
      2. Number of successes: speed
      3. Number of failures: failed
    3. End.
the Benchcore () function: sends an HTTP request repeatedly (in a child process).
    1. Use SIGALRM to install timers: The length of time is from the command line arguments.
    2. Loop until timer Expires:
      1. 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
      2. Writes the request string to the FD, and if the write fails, failed increases by 1, closes the FD, jumps to the next cycle
      3. If the http/0.9 protocol is used, stop sending: Use Shutdown ();
      4. To wait for the server to respond, the
        1. The response data is read repeatedly from FD until 0 bytes are read (for reading), and the accumulated bytes are counted to bytes.
        2. If an error occurs, failed increases by 1, closes the FD, jumps to the next cycle
      5. Close FD. If an error occurs, the failed increases by 1.
      6. Cumulative number of successes speed.
    3. 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

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.