2014-02-28 01:10:42 +01:00
|
|
|
/*
|
2014-03-30 12:09:21 +02:00
|
|
|
* nghttp2 - HTTP/2 C Library
|
2014-02-28 01:10:42 +01:00
|
|
|
*
|
|
|
|
* Copyright (c) 2014 Tatsuhiro Tsujikawa
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining
|
|
|
|
* a copy of this software and associated documentation files (the
|
|
|
|
* "Software"), to deal in the Software without restriction, including
|
|
|
|
* without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
* distribute, sublicense, and/or sell copies of the Software, and to
|
|
|
|
* permit persons to whom the Software is furnished to do so, subject to
|
|
|
|
* the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be
|
|
|
|
* included in all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
|
|
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
|
|
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
|
|
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
|
|
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
#ifndef H2LOAD_H
|
|
|
|
#define H2LOAD_H
|
|
|
|
|
|
|
|
#include "nghttp2_config.h"
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <netdb.h>
|
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
#include <string>
|
|
|
|
#include <unordered_map>
|
|
|
|
#include <memory>
|
2015-01-31 10:13:07 +01:00
|
|
|
#include <chrono>
|
|
|
|
#include <array>
|
2014-02-28 01:10:42 +01:00
|
|
|
|
|
|
|
#include <nghttp2/nghttp2.h>
|
|
|
|
|
2014-12-27 18:59:06 +01:00
|
|
|
#include <ev.h>
|
2014-02-28 01:10:42 +01:00
|
|
|
|
|
|
|
#include <openssl/ssl.h>
|
|
|
|
|
2014-10-22 15:53:54 +02:00
|
|
|
#include "http2.h"
|
2015-02-06 15:40:34 +01:00
|
|
|
#include "buffer.h"
|
2014-12-27 18:59:06 +01:00
|
|
|
|
|
|
|
using namespace nghttp2;
|
2014-10-22 15:53:54 +02:00
|
|
|
|
2014-02-28 01:10:42 +01:00
|
|
|
namespace h2load {
|
|
|
|
|
|
|
|
class Session;
|
|
|
|
|
|
|
|
struct Config {
|
2014-03-14 15:15:01 +01:00
|
|
|
std::vector<std::vector<nghttp2_nv>> nva;
|
2014-11-27 15:39:04 +01:00
|
|
|
std::vector<std::vector<const char *>> nv;
|
2014-10-22 15:53:54 +02:00
|
|
|
nghttp2::Headers custom_headers;
|
2014-02-28 01:10:42 +01:00
|
|
|
std::string scheme;
|
|
|
|
std::string host;
|
2014-10-22 00:29:36 +02:00
|
|
|
std::string ifile;
|
2015-03-26 11:53:42 +01:00
|
|
|
// length of upload data
|
|
|
|
int64_t data_length;
|
2014-02-28 01:10:42 +01:00
|
|
|
addrinfo *addrs;
|
|
|
|
size_t nreqs;
|
|
|
|
size_t nclients;
|
2014-03-03 17:12:11 +01:00
|
|
|
size_t nthreads;
|
2014-02-28 01:10:42 +01:00
|
|
|
// The maximum number of concurrent streams per session.
|
2014-03-14 15:15:01 +01:00
|
|
|
ssize_t max_concurrent_streams;
|
2014-02-28 01:10:42 +01:00
|
|
|
size_t window_bits;
|
|
|
|
size_t connection_window_bits;
|
2014-11-27 15:39:04 +01:00
|
|
|
enum { PROTO_HTTP2, PROTO_SPDY2, PROTO_SPDY3, PROTO_SPDY3_1 } no_tls_proto;
|
2015-03-26 11:53:42 +01:00
|
|
|
// file descriptor for upload data
|
|
|
|
int data_fd;
|
2014-02-28 01:10:42 +01:00
|
|
|
uint16_t port;
|
2014-10-22 15:42:40 +02:00
|
|
|
uint16_t default_port;
|
2014-02-28 01:10:42 +01:00
|
|
|
bool verbose;
|
|
|
|
|
|
|
|
Config();
|
|
|
|
~Config();
|
|
|
|
};
|
|
|
|
|
2015-01-31 10:13:07 +01:00
|
|
|
struct RequestStat {
|
|
|
|
RequestStat();
|
|
|
|
// time point when request was sent
|
|
|
|
std::chrono::steady_clock::time_point request_time;
|
|
|
|
// time point when stream was closed
|
|
|
|
std::chrono::steady_clock::time_point stream_close_time;
|
2015-03-26 11:53:42 +01:00
|
|
|
// upload data length sent so far
|
|
|
|
int64_t data_offset;
|
2015-01-31 10:13:07 +01:00
|
|
|
// true if stream was successfully closed. This means stream was
|
|
|
|
// not reset, but it does not mean HTTP level error (e.g., 404).
|
|
|
|
bool completed;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct TimeStats {
|
|
|
|
// time for request: max, min, mean and sd (standard deviation)
|
|
|
|
std::chrono::microseconds time_max, time_min, time_mean, time_sd;
|
|
|
|
// percentage of number of requests inside mean -/+ sd
|
|
|
|
double within_sd;
|
|
|
|
};
|
|
|
|
|
2014-02-28 01:10:42 +01:00
|
|
|
struct Stats {
|
2015-01-31 10:13:07 +01:00
|
|
|
Stats(size_t req_todo);
|
2014-02-28 01:10:42 +01:00
|
|
|
// The total number of requests
|
|
|
|
size_t req_todo;
|
|
|
|
// The number of requests issued so far
|
|
|
|
size_t req_started;
|
|
|
|
// The number of requests finished
|
|
|
|
size_t req_done;
|
2015-01-31 10:13:07 +01:00
|
|
|
// The number of requests completed successfull, but not necessarily
|
|
|
|
// means successful HTTP status code.
|
2014-02-28 01:10:42 +01:00
|
|
|
size_t req_success;
|
2015-01-31 10:13:07 +01:00
|
|
|
// The number of requests marked as success. HTTP status code is
|
|
|
|
// also considered as success. This is subset of req_done.
|
|
|
|
size_t req_status_success;
|
2014-02-28 01:10:42 +01:00
|
|
|
// The number of requests failed. This is subset of req_done.
|
|
|
|
size_t req_failed;
|
|
|
|
// The number of requests failed due to network errors. This is
|
|
|
|
// subset of req_failed.
|
|
|
|
size_t req_error;
|
|
|
|
// The number of bytes received on the "wire". If SSL/TLS is used,
|
|
|
|
// this is the number of decrypted bytes the application received.
|
|
|
|
int64_t bytes_total;
|
|
|
|
// The number of bytes received in HEADERS frame payload.
|
|
|
|
int64_t bytes_head;
|
|
|
|
// The number of bytes received in DATA frame.
|
|
|
|
int64_t bytes_body;
|
|
|
|
// The number of each HTTP status category, status[i] is status code
|
|
|
|
// in the range [i*100, (i+1)*100).
|
2015-01-31 10:13:07 +01:00
|
|
|
std::array<size_t, 6> status;
|
|
|
|
// The statistics per request
|
|
|
|
std::vector<RequestStat> req_stats;
|
2014-02-28 01:10:42 +01:00
|
|
|
};
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
enum ClientState { CLIENT_IDLE, CLIENT_CONNECTED };
|
2014-02-28 01:10:42 +01:00
|
|
|
|
|
|
|
struct Client;
|
|
|
|
|
|
|
|
struct Worker {
|
|
|
|
std::vector<std::unique_ptr<Client>> clients;
|
|
|
|
Stats stats;
|
2014-12-27 18:59:06 +01:00
|
|
|
struct ev_loop *loop;
|
2014-02-28 01:10:42 +01:00
|
|
|
SSL_CTX *ssl_ctx;
|
|
|
|
Config *config;
|
|
|
|
size_t progress_interval;
|
2014-03-03 17:12:11 +01:00
|
|
|
uint32_t id;
|
2014-11-08 13:24:24 +01:00
|
|
|
bool tls_info_report_done;
|
2014-02-28 01:10:42 +01:00
|
|
|
|
2014-03-03 17:12:11 +01:00
|
|
|
Worker(uint32_t id, SSL_CTX *ssl_ctx, size_t nreq_todo, size_t nclients,
|
|
|
|
Config *config);
|
2014-02-28 01:10:42 +01:00
|
|
|
~Worker();
|
2015-01-31 10:13:07 +01:00
|
|
|
Worker(Worker &&o) = default;
|
2014-02-28 01:10:42 +01:00
|
|
|
void run();
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Stream {
|
|
|
|
int status_success;
|
|
|
|
Stream();
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Client {
|
|
|
|
std::unordered_map<int32_t, Stream> streams;
|
|
|
|
std::unique_ptr<Session> session;
|
2014-12-27 18:59:06 +01:00
|
|
|
ev_io wev;
|
|
|
|
ev_io rev;
|
|
|
|
std::function<int(Client &)> readfn, writefn;
|
2014-02-28 01:10:42 +01:00
|
|
|
Worker *worker;
|
|
|
|
SSL *ssl;
|
|
|
|
addrinfo *next_addr;
|
2014-03-14 15:15:01 +01:00
|
|
|
size_t reqidx;
|
2014-02-28 01:10:42 +01:00
|
|
|
ClientState state;
|
2014-06-22 04:33:16 +02:00
|
|
|
// The number of requests this client has to issue.
|
|
|
|
size_t req_todo;
|
|
|
|
// The number of requests this client has issued so far.
|
|
|
|
size_t req_started;
|
2014-11-06 16:28:10 +01:00
|
|
|
// The number of requests this client has done so far.
|
2014-06-22 04:33:16 +02:00
|
|
|
size_t req_done;
|
2014-12-27 18:59:06 +01:00
|
|
|
int fd;
|
2015-02-06 15:40:34 +01:00
|
|
|
Buffer<65536> wb;
|
2014-02-28 01:10:42 +01:00
|
|
|
|
2015-01-04 17:56:02 +01:00
|
|
|
enum { ERR_CONNECT_FAIL = -100 };
|
|
|
|
|
2014-06-22 04:33:16 +02:00
|
|
|
Client(Worker *worker, size_t req_todo);
|
2014-02-28 01:10:42 +01:00
|
|
|
~Client();
|
|
|
|
int connect();
|
|
|
|
void disconnect();
|
2014-06-26 16:20:12 +02:00
|
|
|
void fail();
|
2014-02-28 01:10:42 +01:00
|
|
|
void submit_request();
|
|
|
|
void process_abandoned_streams();
|
|
|
|
void report_progress();
|
2014-11-08 13:24:24 +01:00
|
|
|
void report_tls_info();
|
2014-02-28 01:10:42 +01:00
|
|
|
void terminate_session();
|
2014-12-27 18:59:06 +01:00
|
|
|
|
|
|
|
int do_read();
|
|
|
|
int do_write();
|
|
|
|
|
2015-03-28 12:17:30 +01:00
|
|
|
// low-level I/O callback functions called by do_read/do_write
|
2014-12-27 18:59:06 +01:00
|
|
|
int connected();
|
|
|
|
int read_clear();
|
|
|
|
int write_clear();
|
|
|
|
int tls_handshake();
|
|
|
|
int read_tls();
|
|
|
|
int write_tls();
|
|
|
|
|
|
|
|
int on_read(const uint8_t *data, size_t len);
|
2014-02-28 01:10:42 +01:00
|
|
|
int on_write();
|
2014-12-27 18:59:06 +01:00
|
|
|
|
2015-03-28 12:13:37 +01:00
|
|
|
int connection_made();
|
|
|
|
|
2014-02-28 01:10:42 +01:00
|
|
|
void on_request(int32_t stream_id);
|
2014-11-27 15:39:04 +01:00
|
|
|
void on_header(int32_t stream_id, const uint8_t *name, size_t namelen,
|
2014-02-28 01:10:42 +01:00
|
|
|
const uint8_t *value, size_t valuelen);
|
2015-01-31 10:13:07 +01:00
|
|
|
void on_stream_close(int32_t stream_id, bool success, RequestStat *req_stat);
|
|
|
|
|
|
|
|
void record_request_time(RequestStat *req_stat);
|
2014-12-27 18:59:06 +01:00
|
|
|
|
|
|
|
void signal_write();
|
2014-02-28 01:10:42 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace h2load
|
|
|
|
|
|
|
|
#endif // H2LOAD_H
|