12.38

    code in directory chapter12/code/12.38

    files:

    • sbuf.h, sbuf.c: prethreading package from section 12.5.4 in book. differences
      are that add new functions sbuf_full, sbuf_empty.
    • tiny.h, tiny.c: origin tiny server from section 11.6 in book. differences are
      we seperate function declarations into a .h file
    • main.c: listen new connections, create threads and adjust threads number dynamicly

    see new functions in sbuf, sbuf_full and sbuf_empty

    1. /* Empty buf? */
    2. int sbuf_empty(sbuf_t *sp) {
    3. int e;
    4. P(&sp->mutex); /* Lock the buffer */
    5. e = sp->front == sp->rear;
    6. V(&sp->mutex); /* Lock the buffer */
    7. return e;
    8. }
    9. /* Full buf? */
    10. int sbuf_full(sbuf_t *sp) {
    11. int f;
    12. P(&sp->mutex); /* Lock the buffer */
    13. f = (sp->rear - sp->front) == sp->n;
    14. V(&sp->mutex); /* Lock the buffer */
    15. return f;
    16. }

    main.c

    1. !INCLUDE "./code/12.38/main.c"

    run server

    1. (cd chapter12/code/12.38; make && ./main)

    open another terminal and benchmark it

    1. wrk -d4 http://localhost:5000

    output

    1. Running 4s test @ http://localhost:5000
    2. 2 threads and 10 connections
    3. Thread Stats Avg Stdev Max +/- Stdev
    4. Latency 130.44us 183.40us 9.03ms 98.06%
    5. Req/Sec 11.08k 5.09k 17.78k 53.66%
    6. 90380 requests in 4.10s, 19.74MB read
    7. Requests/sec: 22039.72
    8. Transfer/sec: 4.81MB