diff --git a/examples/mic.c b/examples/mic.c index 90a7c3e..6fd6484 100644 --- a/examples/mic.c +++ b/examples/mic.c @@ -47,12 +47,18 @@ main( int argc, char** argv ) iperf_set_test_duration( test, 5 ); iperf_set_test_reporter_interval( test, 1 ); iperf_set_test_stats_interval( test, 1 ); + /* iperf_set_test_json_output( test, 1 ); */ if ( iperf_run_client( test ) < 0 ) { fprintf( stderr, "%s: error - %s\n", argv0, iperf_strerror( i_errno ) ); exit( EXIT_FAILURE ); } + if (iperf_get_test_json_output_string(test)) { + fprintf(iperf_get_test_outfile(test), "%zd bytes of JSON emitted\n", + strlen(iperf_get_test_json_output_string(test))); + } + iperf_free_test( test ); exit( EXIT_SUCCESS ); } diff --git a/src/iperf.h b/src/iperf.h index 22037df..dd59483 100644 --- a/src/iperf.h +++ b/src/iperf.h @@ -193,6 +193,7 @@ struct iperf_test int multisend; int may_use_sigalrm; + char *json_output_string; /* rendered JSON output if json_output is set */ /* Select related parameters */ int max_fd; fd_set read_set; /* set of read sockets */ diff --git a/src/iperf_api.c b/src/iperf_api.c index fba928c..e69c488 100644 --- a/src/iperf_api.c +++ b/src/iperf_api.c @@ -145,6 +145,12 @@ iperf_get_test_blksize(struct iperf_test *ipt) return ipt->settings->blksize; } +FILE * +iperf_get_test_outfile (struct iperf_test *ipt) +{ + return ipt->outfile; +} + int iperf_get_test_socket_bufsize(struct iperf_test *ipt) { @@ -193,6 +199,12 @@ iperf_get_test_json_output(struct iperf_test *ipt) return ipt->json_output; } +char * +iperf_get_test_json_output_string(struct iperf_test *ipt) +{ + return ipt->json_output_string; +} + int iperf_get_test_zerocopy(struct iperf_test *ipt) { @@ -1681,6 +1693,11 @@ iperf_free_test(struct iperf_test *test) free(prot); } + if (test->json_output_string) { + free(test->json_output_string); + test->json_output_string = NULL; + } + /* XXX: Why are we setting these values to NULL? */ // test->streams = NULL; test->stats_callback = NULL; @@ -2535,14 +2552,11 @@ iperf_json_start(struct iperf_test *test) int iperf_json_finish(struct iperf_test *test) { - char *str; - - str = cJSON_Print(test->json_top); - if (str == NULL) + test->json_output_string = cJSON_Print(test->json_top); + if (test->json_output_string == NULL) return -1; - fprintf(test->outfile, "%s\n", str); + fprintf(test->outfile, "%s\n", test->json_output_string); iflush(test); - free(str); cJSON_Delete(test->json_top); test->json_top = test->json_start = test->json_intervals = test->json_end = NULL; return 0; diff --git a/src/iperf_api.h b/src/iperf_api.h index 9840fa0..d2f5dbb 100644 --- a/src/iperf_api.h +++ b/src/iperf_api.h @@ -57,6 +57,7 @@ int iperf_get_test_duration( struct iperf_test* ipt ); char iperf_get_test_role( struct iperf_test* ipt ); int iperf_get_test_reverse( struct iperf_test* ipt ); int iperf_get_test_blksize( struct iperf_test* ipt ); +FILE* iperf_get_test_outfile( struct iperf_test* ipt ); uint64_t iperf_get_test_rate( struct iperf_test* ipt ); int iperf_get_test_burst( struct iperf_test* ipt ); int iperf_get_test_socket_bufsize( struct iperf_test* ipt ); @@ -67,6 +68,7 @@ int iperf_get_test_server_port( struct iperf_test* ipt ); char* iperf_get_test_server_hostname( struct iperf_test* ipt ); int iperf_get_test_protocol_id( struct iperf_test* ipt ); int iperf_get_test_json_output( struct iperf_test* ipt ); +char* iperf_get_test_json_output_string ( struct iperf_test* ipt ); int iperf_get_test_zerocopy( struct iperf_test* ipt ); int iperf_get_test_may_use_sigalrm( struct iperf_test* ipt ); diff --git a/src/libiperf.3 b/src/libiperf.3 index 90dedf1..d0ad7f0 100644 --- a/src/libiperf.3 +++ b/src/libiperf.3 @@ -1,4 +1,4 @@ -.TH LIBIPERF 3 "February 2014" ESnet "User Manuals" +.TH LIBIPERF 3 "March 2014" ESnet "User Manuals" .SH NAME libiperf \- API for iperf3 network throughput tester @@ -39,6 +39,11 @@ Running a test: int iperf_run_server(struct iperf_test *); void iperf_test_reset(struct iperf_test *); .fi +Output: +.nf + FILE *iperf_get_test_outfile(struct iperf_test *); + char* iperf_get_test_json_output_string(struct iperf_test *); +.fi Error reporting: .nf void iperf_err(struct iperf_test *t, const char *format, ...); diff --git a/src/main.c b/src/main.c index 6b49fb2..03ef7b5 100644 --- a/src/main.c +++ b/src/main.c @@ -117,7 +117,7 @@ run(struct iperf_test *test) iperf_errexit(test, "error - %s", iperf_strerror(i_errno)); } for (;;) { - if (iperf_run_server(test) < 0) { + if (iperf_run_server(test) < 0) { iperf_err(test, "error - %s", iperf_strerror(i_errno)); fprintf(stderr, "\n"); ++consecutive_errors; @@ -131,8 +131,8 @@ run(struct iperf_test *test) } iperf_delete_pidfile(test); break; - case 'c': - if (iperf_run_client(test) < 0) + case 'c': + if (iperf_run_client(test) < 0) iperf_errexit(test, "error - %s", iperf_strerror(i_errno)); break; default: