11 /* Timing and delay functionality. We allow the get and put operations to have
12 * a specified minimum latency, and will sleep if the operation would have
13 * completed before that time. This can be used in benchmarking to see the
14 * effect of increasing latency on an application.
16 * Call gettimeofday at the start of the operation to get the starting time,
17 * and then use minimum_delay to wait until at least the specified number of
18 * microseconds have elapsed. */
19 static void minimum_delay(const struct timeval *tv, unsigned int min_usec)
22 if (gettimeofday(&now, NULL) < 0)
25 int64_t t1, t2; /* Times converted to straight microseconds */
26 t1 = (int64_t)tv->tv_sec * 1000000 + tv->tv_usec;
27 t2 = (int64_t)now.tv_sec * 1000000 + now.tv_usec;
29 unsigned int elapsed = t2 - t1;
30 if (elapsed >= min_usec)
33 struct timespec delay;
34 delay.tv_sec = (min_usec - elapsed) / 1000000;
35 delay.tv_nsec = ((min_usec - elapsed) % 1000000) * 1000;
37 while (nanosleep(&delay, &delay) != 0 && errno == EINTR)
44 KeyValueRpcService::KeyValueRpcService(Backend *backend)
49 KeyValueRpcService::~KeyValueRpcService()
53 void KeyValueRpcService::PutValue(
54 ::google::protobuf::RpcController* /*controller*/,
55 const ::kvrpc::Put* request,
56 ::kvrpc::PutReply* response,
57 ::google::protobuf::Closure* done)
60 gettimeofday(&start, NULL);
62 if (_backend->Put(request->key(), request->value()))
64 response->set_result(kvrpc::SUCCESS);
68 response->set_result(kvrpc::FAILURE);
71 //minimum_delay(&start, 1000000);
75 void KeyValueRpcService::GetValue(
76 ::google::protobuf::RpcController* /*controller*/,
77 const ::kvrpc::Get* request,
78 ::kvrpc::GetReply* response,
79 ::google::protobuf::Closure* done)
82 gettimeofday(&start, NULL);
85 if (_backend->Get(request->key(), &value))
87 response->set_result(kvrpc::SUCCESS);
88 response->set_value(value);
92 response->set_result(kvrpc::FAILURE);
95 //minimum_delay(&start, 1000000);
99 }; // namespace kvstore