mirror of https://github.com/grpc/grpc.git
Change on 2015/01/08 by hongyu <hongyu@google.com> ------------- Created by MOE: http://code.google.com/p/moe-java MOE_MIGRATED_REVID=83556470pull/3/merge
parent
6edb547c99
commit
24200d3cbc
17 changed files with 1341 additions and 35 deletions
@ -0,0 +1,59 @@ |
||||
/*
|
||||
* |
||||
* Copyright 2014, Google Inc. |
||||
* All rights reserved. |
||||
* |
||||
* Redistribution and use in source and binary forms, with or without |
||||
* modification, are permitted provided that the following conditions are |
||||
* met: |
||||
* |
||||
* * Redistributions of source code must retain the above copyright |
||||
* notice, this list of conditions and the following disclaimer. |
||||
* * Redistributions in binary form must reproduce the above |
||||
* copyright notice, this list of conditions and the following disclaimer |
||||
* in the documentation and/or other materials provided with the |
||||
* distribution. |
||||
* * Neither the name of Google Inc. nor the names of its |
||||
* contributors may be used to endorse or promote products derived from |
||||
* this software without specific prior written permission. |
||||
* |
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
||||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
||||
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
* |
||||
*/ |
||||
|
||||
#ifndef __GRPC_INTERNAL_STATISTICS_CENSUS_TRACING_H_ |
||||
#define __GRPC_INTERNAL_STATISTICS_CENSUS_TRACING_H_ |
||||
|
||||
/* Opaque structure for trace object */ |
||||
typedef struct trace_obj trace_obj; |
||||
|
||||
/* Initializes trace store. This function is thread safe. */ |
||||
void census_tracing_init(); |
||||
|
||||
/* Shutsdown trace store. This function is thread safe. */ |
||||
void census_tracing_shutdown(); |
||||
|
||||
/* Gets trace obj corresponding to the input op_id. Returns NULL if trace store
|
||||
is not initialized or trace obj is not found. Requires trace store being |
||||
locked before calling this function. */ |
||||
trace_obj* census_get_trace_obj_locked(census_op_id op_id); |
||||
|
||||
/* The following two functions acquire and release the trace store global lock.
|
||||
They are for census internal use only. */ |
||||
void census_internal_lock_trace_store(); |
||||
void census_internal_unlock_trace_store(); |
||||
|
||||
/* Gets method tag name associated with the input trace object. */ |
||||
const char* census_get_trace_method_name(const trace_obj* trace); |
||||
|
||||
#endif /* __GRPC_INTERNAL_STATISTICS_CENSUS_TRACING_H_ */ |
@ -0,0 +1,176 @@ |
||||
/*
|
||||
* |
||||
* Copyright 2014, Google Inc. |
||||
* All rights reserved. |
||||
* |
||||
* Redistribution and use in source and binary forms, with or without |
||||
* modification, are permitted provided that the following conditions are |
||||
* met: |
||||
* |
||||
* * Redistributions of source code must retain the above copyright |
||||
* notice, this list of conditions and the following disclaimer. |
||||
* * Redistributions in binary form must reproduce the above |
||||
* copyright notice, this list of conditions and the following disclaimer |
||||
* in the documentation and/or other materials provided with the |
||||
* distribution. |
||||
* * Neither the name of Google Inc. nor the names of its |
||||
* contributors may be used to endorse or promote products derived from |
||||
* this software without specific prior written permission. |
||||
* |
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
||||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
||||
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
* |
||||
*/ |
||||
|
||||
#include "test/core/end2end/end2end_tests.h" |
||||
|
||||
#include <stdio.h> |
||||
#include <string.h> |
||||
#include <unistd.h> |
||||
|
||||
#include <grpc/byte_buffer.h> |
||||
#include <grpc/support/alloc.h> |
||||
#include <grpc/support/log.h> |
||||
#include <grpc/support/time.h> |
||||
#include <grpc/support/useful.h> |
||||
#include "test/core/end2end/cq_verifier.h" |
||||
|
||||
static gpr_timespec n_seconds_time(int n) { |
||||
return gpr_time_add(gpr_now(), gpr_time_from_micros(GPR_US_PER_SEC * n)); |
||||
} |
||||
|
||||
static grpc_end2end_test_fixture begin_test(grpc_end2end_test_config config, |
||||
const char *test_name, |
||||
grpc_channel_args *client_args, |
||||
grpc_channel_args *server_args) { |
||||
grpc_end2end_test_fixture f; |
||||
gpr_log(GPR_INFO, "%s/%s", test_name, config.name); |
||||
f = config.create_fixture(client_args, server_args); |
||||
config.init_client(&f, client_args); |
||||
config.init_server(&f, server_args); |
||||
return f; |
||||
} |
||||
|
||||
static void shutdown_server(grpc_end2end_test_fixture *f) { |
||||
if (!f->server) return; |
||||
grpc_server_shutdown(f->server); |
||||
grpc_server_destroy(f->server); |
||||
f->server = NULL; |
||||
} |
||||
|
||||
static void shutdown_client(grpc_end2end_test_fixture *f) { |
||||
if (!f->client) return; |
||||
grpc_channel_destroy(f->client); |
||||
f->client = NULL; |
||||
} |
||||
|
||||
static void drain_cq(grpc_completion_queue *cq) { |
||||
grpc_event *ev; |
||||
grpc_completion_type type; |
||||
do { |
||||
ev = grpc_completion_queue_next(cq, n_seconds_time(5)); |
||||
GPR_ASSERT(ev); |
||||
type = ev->type; |
||||
grpc_event_finish(ev); |
||||
} while (type != GRPC_QUEUE_SHUTDOWN); |
||||
} |
||||
|
||||
static void end_test(grpc_end2end_test_fixture *f) { |
||||
shutdown_server(f); |
||||
shutdown_client(f); |
||||
|
||||
grpc_completion_queue_shutdown(f->server_cq); |
||||
drain_cq(f->server_cq); |
||||
grpc_completion_queue_destroy(f->server_cq); |
||||
grpc_completion_queue_shutdown(f->client_cq); |
||||
drain_cq(f->client_cq); |
||||
grpc_completion_queue_destroy(f->client_cq); |
||||
} |
||||
|
||||
static void *tag(gpr_intptr t) { return (void *)t; } |
||||
|
||||
static void test_body(grpc_end2end_test_fixture f) { |
||||
grpc_call *c; |
||||
grpc_call *s; |
||||
gpr_timespec deadline = n_seconds_time(10); |
||||
cq_verifier *v_client = cq_verifier_create(f.client_cq); |
||||
cq_verifier *v_server = cq_verifier_create(f.server_cq); |
||||
|
||||
c = grpc_channel_create_call(f.client, "/foo", "test.google.com", deadline); |
||||
GPR_ASSERT(c); |
||||
tag(1); |
||||
GPR_ASSERT(GRPC_CALL_OK == |
||||
grpc_call_start_invoke(c, f.client_cq, tag(1), tag(2), tag(3), 0)); |
||||
cq_expect_invoke_accepted(v_client, tag(1), GRPC_OP_OK); |
||||
cq_verify(v_client); |
||||
|
||||
GPR_ASSERT(GRPC_CALL_OK == grpc_call_writes_done(c, tag(4))); |
||||
cq_expect_finish_accepted(v_client, tag(4), GRPC_OP_OK); |
||||
cq_verify(v_client); |
||||
|
||||
GPR_ASSERT(GRPC_CALL_OK == grpc_server_request_call(f.server, tag(100))); |
||||
cq_expect_server_rpc_new(v_server, &s, tag(100), "/foo", "test.google.com", |
||||
deadline, NULL); |
||||
cq_verify(v_server); |
||||
|
||||
GPR_ASSERT(GRPC_CALL_OK == grpc_call_accept(s, f.server_cq, tag(102), 0)); |
||||
cq_expect_client_metadata_read(v_client, tag(2), NULL); |
||||
cq_verify(v_client); |
||||
|
||||
GPR_ASSERT(GRPC_CALL_OK == grpc_call_start_write_status( |
||||
s, GRPC_STATUS_UNIMPLEMENTED, "xyz", tag(5))); |
||||
cq_expect_finished_with_status(v_client, tag(3), GRPC_STATUS_UNIMPLEMENTED, |
||||
"xyz", NULL); |
||||
cq_verify(v_client); |
||||
|
||||
cq_expect_finish_accepted(v_server, tag(5), GRPC_OP_OK); |
||||
cq_verify(v_server); |
||||
cq_expect_finished(v_server, tag(102), NULL); |
||||
cq_verify(v_server); |
||||
grpc_call_destroy(c); |
||||
grpc_call_destroy(s); |
||||
|
||||
cq_verifier_destroy(v_client); |
||||
cq_verifier_destroy(v_server); |
||||
} |
||||
|
||||
static void test_invoke_request_with_census( |
||||
grpc_end2end_test_config config, const char *name, |
||||
void (*body)(grpc_end2end_test_fixture f)) { |
||||
char fullname[64]; |
||||
grpc_end2end_test_fixture f; |
||||
grpc_arg client_arg, server_arg; |
||||
grpc_channel_args client_args, server_args; |
||||
|
||||
client_arg.type = GRPC_ARG_INTEGER; |
||||
client_arg.key = GRPC_ARG_ENABLE_CENSUS; |
||||
client_arg.value.integer = 1; |
||||
|
||||
client_args.num_args = 1; |
||||
client_args.args = &client_arg; |
||||
|
||||
server_arg.type = GRPC_ARG_INTEGER; |
||||
server_arg.key = GRPC_ARG_ENABLE_CENSUS; |
||||
server_arg.value.integer = 1; |
||||
server_args.num_args = 1; |
||||
server_args.args = &server_arg; |
||||
|
||||
sprintf(fullname, "%s/%s", __FUNCTION__, name); |
||||
f = begin_test(config, fullname, &client_args, &server_args); |
||||
body(f); |
||||
end_test(&f); |
||||
config.tear_down_data(&f); |
||||
} |
||||
|
||||
void grpc_end2end_tests(grpc_end2end_test_config config) { |
||||
test_invoke_request_with_census(config, "census_simple_request", test_body); |
||||
} |
@ -0,0 +1,197 @@ |
||||
/*
|
||||
* |
||||
* Copyright 2014, Google Inc. |
||||
* All rights reserved. |
||||
* |
||||
* Redistribution and use in source and binary forms, with or without |
||||
* modification, are permitted provided that the following conditions are |
||||
* met: |
||||
* |
||||
* * Redistributions of source code must retain the above copyright |
||||
* notice, this list of conditions and the following disclaimer. |
||||
* * Redistributions in binary form must reproduce the above |
||||
* copyright notice, this list of conditions and the following disclaimer |
||||
* in the documentation and/or other materials provided with the |
||||
* distribution. |
||||
* * Neither the name of Google Inc. nor the names of its |
||||
* contributors may be used to endorse or promote products derived from |
||||
* this software without specific prior written permission. |
||||
* |
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
||||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
||||
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
* |
||||
*/ |
||||
|
||||
#include <string.h> |
||||
|
||||
#include "src/core/statistics/census_interface.h" |
||||
#include "src/core/statistics/census_rpc_stats.h" |
||||
#include "src/core/statistics/census_tracing.h" |
||||
#include <grpc/support/alloc.h> |
||||
#include <grpc/support/log.h> |
||||
#include <grpc/support/port_platform.h> |
||||
#include <grpc/support/string.h> |
||||
#include <grpc/support/thd.h> |
||||
#include <grpc/support/time.h> |
||||
#include "test/core/util/test_config.h" |
||||
|
||||
/* Ensure all possible state transitions are called without causing problem */ |
||||
static void test_init_shutdown() { |
||||
census_stats_store_init(); |
||||
census_stats_store_init(); |
||||
census_stats_store_shutdown(); |
||||
census_stats_store_shutdown(); |
||||
census_stats_store_init(); |
||||
} |
||||
|
||||
static void test_create_and_destroy() { |
||||
census_rpc_stats* stats = NULL; |
||||
census_aggregated_rpc_stats agg_stats = {0, NULL}; |
||||
|
||||
stats = census_rpc_stats_create_empty(); |
||||
GPR_ASSERT(stats != NULL); |
||||
GPR_ASSERT(stats->cnt == 0 && stats->rpc_error_cnt == 0 && |
||||
stats->app_error_cnt == 0 && stats->elapsed_time_ms == 0.0 && |
||||
stats->api_request_bytes == 0 && stats->wire_request_bytes == 0 && |
||||
stats->api_response_bytes == 0 && stats->wire_response_bytes == 0); |
||||
gpr_free(stats); |
||||
|
||||
census_aggregated_rpc_stats_set_empty(&agg_stats); |
||||
GPR_ASSERT(agg_stats.num_entries == 0); |
||||
GPR_ASSERT(agg_stats.stats == NULL); |
||||
agg_stats.num_entries = 1; |
||||
agg_stats.stats = (census_per_method_rpc_stats*)gpr_malloc( |
||||
sizeof(census_per_method_rpc_stats)); |
||||
agg_stats.stats[0].method = gpr_strdup("foo"); |
||||
census_aggregated_rpc_stats_set_empty(&agg_stats); |
||||
GPR_ASSERT(agg_stats.num_entries == 0); |
||||
GPR_ASSERT(agg_stats.stats == NULL); |
||||
} |
||||
|
||||
#define ASSERT_NEAR(a, b) \ |
||||
GPR_ASSERT((a - b) * (a - b) < 1e-24 * (a + b) * (a + b)) |
||||
|
||||
static void test_record_and_get_stats() { |
||||
census_rpc_stats stats = {1, 2, 3, 4, 5.1, 6.2, 7.3, 8.4}; |
||||
census_op_id id; |
||||
census_aggregated_rpc_stats agg_stats = {0, NULL}; |
||||
|
||||
/* Record client stats twice with the same op_id. */ |
||||
census_init(); |
||||
id = census_tracing_start_op(); |
||||
census_add_method_tag(id, "m1"); |
||||
census_record_rpc_client_stats(id, &stats); |
||||
census_record_rpc_client_stats(id, &stats); |
||||
census_tracing_end_op(id); |
||||
/* Server stats expect to be empty */ |
||||
census_get_server_stats(&agg_stats); |
||||
GPR_ASSERT(agg_stats.num_entries == 0); |
||||
GPR_ASSERT(agg_stats.stats == NULL); |
||||
/* Client stats expect to have one entry */ |
||||
census_get_client_stats(&agg_stats); |
||||
GPR_ASSERT(agg_stats.num_entries == 1); |
||||
GPR_ASSERT(agg_stats.stats != NULL); |
||||
GPR_ASSERT(strcmp(agg_stats.stats[0].method, "m1") == 0); |
||||
GPR_ASSERT(agg_stats.stats[0].minute_stats.cnt == 2 && |
||||
agg_stats.stats[0].hour_stats.cnt == 2 && |
||||
agg_stats.stats[0].total_stats.cnt == 2); |
||||
ASSERT_NEAR(agg_stats.stats[0].minute_stats.wire_response_bytes, 16.8); |
||||
ASSERT_NEAR(agg_stats.stats[0].hour_stats.wire_response_bytes, 16.8); |
||||
ASSERT_NEAR(agg_stats.stats[0].total_stats.wire_response_bytes, 16.8); |
||||
/* Get stats again, results should be the same. */ |
||||
census_get_client_stats(&agg_stats); |
||||
GPR_ASSERT(agg_stats.num_entries == 1); |
||||
census_aggregated_rpc_stats_set_empty(&agg_stats); |
||||
census_shutdown(); |
||||
|
||||
/* Record both server (once) and client (twice) stats with different op_ids.*/ |
||||
census_init(); |
||||
id = census_tracing_start_op(); |
||||
census_add_method_tag(id, "m2"); |
||||
census_record_rpc_client_stats(id, &stats); |
||||
census_tracing_end_op(id); |
||||
id = census_tracing_start_op(); |
||||
census_add_method_tag(id, "m3"); |
||||
census_record_rpc_server_stats(id, &stats); |
||||
census_tracing_end_op(id); |
||||
id = census_tracing_start_op(); |
||||
census_add_method_tag(id, "m4"); |
||||
census_record_rpc_client_stats(id, &stats); |
||||
census_tracing_end_op(id); |
||||
/* Check server stats */ |
||||
census_get_server_stats(&agg_stats); |
||||
GPR_ASSERT(agg_stats.num_entries == 1); |
||||
GPR_ASSERT(strcmp(agg_stats.stats[0].method, "m3") == 0); |
||||
GPR_ASSERT(agg_stats.stats[0].minute_stats.app_error_cnt == 3 && |
||||
agg_stats.stats[0].hour_stats.app_error_cnt == 3 && |
||||
agg_stats.stats[0].total_stats.app_error_cnt == 3); |
||||
census_aggregated_rpc_stats_set_empty(&agg_stats); |
||||
/* Check client stats */ |
||||
census_get_client_stats(&agg_stats); |
||||
GPR_ASSERT(agg_stats.num_entries == 2); |
||||
GPR_ASSERT(agg_stats.stats != NULL); |
||||
GPR_ASSERT((strcmp(agg_stats.stats[0].method, "m2") == 0 && |
||||
strcmp(agg_stats.stats[1].method, "m4") == 0) || |
||||
(strcmp(agg_stats.stats[0].method, "m4") == 0 && |
||||
strcmp(agg_stats.stats[1].method, "m2") == 0)); |
||||
GPR_ASSERT(agg_stats.stats[0].minute_stats.cnt == 1 && |
||||
agg_stats.stats[1].minute_stats.cnt == 1); |
||||
census_aggregated_rpc_stats_set_empty(&agg_stats); |
||||
census_shutdown(); |
||||
} |
||||
|
||||
static void test_record_stats_on_unknown_op_id() { |
||||
census_op_id unknown_id = {0xDEAD, 0xBEEF}; |
||||
census_rpc_stats stats = {1, 2, 3, 4, 5.1, 6.2, 7.3, 8.4}; |
||||
census_aggregated_rpc_stats agg_stats = {0, NULL}; |
||||
|
||||
census_init(); |
||||
/* Tests that recording stats against unknown id is noop. */ |
||||
census_record_rpc_client_stats(unknown_id, &stats); |
||||
census_record_rpc_server_stats(unknown_id, &stats); |
||||
census_get_server_stats(&agg_stats); |
||||
GPR_ASSERT(agg_stats.num_entries == 0); |
||||
GPR_ASSERT(agg_stats.stats == NULL); |
||||
census_get_client_stats(&agg_stats); |
||||
GPR_ASSERT(agg_stats.num_entries == 0); |
||||
GPR_ASSERT(agg_stats.stats == NULL); |
||||
census_aggregated_rpc_stats_set_empty(&agg_stats); |
||||
census_shutdown(); |
||||
} |
||||
|
||||
/* Test that record stats is noop when trace store is uninitialized. */ |
||||
static void test_record_stats_with_trace_store_uninitialized() { |
||||
census_rpc_stats stats = {1, 2, 3, 4, 5.1, 6.2, 7.3, 8.4}; |
||||
census_op_id id = {0, 0}; |
||||
census_aggregated_rpc_stats agg_stats = {0, NULL}; |
||||
|
||||
census_init(); |
||||
id = census_tracing_start_op(); |
||||
census_add_method_tag(id, "m"); |
||||
census_tracing_end_op(id); |
||||
/* shuts down trace store only. */ |
||||
census_tracing_shutdown(); |
||||
census_record_rpc_client_stats(id, &stats); |
||||
census_get_client_stats(&agg_stats); |
||||
GPR_ASSERT(agg_stats.num_entries == 0); |
||||
census_stats_store_shutdown(); |
||||
} |
||||
|
||||
int main(int argc, char** argv) { |
||||
grpc_test_init(argc, argv); |
||||
test_init_shutdown(); |
||||
test_create_and_destroy(); |
||||
test_record_and_get_stats(); |
||||
test_record_stats_on_unknown_op_id(); |
||||
test_record_stats_with_trace_store_uninitialized(); |
||||
return 0; |
||||
} |
@ -0,0 +1,158 @@ |
||||
/*
|
||||
* |
||||
* Copyright 2014, Google Inc. |
||||
* All rights reserved. |
||||
* |
||||
* Redistribution and use in source and binary forms, with or without |
||||
* modification, are permitted provided that the following conditions are |
||||
* met: |
||||
* |
||||
* * Redistributions of source code must retain the above copyright |
||||
* notice, this list of conditions and the following disclaimer. |
||||
* * Redistributions in binary form must reproduce the above |
||||
* copyright notice, this list of conditions and the following disclaimer |
||||
* in the documentation and/or other materials provided with the |
||||
* distribution. |
||||
* * Neither the name of Google Inc. nor the names of its |
||||
* contributors may be used to endorse or promote products derived from |
||||
* this software without specific prior written permission. |
||||
* |
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
||||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
||||
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
* |
||||
*/ |
||||
|
||||
#include <string.h> |
||||
|
||||
#include "src/core/statistics/census_interface.h" |
||||
#include "src/core/statistics/census_tracing.h" |
||||
#include "src/core/statistics/census_tracing.h" |
||||
#include <grpc/support/log.h> |
||||
#include <grpc/support/port_platform.h> |
||||
#include <grpc/support/sync.h> |
||||
#include <grpc/support/thd.h> |
||||
#include <grpc/support/time.h> |
||||
#include "test/core/util/test_config.h" |
||||
|
||||
/* Ensure all possible state transitions are called without causing problem */ |
||||
static void test_init_shutdown() { |
||||
census_tracing_init(); |
||||
census_tracing_init(); |
||||
census_tracing_shutdown(); |
||||
census_tracing_shutdown(); |
||||
census_tracing_init(); |
||||
} |
||||
|
||||
static void test_start_op_generates_locally_unique_ids() { |
||||
/* Check that ids generated within window size of 1000 are unique.
|
||||
TODO(hongyu): Replace O(n^2) duplicate detection algorithm with O(nlogn) |
||||
algorithm. Enhance the test to larger window size (>10^6) */ |
||||
#define WINDOW_SIZE 1000 |
||||
census_op_id ids[WINDOW_SIZE]; |
||||
int i; |
||||
census_init(); |
||||
for (i = 0; i < WINDOW_SIZE; i++) { |
||||
ids[i] = census_tracing_start_op(); |
||||
census_tracing_end_op(ids[i]); |
||||
} |
||||
for (i = 0; i < WINDOW_SIZE - 1; i++) { |
||||
int j; |
||||
for (j = i + 1; j < WINDOW_SIZE; j++) { |
||||
GPR_ASSERT(ids[i].upper != ids[j].upper || ids[i].lower != ids[j].lower); |
||||
} |
||||
} |
||||
#undef WINDOW_SIZE |
||||
census_shutdown(); |
||||
} |
||||
|
||||
static void test_get_trace_method_name() { |
||||
census_op_id id; |
||||
const char write_name[] = "service/method"; |
||||
census_tracing_init(); |
||||
id = census_tracing_start_op(); |
||||
census_add_method_tag(id, write_name); |
||||
census_internal_lock_trace_store(); |
||||
{ |
||||
const char* read_name = |
||||
census_get_trace_method_name(census_get_trace_obj_locked(id)); |
||||
GPR_ASSERT(strcmp(read_name, write_name) == 0); |
||||
} |
||||
census_internal_unlock_trace_store(); |
||||
census_tracing_shutdown(); |
||||
} |
||||
|
||||
typedef struct thd_arg { |
||||
int num_done; |
||||
gpr_cv done; |
||||
gpr_mu mu; |
||||
} thd_arg; |
||||
|
||||
static void mimic_trace_op_sequences(void* arg) { |
||||
census_op_id id; |
||||
char method_name[200]; |
||||
int i = 0; |
||||
const int num_iter = 200; |
||||
thd_arg* args = (thd_arg*)arg; |
||||
GPR_ASSERT(args != NULL); |
||||
gpr_log(GPR_INFO, "Start trace op sequence thread."); |
||||
for (i = 0; i < num_iter; i++) { |
||||
id = census_tracing_start_op(); |
||||
census_add_method_tag(id, method_name); |
||||
/* pretend doing 1us work. */ |
||||
gpr_sleep_until(gpr_time_add(gpr_now(), gpr_time_from_micros(1))); |
||||
census_tracing_end_op(id); |
||||
} |
||||
gpr_log(GPR_INFO, "End trace op sequence thread."); |
||||
gpr_mu_lock(&args->mu); |
||||
args->num_done += 1; |
||||
gpr_cv_broadcast(&args->done); |
||||
gpr_mu_unlock(&args->mu); |
||||
} |
||||
|
||||
static void test_concurrency() { |
||||
#define NUM_THREADS 1000 |
||||
gpr_thd_id tid[NUM_THREADS]; |
||||
int i = 0; |
||||
thd_arg arg; |
||||
arg.num_done = 0; |
||||
gpr_mu_init(&arg.mu); |
||||
gpr_cv_init(&arg.done); |
||||
census_tracing_init(); |
||||
for (i = 0; i < NUM_THREADS; ++i) { |
||||
gpr_thd_new(tid + i, mimic_trace_op_sequences, &arg, NULL); |
||||
} |
||||
while (arg.num_done < NUM_THREADS) { |
||||
gpr_log(GPR_INFO, "num done %d", arg.num_done); |
||||
gpr_cv_wait(&arg.done, &arg.mu, gpr_inf_future); |
||||
} |
||||
census_tracing_shutdown(); |
||||
#undef NUM_THREADS |
||||
} |
||||
|
||||
static void test_add_method_tag_to_unknown_op_id() { |
||||
census_op_id unknown_id = {0xDEAD, 0xBEEF}; |
||||
int ret = 0; |
||||
census_tracing_init(); |
||||
ret = census_add_method_tag(unknown_id, "foo"); |
||||
GPR_ASSERT(ret != 0); |
||||
census_tracing_shutdown(); |
||||
} |
||||
|
||||
int main(int argc, char** argv) { |
||||
grpc_test_init(argc, argv); |
||||
test_init_shutdown(); |
||||
test_start_op_generates_locally_unique_ids(); |
||||
test_get_trace_method_name(); |
||||
test_concurrency(); |
||||
test_add_method_tag_to_unknown_op_id(); |
||||
return 0; |
||||
} |
Loading…
Reference in new issue