Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 23 additions & 4 deletions greeter_async_client2.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <iostream>
#include <memory>
#include <string>
#include <sys/time.h>

#include <grpc++/grpc++.h>
#include <grpc/support/log.h>
Expand Down Expand Up @@ -51,7 +52,12 @@ class GreeterClient {

// Assembles the client's payload and sends it to the server.
void SayHello(const std::string& user) {
void* payload_alloc = GenPayload(1024*1024*200);
const int size = 12 * 1024 * 1024;
char* payload_alloc = (char*)GenPayload(size);

struct timeval t0_copy, t1_copy;
gettimeofday(&t0_copy, 0);
//std::string pay_load(payload_alloc, size);

// Call object to store rpc data
AsyncClientCall* call = new AsyncClientCall;
Expand All @@ -60,7 +66,13 @@ class GreeterClient {
// Data we are sending to the server.
HelloRequest request;
request.set_name(user);
request.set_payload(*reinterpret_cast<std::string*>(payload_alloc));
request.set_payload(payload_alloc, size);

gettimeofday(&t1_copy, 0);
double dif = double((t1_copy.tv_sec - t0_copy.tv_sec) * 1000.0 +
(t1_copy.tv_usec - t0_copy.tv_usec) / 1000.0);
printf("time is %.2f ms\n", dif);

// auto* pl = request.mutable_payload();
// pl = reinterpret_cast<std::string*>(payload_alloc);
free(payload_alloc);
Expand Down Expand Up @@ -150,8 +162,15 @@ int main(int argc, char** argv) {
// are created. This channel models a connection to an endpoint (in this case,
// localhost at port 50051). We indicate that the channel isn't authenticated
// (use of InsecureChannelCredentials()).
GreeterClient greeter(grpc::CreateChannel(
"localhost:50051", grpc::InsecureChannelCredentials()));
grpc::ChannelArguments args;
args.SetMaxSendMessageSize(std::numeric_limits<int>::max());
args.SetMaxReceiveMessageSize(std::numeric_limits<int>::max());

auto ch = std::shared_ptr<grpc::Channel>( grpc::CreateCustomChannel("127.0.0.1:50051", grpc::InsecureChannelCredentials(), args));

//GreeterClient greeter(grpc::CreateChannel(
// "localhost:50051", grpc::InsecureChannelCredentials()));
GreeterClient greeter(ch);

// Spawn reader thread that loops indefinitely
std::thread thread_ = std::thread(&GreeterClient::AsyncCompleteRpc, &greeter);
Expand Down
29 changes: 23 additions & 6 deletions greeter_async_client3.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
#include <iostream>
#include <memory>
#include <string>
#include <atomic>
#include <sys/time.h>

#include <grpc++/generic/generic_stub.h>
#include <grpc++/grpc++.h>
Expand Down Expand Up @@ -75,13 +77,28 @@ class AsyncClientCallDirect {
::grpc::CompletionQueue* cq,
::grpc::GenericStub* stub) {
// encode a message directly
void* payload_alloc = GenPayload(1024*1024*8);
// void* payload_alloc = GenPayload(1024*1024*12);

const int size = 12 * 1024 * 1024;
char* payload_alloc = (char*)GenPayload(size);

struct timeval t0_copy, t1_copy;
gettimeofday(&t0_copy, 0);
//std::string pay_load(payload_alloc, size);


start_time_ = GetTimestamp();
HelloRequest request;
request.set_name(user);
auto* pl = request.mutable_payload();
pl = reinterpret_cast<std::string*>(payload_alloc);
RequestToByteBuffer(request, &request_buf_);

gettimeofday(&t1_copy, 0);
double dif = double((t1_copy.tv_sec - t0_copy.tv_sec) * 1000.0 +
(t1_copy.tv_usec - t0_copy.tv_usec) / 1000.0);
printf("time is %.2f ms\n", dif);


call_ = std::move(stub->Call(&context_, "/helloworld.Greeter/SayHello", cq, this));
call_times = 0;
Expand All @@ -92,7 +109,7 @@ class AsyncClientCallDirect {
cond_.notify_one();
}
void OnComplete(bool ok) {
std::cout << "call times: " << call_times << std::endl;
// std::cout << "call times: " << call_times << std::endl;
if (call_times == 0) {
std::unique_lock<std::mutex> lock(mu_);
cond_.wait(lock, [this]{return this->call_cond_ == 1;});
Expand All @@ -107,10 +124,10 @@ class AsyncClientCallDirect {
if (status.ok()) {
HelloReply reply;
GrpcParseProto(response_buf_, &reply);
std::cout << "call end ok: " << response_buf_.Length() << std::endl;
std::cout << "reply: " << reply.message() << " timespent: "
<< GetTimestamp() - start_time_
<< " start time " << start_time_ << std::endl;
//std::cout << "call end ok: " << response_buf_.Length() << std::endl;
//std::cout << "reply: " << reply.message() << " timespent: "
// << GetTimestamp() - start_time_
//<< " start time " << start_time_ << std::endl;
} else {
std::cout << "call end error";
}
Expand Down