Files
spark/src/server/sparkd/cmds/QuickTest.cpp
T
2019-12-26 11:49:02 -05:00

186 lines
6.0 KiB
C++

#include "QuickTest.h"
#include <arpa/inet.h>
#include <fcntl.h>
#include <fstream>
#include <netinet/tcp.h>
#include <stdlib.h>
#include <sys/epoll.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/uio.h>
#include <unistd.h>
#include "lib/logging/logging.h"
#include "lib/orderman_v2/actors/network/Reactor.h"
#include "lib/orderman_v2/actors/network/IConnectionHandler.h"
#include "lib/orderman_v2/actors/network/IConnectionController.h"
#include "lib/utils/bts_assert.h"
#include "lib/utils/bts_chrono.h"
#include <boost/algorithm/string.hpp>
namespace bts {
namespace cmd {
using namespace orderman_v2;
int QuickTest::execute(std::vector<std::string> const &) {
std::ifstream f("/home/ppinto/Dropbox/20191120-MBO_xnym_cl_fut_1-eth");
std::string line;
size_t lines = 0;
std::vector<std::string_view> strs;
while (std::getline(f,line)) {
lines ++;
auto start = 0;
for (size_t i= 0; i < line.size();i++)
if (line[i] ==1 ) {
strs.push_back (std::string_view {line.data()+start, i-start+1});
start = i;
}
}
std::cout << lines << "\n";
return 0;
}
// struct MyHandler : IConnectionHandler {
// void init_handler(IConnectionController &c) override {
// c.connect();
// }
// void on_connect_success() override {
// std::cout << "Connected " << this << "\n";
// }
// void on_connect_fail() override {
// std::cout << "Connection failed " << this << "\n";
// }
// void on_disconnect() override {
// std::cout << "Disconnected " << this << "\n";
// }
// void on_data(std::string_view const &data) override {
// std::cout << "Got: " << data << " " << this << "\n";
// }
// };
// int QuickTest::execute(std::vector<std::string> const &args) {
// Reactor r;
// MyHandler h1;
// MyHandler h2;
// r.register_connection_handler({{args[0],3000}},h1);
// r.register_connection_handler({{args[0],3001}},h2);
// while (true) {
// r.do_work();
// }
// }
// int connect(std::string const &host, int port) {
// auto fd = socket(AF_INET, SOCK_STREAM, 0);
// int flag = 1;
// auto result =
// ::setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *)&flag,
// sizeof(int));
// bts_assert(result == 0);
// if (fd < 0) {
// bts_assert_msg(false, "No socket");
// }
// struct sockaddr_in serv_addr;
// memset(&serv_addr, '0', sizeof(serv_addr));
// serv_addr.sin_family = AF_INET;
// serv_addr.sin_port = htons(port);
// result = inet_pton(AF_INET, host.c_str(), &serv_addr.sin_addr);
// bts_assert(result > 0);
// result = ::connect(fd, (struct sockaddr *)&serv_addr, sizeof(serv_addr));
// if (result < 0) {
// bts_assert_msg(false, "Connect");
// } else {
// int flag = 1;
// result = ::setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *)&flag,
// sizeof(int));
// bts_assert(result == 0);
// return fd;
// }
// }
// int QuickTest::execute(std::vector<std::string> const &args) {
// auto n_connect = 2;
// auto epfd = epoll_create(2);
// bts_assert(epfd >= 0);
// for (int i = 0; i < n_connect; i++) {
// epoll_event ev;
// ev.events = EPOLLIN | EPOLLHUP | EPOLLERR | EPOLLRDHUP;
// auto fd = connect(args[0],3000 + i);
// ev.data.fd = fd;
// auto result = epoll_ctl(epfd, EPOLL_CTL_ADD, fd, &ev);
// bts_assert_msg(result == 0, "epoll");
// }
// std::vector<epoll_event> ev_list;
// ev_list.resize(n_connect);
// while (n_connect) {
// auto t = Timer::now();
// int ready = 0;
// auto n_iter = 1000;
// for (int i = 0; i < n_iter;i++)
// ready = epoll_wait(epfd, ev_list.data(), n_connect, 0);
// std::cout << ns_diff(Timer::now(),t) / n_iter << "ns\n";
// if (ready == -1) {
// if (errno == EINTR)
// continue;
// else
// bts_assert_msg(false, "epoll_wait");
// }
// for (int i = 0; i < ready; i++) {
// auto &ev = ev_list[i];
// // LOG(OrderMan, Error, "fd={} raw_ev: {} events: {} {} {} {}",
// (int) ev.data.fd,
// // (int) ev.events,
// // std::string((ev.events & EPOLLIN) ? "EPOLLIN" : ""),
// // std::string((ev.events & EPOLLHUP) ? "EPOLLHUP" : ""),
// // std::string((ev.events & EPOLLRDHUP) ? "EPOLLRDHUP" : ""),
// // std::string((ev.events & EPOLLERR) ? "EPOLLERR" : ""));
// if (ev.events & EPOLLIN) {
// char buffer[1024];
// auto s = read(ev.data.fd, buffer, sizeof(buffer));
// bts_assert_msg(s >= 0, "read");
// LOG(OrderMan, Error, "Read {} bytes from {}. Data: {}", s,
// (int) ev.data.fd,
// std::string(buffer, s));
// if (s) {
// auto w = write (ev.data.fd,buffer, s);
// if (w <= 0) {
// // LOG(OrderMan, Error, "Closing {} of a write error",
// (int) ev.data.fd); close(ev.data.fd); n_connect--;
// }
// }
// else if (ev.events & (EPOLLHUP | EPOLLERR | EPOLLRDHUP)) {
// // LOG(OrderMan, Error, "Closing {} of a zero sized read",
// (int) ev.data.fd); close(ev.data.fd); n_connect--;
// }
// } else if (ev.events & (EPOLLHUP | EPOLLERR | EPOLLRDHUP)) {
// // if EPOLLIN was set along with EPOLLHUP or EPOLLERR, we
// // pretend it is a normal read so that we drain the bytes
// // in the socket. The error or hup condition would remain
// // and we would catch in the next iteration.
// // LOG(OrderMan, Error, "Closing {}", (int) ev.data.fd);
// close(ev.data.fd);
// n_connect--;
// }
// }
// }
// return 0;
// }
std::string QuickTest::help() {
return "This is a place holder to make it easy to quickly write "
"discardable "
"test code";
}
} // namespace cmd
} // namespace bts