1
0
Fork 0
mirror of https://git.rwth-aachen.de/acs/public/villas/node/ synced 2025-03-16 00:00:02 +01:00
VILLASnode/src/test.c

192 lines
4.6 KiB
C
Raw Normal View History

/** Some basic tests.
*
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
2015-06-02 21:53:04 +02:00
* @copyright 2014-2015, Institute for Automation of Complex Power Systems, EONERC
* This file is part of S2SS. All Rights Reserved. Proprietary and confidential.
2015-08-07 01:11:43 +02:00
* Unauthorized copying of this file, via any medium is strictly prohibited.
2015-06-02 21:53:04 +02:00
*********************************************************************************/
#include <stdlib.h>
2015-06-03 10:13:35 +02:00
#include <string.h>
#include <signal.h>
#include <unistd.h>
#include <ctype.h>
#include <sys/stat.h>
#include "config.h"
#include "cfg.h"
#include "msg.h"
#include "node.h"
#include "utils.h"
#include "hist.h"
2015-06-02 22:06:15 +02:00
#include "timing.h"
#include "pool.h"
2015-06-02 22:06:15 +02:00
2015-12-13 00:46:22 +01:00
struct settings settings; /** <The global configuration */
static struct node *node;
/* Test options */
2015-12-13 00:46:22 +01:00
static int running = 1; /**< Initiate shutdown if zero */
static int count = -1; /**< Amount of messages which should be sent (default: -1 for unlimited) */
/** File descriptor for Matlab results.
* This allows you to write Matlab results in a seperate log file:
*
* ./test etc/example.conf rtt -f 3 3>> measurement_results.m
*/
2015-06-02 22:04:03 +02:00
static int fd = STDOUT_FILENO;
2015-12-13 00:46:22 +01:00
/* Histogram */
static double low = 0; /**< Lowest value in histogram. */
static double high = 2e-4; /**< Highest value in histogram. */
static double res = 1e-5; /**< Histogram resolution. */
#define CLOCK_ID CLOCK_MONOTONIC
/* Prototypes */
void test_rtt();
2015-06-05 12:15:31 +02:00
void quit()
{
running = 0;
}
int main(int argc, char *argv[])
{
config_t config;
2015-08-07 01:11:43 +02:00
if (argc < 4) {
printf("Usage: %s CONFIG TEST NODE [ARGS]\n", argv[0]);
printf(" CONFIG path to a configuration file\n");
printf(" TEST the name of the test to execute: 'rtt'\n");
printf(" NODE name of the node which shoud be used\n\n");
printf("Simulator2Simulator Server %s (built on %s %s)\n",
BLU(VERSION), MAG(__DATE__), MAG(__TIME__));
2015-06-02 22:44:57 +02:00
printf(" copyright 2014-2015, Institute for Automation of Complex Power Systems, EONERC\n");
printf(" Steffen Vogel <StVogel@eonerc.rwth-aachen.de>\n");
exit(EXIT_FAILURE);
}
/* Setup signals */
struct sigaction sa_quit = {
.sa_flags = SA_SIGINFO,
.sa_sigaction = quit
};
sigemptyset(&sa_quit.sa_mask);
sigaction(SIGTERM, &sa_quit, NULL);
sigaction(SIGINT, &sa_quit, NULL);
2015-10-11 13:25:06 +02:00
log_init();
config_init(&config);
2015-06-02 22:04:03 +02:00
config_parse(argv[1], &config, &settings, &nodes, NULL);
2015-08-07 01:11:43 +02:00
node = list_lookup(&nodes, argv[3]);
if (!node)
error("There's no node with the name '%s'", argv[3]);
node_init(node->_vt, argc-3, argv+3, config_root_setting(&config));
node_start(node);
2015-08-07 01:11:43 +02:00
/* Parse Arguments */
char c, *endptr;
while ((c = getopt (argc-3, argv+3, "l:h:r:f:c:")) != -1) {
switch (c) {
case 'c':
count = strtoul(optarg, &endptr, 10);
goto check;
case 'f':
fd = strtoul(optarg, &endptr, 10);
goto check;
case 'l':
low = strtod(optarg, &endptr);
goto check;
case 'h':
high = strtod(optarg, &endptr);
goto check;
case 'r':
res = strtod(optarg, &endptr);
goto check;
case '?':
if (optopt == 'c')
error("Option -%c requires an argument.", optopt);
else if (isprint(optopt))
error("Unknown option '-%c'.", optopt);
else
error("Unknown option character '\\x%x'.", optopt);
2015-03-21 18:03:55 +01:00
exit(EXIT_FAILURE);
default:
2015-03-21 18:03:55 +01:00
abort();
}
2015-08-07 01:11:43 +02:00
continue;
check:
if (optarg == endptr)
error("Failed to parse parse option argument '-%c %s'", c, optarg);
}
if (!strcmp(argv[2], "rtt"))
test_rtt();
else
error("Unknown test: '%s'", argv[2]);
node_stop(node);
node_deinit(node->_vt);
config_destroy(&config);
return 0;
}
void test_rtt() {
struct timespec sent, recv;
2015-06-02 22:43:57 +02:00
struct hist hist;
struct msg *m;
m = msg_create(0);
2015-06-02 22:43:57 +02:00
hist_create(&hist, low, high, res);
2015-06-02 22:43:57 +02:00
/* Print header */
fprintf(stdout, "%17s%5s%10s%10s%10s%10s%10s\n", "timestamp", "seq", "rtt", "min", "max", "mean", "stddev");
while (running && (count < 0 || count--)) {
clock_gettime(CLOCK_ID, &sent);
m->ts.sec = sent.tv_sec;
m->ts.nsec = sent.tv_nsec;
node_write_single(node, m); /* Ping */
node_read_single(node, m); /* Pong */
clock_gettime(CLOCK_ID, &recv);
double rtt = time_delta(&recv, &sent);
2015-06-02 22:43:57 +02:00
if (rtt < 0)
warn("Negative RTT: %f", rtt);
2015-08-07 01:11:43 +02:00
2015-06-02 22:43:57 +02:00
hist_put(&hist, rtt);
m->sequence++;
fprintf(stdout, "%10lu.%06lu%5u%10.3f%10.3f%10.3f%10.3f%10.3f\n",
recv.tv_sec, recv.tv_nsec / 1000, m->sequence,
2015-06-02 22:43:57 +02:00
1e3 * rtt, 1e3 * hist.lowest, 1e3 * hist.highest,
1e3 * hist_mean(&hist), 1e3 * hist_stddev(&hist));
}
struct stat st;
if (!fstat(fd, &st)) {
FILE *f = fdopen(fd, "w");
2015-06-02 22:43:57 +02:00
hist_matlab(&hist, f);
}
else
error("Invalid file descriptor: %u", fd);
2015-08-07 01:11:43 +02:00
hist_print(&hist);
2015-06-02 22:43:57 +02:00
hist_destroy(&hist);
}