1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00
RIOT/dist/tools/linux-border_router/testing.c

180 lines
3.9 KiB
C
Raw Normal View History

/*
* Copyright (C) 2014 Freie Universität Berlin.
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*/
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <sys/time.h>
#include "testing.h"
#include "flowcontrol.h"
struct timeval timers[256];
struct test_stats *stats = NULL;
int run_counter;
FILE *stats_file = NULL;
2013-06-21 04:00:45 +02:00
void init_file(const char *skeleton_file_name,
int runs_per_test, float interval)
{
FILE *skeleton_file = NULL;
char line[1024];
2013-06-21 04:00:45 +02:00
skeleton_file = fopen(skeleton_file_name, "r");
2013-06-21 04:00:45 +02:00
while (fgets(line, 1024, skeleton_file) != NULL) {
if (strncmp(line, "# sending window size=%d\n", 1024) == 0) {
fprintf(stats_file, line, BORDER_SWS);
2013-06-21 04:00:45 +02:00
}
else if (strncmp(line, "# count=%ld (-c)\n", 1024) == 0) {
fprintf(stats_file, line, runs_per_test);
2013-06-21 04:00:45 +02:00
}
else if (strncmp(line, "# interval=%f (-i)\n", 1024) == 0) {
fprintf(stats_file, line, interval);
2013-06-21 04:00:45 +02:00
}
else {
fprintf(stats_file, "%s", line);
}
}
fclose(skeleton_file);
}
2013-06-21 04:00:45 +02:00
int testing_init(const char *stats_file_name,
const char *skeleton_file_name,
int runs_per_test, float interval)
{
if (stats_file != NULL) {
return -1;
}
2013-06-21 04:00:45 +02:00
stats = (struct test_stats *)calloc(runs_per_test, sizeof(struct test_stats));
run_counter = 0;
2013-06-21 04:00:45 +02:00
stats_file = fopen(stats_file_name, "w");
2013-06-21 04:00:45 +02:00
if (stats_file == NULL) {
return -1;
}
2013-06-21 04:00:45 +02:00
init_file(skeleton_file_name, runs_per_test, interval);
return 0;
}
2013-06-21 04:00:45 +02:00
int testing_destroy()
{
int res, i;
2013-06-21 04:00:45 +02:00
for (i = 0; i < run_counter; i++) {
2013-06-21 04:00:45 +02:00
fprintf(stats_file, "%7d\t%3d\t%7ld\n",
i, stats[i].seq_num, stats[i].time_diff
);
}
2013-06-21 04:00:45 +02:00
free(stats);
2013-06-21 04:00:45 +02:00
stats = NULL;
2013-06-21 04:00:45 +02:00
res = fclose(stats_file);
2013-06-21 04:00:45 +02:00
stats_file = NULL;
2013-06-21 04:00:45 +02:00
return res;
}
2013-06-21 04:00:45 +02:00
void testing_start(uint8_t seq_num)
{
if (stats_file == NULL) {
return;
}
2013-06-21 04:00:45 +02:00
gettimeofday(&(timers[seq_num]), NULL);
}
2013-06-21 04:00:45 +02:00
void testing_stop(uint8_t seq_num)
{
if (stats_file == NULL) {
return;
}
2013-06-21 04:00:45 +02:00
struct timeval start = timers[seq_num], end;
2013-06-21 04:00:45 +02:00
gettimeofday(&end, NULL);
stats[run_counter].seq_num = seq_num;
2013-06-21 04:00:45 +02:00
stats[run_counter].time_diff = (end.tv_sec - start.tv_sec) * 1000;
2013-06-21 04:00:45 +02:00
stats[run_counter].time_diff += (end.tv_usec - start.tv_usec) / 1000;
2013-06-21 04:00:45 +02:00
run_counter++;
}
void generate_filename(
2013-06-21 04:00:45 +02:00
char *filename,
const char *results_dir_name,
int runs_per_test,
float interval)
{
time_t today;
struct tm *tmp = NULL;
char timestr[16];
FILE *test = NULL;
int count = 1;
2013-06-21 04:00:45 +02:00
today = time(NULL);
tmp = localtime(&today);
2013-06-21 04:00:45 +02:00
if (tmp == NULL) {
perror("localtime");
return;
}
2013-06-21 04:00:45 +02:00
strftime(timestr, 16, "%Y.%m.%d", tmp);
do {
sprintf(filename,
2013-06-21 04:00:45 +02:00
"%s/%s-Results-Ping6_%d_%d_%f-%d.txt",
results_dir_name,
timestr,
BORDER_SWS,
runs_per_test,
interval,
count++
);
}
while ((test = fopen(filename, "r")) != NULL);
2013-06-21 04:00:45 +02:00
if (test != NULL) {
fclose(test);
}
}
void start_test(const char *ping_addr,
2013-06-21 04:00:45 +02:00
const char *results_dir_name, const char *skeleton_file,
int runs_per_test, float interval)
{
printf("%d, %f\n", runs_per_test, interval);
char command[50];
char filename[50];
generate_filename(filename, results_dir_name, runs_per_test, interval);
2013-06-21 04:00:45 +02:00
sprintf(command, "ping6 -v -f -c %d -i %f -W 1 abcd::d",
runs_per_test,
interval
2013-06-21 04:00:45 +02:00
);
testing_init(filename, skeleton_file, runs_per_test, interval);
puts(command);
system(command);
testing_destroy();
}