📄 ab.c
字号:
BIO_dump(out,(char *)argp,(int)ret); return(ret); } else if (cmd == (BIO_CB_WRITE|BIO_CB_RETURN)) { BIO_printf(out,"write to %08X [%08lX] (%d bytes => %ld (0x%X))\n", bio,argp,argi,ret,ret); BIO_dump(out,(char *)argp,(int)ret); } return(ret);}#ifndef RAND_MAX#include <limits.h>#define RAND_MAX INT_MAX#endifstatic int ssl_rand_choosenum(int l, int h){ int i; char buf[50]; srand((unsigned int)time(NULL)); apr_snprintf(buf, sizeof(buf), "%.0f", (((double)(rand()%RAND_MAX)/RAND_MAX)*(h-l))); i = atoi(buf)+1; if (i < l) i = l; if (i > h) i = h; return i;}void ssl_rand_seed(){ int nDone = 0; int n, l; time_t t; pid_t pid; unsigned char stackdata[256]; /* * seed in the current time (usually just 4 bytes) */ t = time(NULL); l = sizeof(time_t); RAND_seed((unsigned char *)&t, l); nDone += l; /* * seed in the current process id (usually just 4 bytes) */ pid = getpid(); l = sizeof(pid_t); RAND_seed((unsigned char *)&pid, l); nDone += l; /* * seed in some current state of the run-time stack (128 bytes) */ n = ssl_rand_choosenum(0, sizeof(stackdata)-128-1); RAND_seed(stackdata+n, 128); nDone += 128;}int ssl_print_connection_info(bio,ssl)BIO *bio;SSL *ssl;{ SSL_CIPHER *c; int alg_bits,bits; c=SSL_get_current_cipher(ssl); BIO_printf(bio,"Cipher Suite Protocol :%s\n", SSL_CIPHER_get_version(c)); BIO_printf(bio,"Cipher Suite Name :%s\n",SSL_CIPHER_get_name(c)); bits=SSL_CIPHER_get_bits(c,&alg_bits); BIO_printf(bio,"Cipher Suite Cipher Bits:%d (%d)\n",bits,alg_bits); return(1);}int ssl_print_cert_info(bio,x509cert)BIO *bio;X509 *x509cert;{ X509_NAME *dn; char buf[64]; BIO_printf(bio,"Certificate version: %d\n",X509_get_version(x509cert)+1); BIO_printf(bio,"Valid from: "); ASN1_UTCTIME_print(bio, X509_get_notBefore(x509cert)); BIO_printf(bio,"\n"); BIO_printf(bio,"Valid to : "); ASN1_UTCTIME_print(bio, X509_get_notAfter(x509cert)); BIO_printf(bio,"\n"); BIO_printf(bio,"Public key is %d bits\n", EVP_PKEY_bits(X509_get_pubkey(x509cert))); dn=X509_get_issuer_name(x509cert); X509_NAME_oneline(dn, buf, BUFSIZ); BIO_printf(bio,"The issuer name is %s\n", buf); dn=X509_get_subject_name(x509cert); X509_NAME_oneline(dn, buf, BUFSIZ); BIO_printf(bio,"The subject name is %s\n", buf); /* dump the extension list too */ BIO_printf(bio,"Extension Count: %d\n",X509_get_ext_count(x509cert)); return(1);}void ssl_start_connect(struct connection * c){ BIO *bio; X509 *x509cert;#ifdef RSAREF STACK *sk;#else STACK_OF(X509) *sk;#endif int i, count, hdone = 0; char ssl_hostname[80]; /* XXX - Verify if it's okay - TBD */ if (requests < concurrency) requests = concurrency; if (!(started < requests)) return; c->read = 0; c->bread = 0; c->keepalive = 0; c->cbx = 0; c->gotheader = 0; c->rwrite = 0; if (c->ctx) apr_pool_destroy(c->ctx); apr_pool_create(&c->ctx, cntxt); if ((c->ssl=SSL_new(ctx)) == NULL) { BIO_printf(bio_err,"SSL_new failed\n"); exit(1); } ssl_rand_seed(); c->start = apr_time_now(); memset(ssl_hostname, 0, 80); sprintf(ssl_hostname, "%s:%d", hostname, port); if ((bio = BIO_new_connect(ssl_hostname)) == NULL) { BIO_printf(bio_err,"BIO_new_connect failed\n"); exit(1); } SSL_set_bio(c->ssl,bio,bio); SSL_set_connect_state(c->ssl); if (verbosity >= 4) { BIO_set_callback(bio,ssl_print_cb); BIO_set_callback_arg(bio,(void*)bio_err); } while (!hdone) { i = SSL_do_handshake(c->ssl); switch (SSL_get_error(c->ssl,i)) { case SSL_ERROR_NONE: hdone=1; break; case SSL_ERROR_SSL: case SSL_ERROR_SYSCALL: BIO_printf(bio_err,"SSL connection failed\n"); err_conn++; c->state = STATE_UNCONNECTED; if (bad++ > 10) { SSL_free (c->ssl); BIO_printf(bio_err,"\nTest aborted after 10 failures\n\n"); exit (1); } break; case SSL_ERROR_WANT_READ: case SSL_ERROR_WANT_WRITE: case SSL_ERROR_WANT_CONNECT: BIO_printf(bio_err, "Waiting .. sleep(1)\n"); apr_sleep(apr_time_from_sec(1)); c->state = STATE_CONNECTED; c->rwrite = 0; break; case SSL_ERROR_ZERO_RETURN: BIO_printf(bio_err,"socket closed\n"); break; } } if (verbosity >= 2) { BIO_printf(bio_err, "\n"); sk = SSL_get_peer_cert_chain(c->ssl);#ifdef RSAREF if ((count = sk_num(sk)) > 0)#else if ((count = sk_X509_num(sk)) > 0)#endif { for (i=1; i<count; i++) {#ifdef RSAREF x509cert = (X509 *)sk_value(sk,i);#else x509cert = (X509 *)sk_X509_value(sk,i);#endif ssl_print_cert_info(bio_out,x509cert); X509_free(x509cert); } } x509cert = SSL_get_peer_certificate(c->ssl); if (x509cert == NULL) BIO_printf(bio_out, "Anon DH\n"); else { BIO_printf(bio_out, "Peer certificate\n"); ssl_print_cert_info(bio_out,x509cert); X509_free(x509cert); } ssl_print_connection_info(bio_err,c->ssl); SSL_SESSION_print(bio_err,SSL_get_session(c->ssl)); } /* connected first time */ started++; write_request(c);}#endif /* USE_SSL */static void write_request(struct connection * c){ do { apr_time_t tnow = apr_time_now(); apr_size_t l = c->rwrite; apr_status_t e; /* * First time round ? */ if (c->rwrite == 0) {#ifdef USE_SSL if (ssl != 1)#endif apr_socket_timeout_set(c->aprsock, 0); c->connect = tnow; c->rwrite = reqlen; c->rwrote = 0; if (posting) c->rwrite += postlen; } else if (tnow > c->connect + aprtimeout) { printf("Send request timed out!\n"); close_connection(c); return; }#ifdef USE_SSL if (ssl == 1) { apr_size_t e_ssl; e_ssl = SSL_write(c->ssl,request + c->rwrote, l); if (e_ssl != l) { printf("SSL write failed - closing connection\n"); close_connection (c); return; } l = e_ssl; } else#endif e = apr_send(c->aprsock, request + c->rwrote, &l); /* * Bail early on the most common case */ if (l == c->rwrite) break;#ifdef USE_SSL if (ssl != 1)#endif if (e != APR_SUCCESS) { /* * Let's hope this traps EWOULDBLOCK too ! */ if (!APR_STATUS_IS_EAGAIN(e)) { epipe++; printf("Send request failed!\n"); close_connection(c); } return; } c->rwrote += l; c->rwrite -= l; } while (1); totalposted += c->rwrite; c->state = STATE_READ; c->endwrite = apr_time_now();#ifdef USE_SSL if (ssl != 1)#endif { apr_pollfd_t new_pollfd; new_pollfd.desc_type = APR_POLL_SOCKET; new_pollfd.reqevents = APR_POLLIN; new_pollfd.desc.s = c->aprsock; new_pollfd.client_data = c; apr_pollset_add(readbits, &new_pollfd); }}/* --------------------------------------------------------- *//* calculate and output results */static int compradre(struct data * a, struct data * b){ if ((a->ctime) < (b->ctime)) return -1; if ((a->ctime) > (b->ctime)) return +1; return 0;}static int comprando(struct data * a, struct data * b){ if ((a->time) < (b->time)) return -1; if ((a->time) > (b->time)) return +1; return 0;}static int compri(struct data * a, struct data * b){ apr_interval_time_t p = a->time - a->ctime; apr_interval_time_t q = b->time - b->ctime; if (p < q) return -1; if (p > q) return +1; return 0;}static int compwait(struct data * a, struct data * b){ if ((a->waittime) < (b->waittime)) return -1; if ((a->waittime) > (b->waittime)) return 1; return 0;}static void output_results(void){ apr_interval_time_t timetakenusec; float timetaken; endtime = apr_time_now(); timetakenusec = endtime - start; timetaken = ((float)apr_time_sec(timetakenusec)) + ((float)apr_time_usec(timetakenusec)) / 1000000.0F; printf("\n\n"); printf("Server Software: %s\n", servername); printf("Server Hostname: %s\n", hostname); printf("Server Port: %hd\n", port); printf("\n"); printf("Document Path: %s\n", path); printf("Document Length: %" APR_SIZE_T_FMT " bytes\n", doclen); printf("\n"); printf("Concurrency Level: %d\n", concurrency); printf("Time taken for tests: %ld.%03ld seconds\n", (long) apr_time_sec(timetakenusec), (long) apr_time_usec(timetakenusec)); printf("Complete requests: %ld\n", done); printf("Failed requests: %ld\n", bad); if (bad) printf(" (Connect: %d, Length: %d, Exceptions: %d)\n", err_conn, err_length, err_except); printf("Write errors: %ld\n", epipe); if (err_response) printf("Non-2xx responses: %d\n", err_response); if (keepalive) printf("Keep-Alive requests: %ld\n", doneka); printf("Total transferred: %ld bytes\n", totalread); if (posting > 0) printf("Total POSTed: %ld\n", totalposted); printf("HTML transferred: %ld bytes\n", totalbread); /* avoid divide by zero */ if (timetaken) { printf("Requests per second: %.2f [#/sec] (mean)\n", (float) (done / timetaken)); printf("Time per request: %.3f [ms] (mean)\n", (float) (1000 * concurrency * timetaken / done)); printf("Time per request: %.3f [ms] (mean, across all concurrent requests)\n", (float) (1000 * timetaken / done)); printf("Transfer rate: %.2f [Kbytes/sec] received\n", (float) (totalread / 1024 / timetaken)); if (posting > 0) { printf(" %.2f kb/s sent\n", (float) (totalposted / timetaken / 1024)); printf(" %.2f kb/s total\n", (float) ((totalread + totalposted) / timetaken / 1024)); } } if (requests) { /* work out connection times */ long i; apr_time_t totalcon = 0, total = 0, totald = 0, totalwait = 0; apr_time_t meancon, meantot, meand, meanwait; apr_interval_time_t mincon = AB_MAX, mintot = AB_MAX, mind = AB_MAX, minwait = AB_MAX; apr_interval_time_t maxcon = 0, maxtot = 0, maxd = 0, maxwait = 0; apr_interval_time_t mediancon = 0, mediantot = 0, mediand = 0, medianwait = 0; double sdtot = 0, sdcon = 0, sdd = 0, sdwait = 0; for (i = 0; i < requests; i++) { struct data s = stats[i];
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -