| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
#include "config.h" |
| 22 |
|
| 23 |
#include <libdex.h> |
| 24 |
|
| 25 |
typedef struct _Worker |
| 26 |
{ |
| 27 |
gint64 conn_attempts; |
| 28 |
gint64 conn_failures; |
| 29 |
gint64 conn_success; |
| 30 |
gint64 conn_closed; |
| 31 |
gint64 bytes_sent; |
| 32 |
gint64 bytes_received; |
| 33 |
} Worker; |
| 34 |
|
| 35 |
static DexScheduler *thread_pool; |
| 36 |
static GSocketConnectable *socket_address; |
| 37 |
static gchar *buf; |
| 38 |
static gsize buflen; |
| 39 |
static GPtrArray *fibers; |
| 40 |
static Worker *workers; |
| 41 |
static guint n_workers; |
| 42 |
static gboolean in_shutdown; |
| 43 |
static GTimer *timer; |
| 44 |
|
| 45 |
static DexFuture * |
| 46 |
worker_fiber (gpointer user_data) |
| 47 |
{ |
| 48 |
g_autoptr(GSocketClient) client = NULL; |
| 49 |
Worker *worker = user_data; |
| 50 |
g_autofree char *inbuf = g_malloc (buflen); |
| 51 |
|
| 52 |
client = g_socket_client_new (); |
| 53 |
|
| 54 |
while (!g_atomic_int_get (&in_shutdown)) |
| 55 |
{ |
| 56 |
g_autoptr(GSocketConnection) connection = NULL; |
| 57 |
GOutputStream *output; |
| 58 |
GInputStream *input; |
| 59 |
gssize len; |
| 60 |
|
| 61 |
worker->conn_attempts++; |
| 62 |
|
| 63 |
if (!(connection = dex_await_object (dex_socket_client_connect (client, socket_address), NULL))) |
| 64 |
{ |
| 65 |
worker->conn_failures++; |
| 66 |
break; |
| 67 |
} |
| 68 |
|
| 69 |
worker->conn_success++; |
| 70 |
|
| 71 |
output = g_io_stream_get_output_stream (G_IO_STREAM (connection)); |
| 72 |
input = g_io_stream_get_input_stream (G_IO_STREAM (connection)); |
| 73 |
|
| 74 |
if ((len = dex_await_int64 (dex_output_stream_write (output, buf, buflen, G_PRIORITY_DEFAULT), NULL)) <= 0) |
| 75 |
break; |
| 76 |
worker->bytes_sent += len; |
| 77 |
|
| 78 |
if ((len = dex_await_int64 (dex_input_stream_read (input, inbuf, buflen, G_PRIORITY_DEFAULT), NULL)) <= 0) |
| 79 |
break; |
| 80 |
worker->bytes_received += len; |
| 81 |
|
| 82 |
dex_await (dex_io_stream_close (G_IO_STREAM (connection), G_PRIORITY_DEFAULT), NULL); |
| 83 |
} |
| 84 |
|
| 85 |
return NULL; |
| 86 |
} |
| 87 |
|
| 88 |
static DexFuture * |
| 89 |
shutdown_cb (DexFuture *completed, |
| 90 |
gpointer user_data) |
| 91 |
{ |
| 92 |
|
| 93 |
g_atomic_int_set (&in_shutdown, TRUE); |
| 94 |
|
| 95 |
|
| 96 |
return NULL; |
| 97 |
} |
| 98 |
|
| 99 |
static DexFuture * |
| 100 |
quit_cb (DexFuture *completed, |
| 101 |
gpointer user_data) |
| 102 |
{ |
| 103 |
GMainLoop *main_loop = user_data; |
| 104 |
g_main_loop_quit (main_loop); |
| 105 |
return NULL; |
| 106 |
} |
| 107 |
|
| 108 |
static gboolean |
| 109 |
print_live_status (gpointer data) |
| 110 |
{ |
| 111 |
Worker total = {0}; |
| 112 |
double duration = g_timer_elapsed (timer, NULL); |
| 113 |
g_autofree char *sent_per_sec = NULL; |
| 114 |
g_autofree char *recv_per_sec = NULL; |
| 115 |
|
| 116 |
for (guint i = 0; i < n_workers; i++) |
| 117 |
{ |
| 118 |
total.conn_attempts += workers[i].conn_attempts; |
| 119 |
total.conn_failures += workers[i].conn_failures; |
| 120 |
total.conn_success += workers[i].conn_success; |
| 121 |
total.conn_closed += workers[i].conn_closed; |
| 122 |
total.bytes_sent += workers[i].bytes_sent; |
| 123 |
total.bytes_received += workers[i].bytes_received; |
| 124 |
} |
| 125 |
|
| 126 |
sent_per_sec = g_format_size (total.bytes_sent/duration); |
| 127 |
recv_per_sec = g_format_size (total.bytes_received/duration); |
| 128 |
|
| 129 |
g_printerr ("\n"); |
| 130 |
g_printerr (" req: succ=%"G_GINT64_FORMAT" (per-sec %0.2lf) fail=%"G_GINT64_FORMAT" (per-sec=%0.2lf)\n", |
| 131 |
total.conn_success, total.conn_success/duration, |
| 132 |
total.conn_failures, total.conn_failures/duration); |
| 133 |
g_printerr ("bytes: sent=%"G_GINT64_FORMAT" (per-sec %s) recv=%"G_GINT64_FORMAT" (per-sec %s)\n", |
| 134 |
total.bytes_sent, sent_per_sec, |
| 135 |
total.bytes_received, recv_per_sec); |
| 136 |
|
| 137 |
return G_SOURCE_CONTINUE; |
| 138 |
} |
| 139 |
|
| 140 |
static void |
| 141 |
print_results (void) |
| 142 |
{ |
| 143 |
print_live_status (NULL); |
| 144 |
} |
| 145 |
|
| 146 |
int |
| 147 |
main (int argc, |
| 148 |
char *argv[]) |
| 149 |
{ |
| 150 |
g_autoptr(GOptionContext) context = NULL; |
| 151 |
g_autoptr(GMainLoop) main_loop = NULL; |
| 152 |
g_autoptr(DexFuture) future = NULL; |
| 153 |
g_autoptr(GError) error = NULL; |
| 154 |
g_autofree char *address = NULL; |
| 155 |
g_autofree char *message = NULL; |
| 156 |
int length = 0; |
| 157 |
int duration = 0; |
| 158 |
int number = 0; |
| 159 |
|
| 160 |
GOptionEntry entries[] = { |
| 161 |
{ "address", 'a', 0, G_OPTION_ARG_STRING, &address, "Target echo server adderss.", "0.0.0.0:8080" }, |
| 162 |
{ "length", 'l', 0, G_OPTION_ARG_INT, &length, "Target message length.", "BYTES" }, |
| 163 |
{ "duration", 'd', 0, G_OPTION_ARG_INT, &duration, "Test duration in seconds.", "SECONDS" }, |
| 164 |
{ "number", 'c', 0, G_OPTION_ARG_INT, &number, "Number of concurrent connections.", "CONNECTIONS" }, |
| 165 |
{ "message", 'm', 0, G_OPTION_ARG_STRING, &message, "A custom message to send.", "MSG" }, |
| 166 |
{ NULL } |
| 167 |
}; |
| 168 |
|
| 169 |
context = g_option_context_new ("- Simple echo benchmark client"); |
| 170 |
g_option_context_add_main_entries (context, entries, NULL); |
| 171 |
|
| 172 |
if (!g_option_context_parse (context, &argc, &argv, &error) || |
| 173 |
address == NULL || |
| 174 |
!(socket_address = g_network_address_parse (address, 8080, &error))) |
| 175 |
{ |
| 176 |
g_autofree char *help = g_option_context_get_help (context, TRUE, NULL); |
| 177 |
|
| 178 |
if (error != NULL) |
| 179 |
g_printerr ("%s\n\n", error->message); |
| 180 |
g_printerr ("%s\n", help); |
| 181 |
|
| 182 |
return EXIT_FAILURE; |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
if (duration <= 0) |
| 187 |
duration = 60; |
| 188 |
if (number <= 0) |
| 189 |
number = 1000; |
| 190 |
if (length <= 0) |
| 191 |
length = 512; |
| 192 |
|
| 193 |
|
| 194 |
g_free (address); |
| 195 |
address = g_socket_connectable_to_string (socket_address); |
| 196 |
|
| 197 |
|
| 198 |
if (message != NULL) |
| 199 |
{ |
| 200 |
buf = g_steal_pointer (&message); |
| 201 |
buflen = strlen (buf); |
| 202 |
length = buflen; |
| 203 |
|
| 204 |
g_printerr ("Using custom message:\n\n" |
| 205 |
"================================\n" |
| 206 |
"%s\n" |
| 207 |
"================================\n", |
| 208 |
buf); |
| 209 |
} |
| 210 |
else |
| 211 |
{ |
| 212 |
buflen = length; |
| 213 |
buf = g_strnfill (buflen, 'X'); |
| 214 |
} |
| 215 |
|
| 216 |
g_printerr ("Benchmarking: %s\n", address); |
| 217 |
g_printerr ("%u clients, running %u bytes, %u sec.\n", number, length, duration); |
| 218 |
|
| 219 |
|
| 220 |
n_workers = number; |
| 221 |
workers = g_new0 (Worker, n_workers); |
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
main_loop = g_main_loop_new (NULL, FALSE); |
| 227 |
thread_pool = dex_thread_pool_scheduler_new (); |
| 228 |
timer = g_timer_new (); |
| 229 |
|
| 230 |
|
| 231 |
fibers = g_ptr_array_new_with_free_func (dex_unref); |
| 232 |
for (int i = 0; i < number; i++) |
| 233 |
g_ptr_array_add (fibers, |
| 234 |
dex_scheduler_spawn (thread_pool, 0, worker_fiber, &workers[i], NULL)); |
| 235 |
|
| 236 |
|
| 237 |
future = dex_timeout_new_seconds (duration); |
| 238 |
|
| 239 |
|
| 240 |
future = dex_future_finally (future, shutdown_cb, NULL, NULL); |
| 241 |
|
| 242 |
|
| 243 |
future = dex_future_finally (future, quit_cb, main_loop, NULL); |
| 244 |
|
| 245 |
|
| 246 |
g_timeout_add_seconds (1, print_live_status, NULL); |
| 247 |
|
| 248 |
g_main_loop_run (main_loop); |
| 249 |
|
| 250 |
print_results (); |
| 251 |
|
| 252 |
|
| 253 |
g_object_unref (socket_address); |
| 254 |
g_ptr_array_unref (fibers); |
| 255 |
g_free (workers); |
| 256 |
g_free (buf); |
| 257 |
|
| 258 |
return EXIT_SUCCESS; |
| 259 |
} |