1
0
Fork 0
mirror of https://git.rwth-aachen.de/acs/public/villas/node/ synced 2025-03-09 00:00:00 +01:00

added new villas-test-cmp tool to compare to files (will be used by integration tests)

This commit is contained in:
Steffen Vogel 2017-03-12 17:10:45 -03:00
parent c9d554409b
commit 9a9e9c17d4
3 changed files with 231 additions and 22 deletions

View file

@ -2,7 +2,8 @@
TARGETS = $(BUILDDIR)/villas-node \
$(BUILDDIR)/villas-pipe \
$(BUILDDIR)/villas-signal \
$(BUILDDIR)/villas-test \
$(BUILDDIR)/villas-test-rtt \
$(BUILDDIR)/villas-test-cmp \
$(BUILDDIR)/villas-hook
SRC_LDLIBS = $(LDLIBS) -pthread -lm -lvillas
@ -31,7 +32,6 @@ $(TARGETS): $(BUILDDIR)/villas-%: $(BUILDDIR)/src/%.o
# Some additional prereqs for individual binaries
$(BUILDDIR)/villas-fpga: $(addprefix $(BUILDDIR)/src/,fpga-bench.o $(BENCH_OBJS))
# Compile executable objects
$(BUILDDIR)/src/%.o: src/%.c | $$(dir $$@)
$(CC) $(SRC_CFLAGS) -c $< -o $@

201
src/test-cmp.c Normal file
View file

@ -0,0 +1,201 @@
/** Compare two data files.
*
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
* @copyright 2017, Institute for Automation of Complex Power Systems, EONERC
*********************************************************************************/
#include <stdio.h>
#include <stdbool.h>
#include <math.h>
#include <getopt.h>
#include <jansson.h>
#include <villas/sample.h>
#include <villas/utils.h>
#include <villas/hist.h>
#include <villas/timing.h>
#include <villas/pool.h>
void usage()
{
printf("Usage: villas-test-cmp FILE1 FILE2 [OPTIONS]\n");
printf(" FILE1 first file to compare\n");
printf(" FILE2 second file to compare against\n");
printf(" OPTIONS the following optional options:\n");
printf(" -h print this usage information\n");
printf(" -d LVL adjust the debug level\n");
printf(" -j return the results as a JSON object\n");
printf(" -m return the results as a MATLAB struct\n");
printf(" -e EPS set epsilon for floating point comparisons to EPS\n");
printf(" -l LOW smallest value for histogram\n");
printf(" -H HIGH largest value for histogram\n");
printf(" -r RES bucket resolution for histogram\n");
printf("\n");
print_copyright();
}
int main(int argc, char *argv[])
{
int ret = 0;
double epsilon = 1e-9;
/* Histogram */
double low = 0; /**< Lowest value in histogram. */
double high = 1e-1; /**< Highest value in histogram. */
double res = 1e-3; /**< Histogram resolution. */
enum {
OUTPUT_JSON,
OUTPUT_MATLAB,
OUTPUT_HUMAN
} output = OUTPUT_HUMAN;
struct log log;
struct pool pool = { .state = STATE_DESTROYED };
struct hist hist;
struct sample *samples[2];
struct {
int flags;
char *path;
FILE *handle;
struct sample *sample;
} f1, f2;
/* Parse Arguments */
char c, *endptr;
while ((c = getopt (argc, argv, "hjmd:e:l:H:r:")) != -1) {
switch (c) {
case 'd':
log.level = strtoul(optarg, &endptr, 10);
goto check;
case 'e':
epsilon = strtod(optarg, &endptr);
goto check;
case 'j':
output = OUTPUT_JSON;
break;
case 'm':
output = OUTPUT_MATLAB;
break;
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 'h':
case '?':
usage();
exit(c == '?' ? EXIT_FAILURE : EXIT_SUCCESS);
}
continue;
check: if (optarg == endptr)
error("Failed to parse parse option argument '-%c %s'", c, optarg);
}
if (argc < optind + 2) {
usage();
exit(EXIT_FAILURE);
}
f1.path = argv[optind];
f2.path = argv[optind + 1];
log_init(&log, V, LOG_ALL);
log_start(&log);
hist_init(&hist, low, high, res);
pool_init(&pool, 1024, SAMPLE_LEN(DEFAULT_VALUES), &memtype_heap);
sample_alloc(&pool, samples, 2);
f1.sample = samples[0];
f2.sample = samples[1];
f1.handle = fopen(f1.path, "r");
if (!f1.handle)
serror("Failed to open file: %s", f1.path);
f2.handle = fopen(f2.path, "r");
if (!f2.handle)
serror("Failed to open file: %s", f2.path);
while (!feof(f1.handle) && !feof(f2.handle)) {
ret = sample_fscan(f1.handle, f1.sample, &f1.flags);
if (ret < 0) {
if (feof(f1.handle))
ret = 0;
goto out;
}
ret = sample_fscan(f2.handle, f2.sample, &f2.flags);
if (ret < 0) {
if (feof(f2.handle))
ret = 0;
goto out;
}
/* Compare sequence no */
if ((f1.flags & SAMPLE_SEQUENCE) && (f2.flags & SAMPLE_SEQUENCE)) {
if (f1.sample->sequence != f2.sample->sequence) {
printf("sequence no: %d != %d\n", f1.sample->sequence, f2.sample->sequence);
ret = -1;
goto out;
}
}
/* Compare timestamp */
if (time_delta(&f1.sample->ts.origin, &f2.sample->ts.origin) > epsilon) {
printf("ts.origin: %f != %f\n", time_to_double(&f1.sample->ts.origin), time_to_double(&f2.sample->ts.origin));
ret = -2;
goto out;
}
/* Collect historgram info of offset */
hist_put(&hist, time_delta(&f1.sample->ts.origin, &f2.sample->ts.received));
/* Compare data */
if (f1.sample->length != f2.sample->length) {
printf("length: %d != %d\n", f1.sample->length, f2.sample->length);
ret = -3;
goto out;
}
for (int i = 0; i < f1.sample->length; i++) {
if (fabs(f1.sample->data[i].f - f2.sample->data[i].f) > epsilon) {
printf("data[%d]: %f != %f\n", i, f1.sample->data[i].f, f2.sample->data[i].f);
ret = -4;
goto out;
}
}
}
out: sample_free(samples, 2);
fclose(f1.handle);
fclose(f2.handle);
switch (output) {
case OUTPUT_MATLAB:
hist_dump_matlab(&hist, stdout);
break;
case OUTPUT_JSON:
hist_dump_json(&hist, stdout);
break;
case OUTPUT_HUMAN:
hist_print(&hist, 1);
break;
}
hist_destroy(&hist);
pool_destroy(&pool);
return ret;
}

