Esempio n. 1
0
static void test_add_sub(void) {
    int i;
    int j;
    int k;
    /* Basic addition and subtraction. */
    for (i = -100; i <= 100; i++) {
        for (j = -100; j <= 100; j++) {
            for (k = 1; k <= 10000000; k *= 10) {
                int sum = i + j;
                int diff = i - j;
                gpr_timespec it = gpr_time_from_micros(i * k, GPR_TIMESPAN);
                gpr_timespec jt = gpr_time_from_micros(j * k, GPR_TIMESPAN);
                gpr_timespec sumt = gpr_time_add(it, jt);
                gpr_timespec difft = gpr_time_sub(it, jt);
                if (gpr_time_cmp(gpr_time_from_micros(sum * k, GPR_TIMESPAN), sumt) !=
                        0) {
                    fprintf(stderr, "i %d  j %d  sum %d    sumt ", i, j, sum);
                    ts_to_s(sumt, &to_fp, stderr);
                    fprintf(stderr, "\n");
                    GPR_ASSERT(0);
                }
                if (gpr_time_cmp(gpr_time_from_micros(diff * k, GPR_TIMESPAN), difft) !=
                        0) {
                    fprintf(stderr, "i %d  j %d  diff %d    diff ", i, j, diff);
                    ts_to_s(sumt, &to_fp, stderr);
                    fprintf(stderr, "\n");
                    GPR_ASSERT(0);
                }
            }
        }
    }
}
Esempio n. 2
0
static void test_values(void) {
    int i;

    gpr_timespec x = gpr_time_0(GPR_CLOCK_REALTIME);
    GPR_ASSERT(x.tv_sec == 0 && x.tv_nsec == 0);

    x = gpr_inf_future(GPR_CLOCK_REALTIME);
    fprintf(stderr, "far future ");
    i_to_s(x.tv_sec, 16, 16, &to_fp, stderr);
    fprintf(stderr, "\n");
    GPR_ASSERT(x.tv_sec == INT64_MAX);
    fprintf(stderr, "far future ");
    ts_to_s(x, &to_fp, stderr);
    fprintf(stderr, "\n");

    x = gpr_inf_past(GPR_CLOCK_REALTIME);
    fprintf(stderr, "far past   ");
    i_to_s(x.tv_sec, 16, 16, &to_fp, stderr);
    fprintf(stderr, "\n");
    GPR_ASSERT(x.tv_sec == INT64_MIN);
    fprintf(stderr, "far past   ");
    ts_to_s(x, &to_fp, stderr);
    fprintf(stderr, "\n");

    for (i = 1; i != 1000 * 1000 * 1000; i *= 10) {
        x = gpr_time_from_micros(i, GPR_TIMESPAN);
        GPR_ASSERT(x.tv_sec == i / GPR_US_PER_SEC &&
                   x.tv_nsec == (i % GPR_US_PER_SEC) * GPR_NS_PER_US);
        x = gpr_time_from_nanos(i, GPR_TIMESPAN);
        GPR_ASSERT(x.tv_sec == i / GPR_NS_PER_SEC &&
                   x.tv_nsec == (i % GPR_NS_PER_SEC));
        x = gpr_time_from_millis(i, GPR_TIMESPAN);
        GPR_ASSERT(x.tv_sec == i / GPR_MS_PER_SEC &&
                   x.tv_nsec == (i % GPR_MS_PER_SEC) * GPR_NS_PER_MS);
    }

    /* Test possible overflow in conversion of -ve values. */
    x = gpr_time_from_micros(-(LONG_MAX - 999997), GPR_TIMESPAN);
    GPR_ASSERT(x.tv_sec < 0);
    GPR_ASSERT(x.tv_nsec >= 0 && x.tv_nsec < GPR_NS_PER_SEC);

    x = gpr_time_from_nanos(-(LONG_MAX - 999999997), GPR_TIMESPAN);
    GPR_ASSERT(x.tv_sec < 0);
    GPR_ASSERT(x.tv_nsec >= 0 && x.tv_nsec < GPR_NS_PER_SEC);

    x = gpr_time_from_millis(-(LONG_MAX - 997), GPR_TIMESPAN);
    GPR_ASSERT(x.tv_sec < 0);
    GPR_ASSERT(x.tv_nsec >= 0 && x.tv_nsec < GPR_NS_PER_SEC);

    /* Test general -ve values. */
    for (i = -1; i > -1000 * 1000 * 1000; i *= 7) {
        x = gpr_time_from_micros(i, GPR_TIMESPAN);
        GPR_ASSERT(x.tv_sec * GPR_US_PER_SEC + x.tv_nsec / GPR_NS_PER_US == i);
        x = gpr_time_from_nanos(i, GPR_TIMESPAN);
        GPR_ASSERT(x.tv_sec * GPR_NS_PER_SEC + x.tv_nsec == i);
        x = gpr_time_from_millis(i, GPR_TIMESPAN);
        GPR_ASSERT(x.tv_sec * GPR_MS_PER_SEC + x.tv_nsec / GPR_NS_PER_MS == i);
    }
}
Esempio n. 3
0
void launch_client() {
	/* For a single time frame. */
	struct timespec frame_time = {0};
	struct timespec frame_rem = {0};

	/* Stats */
	long rate_acc = 0;
	int64_t rate_tot = 0;

	/* For the whole experiment. */
	struct timespec exp_start = {0};
	struct timespec exp_end = {0};
	struct timespec frame_start = {0};

	struct sockaddr_in srvaddr = {0};
	char buf[1024] = {0};

	int sent = 0;
	int sock = 0;

	sock = socket(PF_INET, SOCK_DGRAM, 0);

	if(sock < 0) {
		printf("Error while creating the socket!\n");
		return;
	}

	srvaddr.sin_family = AF_INET;
	srvaddr.sin_port = htons(atoi(port));
	srvaddr.sin_addr.s_addr = inet_addr(addr);

	clock_gettime(CLOCK_REALTIME, &exp_start);
	clock_gettime(CLOCK_REALTIME, &frame_start);
	while(!stop) {
		sent = sendto(
			sock,
			buf, 1024,
			0,
			(struct sockaddr *)&srvaddr,
			sizeof(struct sockaddr_in));

		/* Time to end this... */
		clock_gettime(CLOCK_REALTIME, &exp_end);

		/* Report it every second. */
		if(ts_to_us(exp_end) - ts_to_us(frame_start) > 1000000) {
			printf("%lld bytes sent at %ld second, rate ",
				(long long int)rate_tot,
				(ts_to_us(exp_end) - ts_to_us(exp_start)) /
					1000000);
			print_bw_nicely(
				rate_acc,
				ts_to_us(exp_end) - ts_to_us(frame_start));
			printf("\n");

			rate_acc = 0;

			frame_start.tv_sec  = exp_end.tv_sec;
			frame_start.tv_nsec = exp_end.tv_nsec;
		}

		if(sent < 0) {
			printf("Error %d, could not send datagram!\n", sent);
			break;
		}

		rate_acc += sent;
		rate_tot += sent;

		if(ts_to_s(exp_end) - ts_to_s(exp_start) > bw_time) {
			break;
		}
	}

	printf("--- %s bandwidth statistics ---\n", addr);
	printf("%lld bytes transmitted in %ld seconds, rate ",
		(long long int)rate_tot,
		ts_to_s(exp_end) - ts_to_s(exp_start));
	print_bw_nicely(
		rate_tot,
		ts_to_us(exp_end) - ts_to_us(exp_start));
	printf("\n");
out:
	/* Close da socket. */
	close(sock);
}
Esempio n. 4
0
void launch_server() {
	struct sockaddr_in srvaddr = {0};

	struct timespec exp_start = {0};
	struct timespec frame_start = {0};
	struct timespec frame_end = {0};

	int64_t rate_acc = 0;
	int64_t rate_tot = 0;

	char buf[1024] = {0};
	int flags = 0;
	socklen_t len = 0;

	int recv = 0;
	/* Open da socket. */
	int sock = socket(PF_INET, SOCK_DGRAM, 0);

	if(sock < 0) {
		printf("Error while creating the socket!\n");
		return;
	}

	srvaddr.sin_family = AF_INET;
	srvaddr.sin_port = htons(atoi(port));
	srvaddr.sin_addr.s_addr = inet_addr(addr);

	if(bind(
		sock,
		(struct sockaddr *)&srvaddr,
		sizeof(struct sockaddr_in))) {

		printf("Failed to bind the socket.\n");
		close(sock);
		return;
	}

	len = sizeof(struct sockaddr_in);

	/* Non-blocking one... */
	flags = fcntl(sock, F_GETFL);
	flags |= O_NONBLOCK;
	fcntl(sock, F_SETFL, flags);

	clock_gettime(CLOCK_REALTIME, &exp_start);
	clock_gettime(CLOCK_REALTIME, &frame_start);
	while(!stop) {
		recv = recvfrom(
			sock,
			buf, 1024,
			0,
			(struct sockaddr *)&srvaddr,
			&len);

		/* Report it every second. */
		clock_gettime(CLOCK_REALTIME, &frame_end);
		if(ts_to_s(frame_end) - ts_to_s(frame_start) >= 1) {
			if(rate_acc > 0) {
				printf("%lld bytes received, rate ",
					(long long int)rate_tot);
				print_bw_nicely(
					(long long int)rate_acc,
					ts_to_us(frame_end) -
						ts_to_us(frame_start));
				printf("\n");

				rate_acc = 0;

				frame_start.tv_sec  = frame_end.tv_sec;
				frame_start.tv_nsec = frame_end.tv_nsec;
			}
		}

		if(recv < 0) {
			continue;
		}

		rate_acc += recv;
		rate_tot += recv;
	}

	/* Close da socket. */
	close(sock);
}