Skip to content

Commit 512f22e

Browse files
authored
Merge pull request #1 from gongweibao/master
Change to test time.
2 parents e37e3e3 + 2237fb5 commit 512f22e

File tree

2 files changed

+46
-10
lines changed

2 files changed

+46
-10
lines changed

greeter_async_client2.cc

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <iostream>
2020
#include <memory>
2121
#include <string>
22+
#include <sys/time.h>
2223

2324
#include <grpc++/grpc++.h>
2425
#include <grpc/support/log.h>
@@ -51,7 +52,12 @@ class GreeterClient {
5152

5253
// Assembles the client's payload and sends it to the server.
5354
void SayHello(const std::string& user) {
54-
void* payload_alloc = GenPayload(1024*1024*200);
55+
const int size = 12 * 1024 * 1024;
56+
char* payload_alloc = (char*)GenPayload(size);
57+
58+
struct timeval t0_copy, t1_copy;
59+
gettimeofday(&t0_copy, 0);
60+
//std::string pay_load(payload_alloc, size);
5561

5662
// Call object to store rpc data
5763
AsyncClientCall* call = new AsyncClientCall;
@@ -60,7 +66,13 @@ class GreeterClient {
6066
// Data we are sending to the server.
6167
HelloRequest request;
6268
request.set_name(user);
63-
request.set_payload(*reinterpret_cast<std::string*>(payload_alloc));
69+
request.set_payload(payload_alloc, size);
70+
71+
gettimeofday(&t1_copy, 0);
72+
double dif = double((t1_copy.tv_sec - t0_copy.tv_sec) * 1000.0 +
73+
(t1_copy.tv_usec - t0_copy.tv_usec) / 1000.0);
74+
printf("time is %.2f ms\n", dif);
75+
6476
// auto* pl = request.mutable_payload();
6577
// pl = reinterpret_cast<std::string*>(payload_alloc);
6678
free(payload_alloc);
@@ -150,8 +162,15 @@ int main(int argc, char** argv) {
150162
// are created. This channel models a connection to an endpoint (in this case,
151163
// localhost at port 50051). We indicate that the channel isn't authenticated
152164
// (use of InsecureChannelCredentials()).
153-
GreeterClient greeter(grpc::CreateChannel(
154-
"localhost:50051", grpc::InsecureChannelCredentials()));
165+
grpc::ChannelArguments args;
166+
args.SetMaxSendMessageSize(std::numeric_limits<int>::max());
167+
args.SetMaxReceiveMessageSize(std::numeric_limits<int>::max());
168+
169+
auto ch = std::shared_ptr<grpc::Channel>( grpc::CreateCustomChannel("127.0.0.1:50051", grpc::InsecureChannelCredentials(), args));
170+
171+
//GreeterClient greeter(grpc::CreateChannel(
172+
// "localhost:50051", grpc::InsecureChannelCredentials()));
173+
GreeterClient greeter(ch);
155174

156175
// Spawn reader thread that loops indefinitely
157176
std::thread thread_ = std::thread(&GreeterClient::AsyncCompleteRpc, &greeter);

greeter_async_client3.cc

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
#include <iostream>
2020
#include <memory>
2121
#include <string>
22+
#include <atomic>
23+
#include <sys/time.h>
2224

2325
#include <grpc++/generic/generic_stub.h>
2426
#include <grpc++/grpc++.h>
@@ -75,13 +77,28 @@ class AsyncClientCallDirect {
7577
::grpc::CompletionQueue* cq,
7678
::grpc::GenericStub* stub) {
7779
// encode a message directly
78-
void* payload_alloc = GenPayload(1024*1024*8);
80+
// void* payload_alloc = GenPayload(1024*1024*12);
81+
82+
const int size = 12 * 1024 * 1024;
83+
char* payload_alloc = (char*)GenPayload(size);
84+
85+
struct timeval t0_copy, t1_copy;
86+
gettimeofday(&t0_copy, 0);
87+
//std::string pay_load(payload_alloc, size);
88+
89+
7990
start_time_ = GetTimestamp();
8091
HelloRequest request;
8192
request.set_name(user);
8293
auto* pl = request.mutable_payload();
8394
pl = reinterpret_cast<std::string*>(payload_alloc);
8495
RequestToByteBuffer(request, &request_buf_);
96+
97+
gettimeofday(&t1_copy, 0);
98+
double dif = double((t1_copy.tv_sec - t0_copy.tv_sec) * 1000.0 +
99+
(t1_copy.tv_usec - t0_copy.tv_usec) / 1000.0);
100+
printf("time is %.2f ms\n", dif);
101+
85102

86103
call_ = std::move(stub->Call(&context_, "/helloworld.Greeter/SayHello", cq, this));
87104
call_times = 0;
@@ -92,7 +109,7 @@ class AsyncClientCallDirect {
92109
cond_.notify_one();
93110
}
94111
void OnComplete(bool ok) {
95-
std::cout << "call times: " << call_times << std::endl;
112+
// std::cout << "call times: " << call_times << std::endl;
96113
if (call_times == 0) {
97114
std::unique_lock<std::mutex> lock(mu_);
98115
cond_.wait(lock, [this]{return this->call_cond_ == 1;});
@@ -107,10 +124,10 @@ class AsyncClientCallDirect {
107124
if (status.ok()) {
108125
HelloReply reply;
109126
GrpcParseProto(response_buf_, &reply);
110-
std::cout << "call end ok: " << response_buf_.Length() << std::endl;
111-
std::cout << "reply: " << reply.message() << " timespent: "
112-
<< GetTimestamp() - start_time_
113-
<< " start time " << start_time_ << std::endl;
127+
//std::cout << "call end ok: " << response_buf_.Length() << std::endl;
128+
//std::cout << "reply: " << reply.message() << " timespent: "
129+
// << GetTimestamp() - start_time_
130+
//<< " start time " << start_time_ << std::endl;
114131
} else {
115132
std::cout << "call end error";
116133
}

0 commit comments

Comments
 (0)