View file

@ -1,4 +1,4 @@
/** Some basic tests.
/** Measure round-trip time.
*
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
* @copyright 2017, Institute for Automation of Complex Power Systems, EONERC
@ -11,7 +11,7 @@
#include <ctype.h>
#include <sys/stat.h>
#include <villas/cfg.h>
#include <villas/super_node.h>
#include <villas/node.h>
#include <villas/utils.h>
#include <villas/hist.h>
@ -21,7 +21,7 @@
#include "config.h"
struct cfg cfg; /** <The global configuration */
struct super_node sn; /** <The global configuration */
static struct node *node;
@ -53,10 +53,17 @@ void quit(int signal, siginfo_t *sinfo, void *ctx)
void usage()
{
printf("Usage: villas-test CONFIG TEST NODE [ARGS]\n");
printf("Usage: villas-test-rtt CONFIG NODE [ARGS]\n");
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(" NODE name of the node which shoud be used\n");
printf(" ARGS the following optional options:\n");
printf(" -c CNT send CNT messages\n");
printf(" -f FD use file descriptor FD for result output instead of stdout\n");
printf(" -l LOW smallest value for histogram\n");
printf(" -H HIGH largest value for histogram\n");
printf(" -r RES bucket resolution for histogram\n");
printf(" -h show this usage information\n");
printf("\n");
print_copyright();
}
@ -68,22 +75,25 @@ int main(int argc, char *argv[])
exit(EXIT_FAILURE);
}
log_init(&cfg.log, V, LOG_ALL);
cfg_parse(&cfg, argv[1]);
log_init(&sn.log, V, LOG_ALL);
super_node_init(&sn);
super_node_parse_uri(&sn, argv[1]);
signals_init(quit);
rt_init(cfg.priority, cfg.affinity);
memory_init(cfg.hugepages);
rt_init(sn.priority, sn.affinity);
memory_init(sn.hugepages);
node = list_lookup(&cfg.nodes, argv[3]);
node = list_lookup(&sn.nodes, argv[3]);
if (!node)
error("There's no node with the name '%s'", argv[3]);
node_type_start(node->_vt, argc-3, argv+3, config_root_setting(&cfg.cfg));
node_type_start(node->_vt, argc-3, argv+3, config_root_setting(&sn.cfg));
node_start(node);
/* Parse Arguments */
char c, *endptr;
while ((c = getopt (argc-3, argv+3, "l:h:r:f:c:")) != -1) {
while ((c = getopt (argc-3, argv+3, "l:hH:r:f:c:")) != -1) {
switch (c) {
case 'c':
count = strtoul(optarg, &endptr, 10);
@ -94,12 +104,13 @@ int main(int argc, char *argv[])
case 'l':
low = strtod(optarg, &endptr);
goto check;
case 'h':
case 'H':
high = strtod(optarg, &endptr);
goto check;
case 'r':
res = strtod(optarg, &endptr);
goto check;
case 'h':
case '?':
usage();
exit(c == '?' ? EXIT_FAILURE : EXIT_SUCCESS);
@ -111,15 +122,12 @@ 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]);
test_rtt();
node_stop(node);
node_type_stop(node->_vt);
cfg_destroy(&cfg);
super_node_destroy(&sn);
return 0;
}
@ -132,7 +140,7 @@ void test_rtt() {
struct sample *smp_send = alloc(SAMPLE_LEN(2));
struct sample *smp_recv = alloc(SAMPLE_LEN(2));
hist_create(&hist, low, high, res);
hist_init(&hist, low, high, res);
/* Print header */
fprintf(stdout, "%17s%5s%10s%10s%10s%10s%10s\n", "timestamp", "seq", "rtt", "min", "max", "mean", "stddev");