Loading...
Searching...
No Matches
set_random_data.cpp
Go to the documentation of this file.
1/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20#include <fcntl.h>
21#include <sys/stat.h>
22#include <sys/types.h>
23#include <time.h>
24#include <unistd.h>
25
26#include <array>
27#include <chrono>
28#include <fstream>
29#include <iostream>
30#include <string>
31
32std::string Exec(const char* cmd) {
33 std::array<char, 128> buffer;
34 std::string result;
35
36 auto pipe = popen(cmd, "r"); // get rid of shared_ptr
37
38 if (!pipe) throw std::runtime_error("popen() failed!");
39
40 while (!feof(pipe)) {
41 if (fgets(buffer.data(), 128, pipe) != nullptr) result += buffer.data();
42 }
43
44 auto rc = pclose(pipe);
45
46 if (rc == EXIT_SUCCESS) { // == 0
47
48 } else if (rc == EXIT_FAILURE) { // EXIT_FAILURE is not used by all programs,
49 // maybe needs some adaptation.
50 }
51 return result;
52}
53
54int main(int argc, char** argv) {
55 if (argc < 3) {
56 printf("<test/loop> <value> \n");
57 return 0;
58 }
59 std::string command = argv[1];
60 std::string value = argv[2];
61 std::string output = "";
62 int count = 0;
63
64 if (command == "test") {
65 for (int i = 0; i < std::stoi(value); i++) {
66 usleep(1000000);
67 std::string test =
68 "/home/jyu25/nexres/bazel-bin/example/kv_server_tools "
69 "/home/jyu25/nexres/example/kv_client_config.config set " +
70 std::to_string(std::rand() % 500) + " " +
71 std::to_string(std::rand() % 500);
72 output = Exec(test.c_str());
73 std::cout << i << " " << output << std::endl;
74 }
75 } else if (command == "loop") {
76 while (1) {
77 std::string test =
78 "/home/jyu25/nexres/bazel-bin/example/kv_server_tools "
79 "/home/jyu25/nexres/example/kv_client_config.config set " +
80 std::to_string(std::rand() % 500) + " " +
81 std::to_string(std::rand() % 500);
82 output = Exec(test.c_str());
83 std::cout << ++count << " " << output << std::endl;
84 }
85 }
86}
int main(int argc, char **argv)
std::string Exec(const char *cmd)