以特定速度进行标准输出输出

发布于 2024-07-07 06:14:07 字数 182 浏览 5 评论 0原文

对于我的应用程序(在 Linux 下)的负载测试,我正在寻找一个以特定速率(例如 100 字节/秒)在 stdout 上输出数据的工具,以便我可以将输出通过管道传输到 netcat,后者将其发送到我的应用程序应用。 dd 的某些选项是理想的,但到目前为止我还没有找到任何东西。 打印什么类型的数据并不重要(NUL 字节就可以)。 有什么提示吗?

For a load test of my application (under Linux), I'm looking for a tool that outputs data on stdout at a specific rate (like 100 bytes/s), so that I can pipe the output to netcat which sends it to my application. Some option for dd would be ideal, but I didn't find anything so far. It doesn't really matter what kind of data is printed (NUL bytes are OK). Any hints?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

寂寞笑我太脆弱 2024-07-14 06:14:08

我编写了一个快速程序,它接受一个参数,即每秒打印到标准输出的 A 个字符(负参数意味着没有速率限制)。 希望这可以帮助! :-) (在 GNU libc 上,您需要使用 -lrt 链接您的程序。)

编辑:默认情况下修改为打印点,除非指定了第二个参数,在这种情况下,第一个字符是即被使用。 (这意味着,如果您想打印 NUL 字符,只需指定一个空字符串作为第二个参数即可。:-))

#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>

int
sleeptill(const struct timespec *when)
{
    struct timespec now, diff;

    clock_gettime(CLOCK_REALTIME, &now);
    diff.tv_sec = when->tv_sec - now.tv_sec;
    diff.tv_nsec = when->tv_nsec - now.tv_nsec;
    while (diff.tv_nsec < 0) {
        diff.tv_nsec += 1000000000;
        --diff.tv_sec;
    }
    if (diff.tv_sec < 0)
        return 0;
    return nanosleep(&diff, 0);
}

int
main(int argc, char **argv)
{
    double rate = 0.0;
    char *endp;
    struct timespec start;
    double offset;

    if (argc >= 2) {
        rate = strtod(argv[1], &endp);
        if (endp == argv[1] || *endp)
            rate = 0.0;
        else
            rate = 1 / rate;

        if (!argv[2])
            argv[2] = ".";
    }

    if (!rate) {
        fprintf(stderr, "usage: %s rate [char]\n", argv[0]);
        return 1;
    }

    clock_gettime(CLOCK_REALTIME, &start);
    offset = start.tv_nsec / 1000000000.0;

    while (1) {
        struct timespec till = start;
        double frac;
        double whole;

        frac = modf(offset += rate, &whole);
        till.tv_sec += whole;
        till.tv_nsec = frac * 1000000000.0;
        sleeptill(&till);
        write(STDOUT_FILENO, argv[2], 1);
    }
}

I wrote a quick program that takes one argument, how many A characters to print to standard output per second (negative argument means no rate limiting). Hope this helps! :-) (On GNU libc, you will need to link your program with -lrt.)

Edit: revised to print dot by default, unless a second argument is specified, in which case the first character of that is used. (And that means, if you want to print the NUL character, just specify an empty string as the second argument. :-))

#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>

int
sleeptill(const struct timespec *when)
{
    struct timespec now, diff;

    clock_gettime(CLOCK_REALTIME, &now);
    diff.tv_sec = when->tv_sec - now.tv_sec;
    diff.tv_nsec = when->tv_nsec - now.tv_nsec;
    while (diff.tv_nsec < 0) {
        diff.tv_nsec += 1000000000;
        --diff.tv_sec;
    }
    if (diff.tv_sec < 0)
        return 0;
    return nanosleep(&diff, 0);
}

int
main(int argc, char **argv)
{
    double rate = 0.0;
    char *endp;
    struct timespec start;
    double offset;

    if (argc >= 2) {
        rate = strtod(argv[1], &endp);
        if (endp == argv[1] || *endp)
            rate = 0.0;
        else
            rate = 1 / rate;

        if (!argv[2])
            argv[2] = ".";
    }

    if (!rate) {
        fprintf(stderr, "usage: %s rate [char]\n", argv[0]);
        return 1;
    }

    clock_gettime(CLOCK_REALTIME, &start);
    offset = start.tv_nsec / 1000000000.0;

    while (1) {
        struct timespec till = start;
        double frac;
        double whole;

        frac = modf(offset += rate, &whole);
        till.tv_sec += whole;
        till.tv_nsec = frac * 1000000000.0;
        sleeptill(&till);
        write(STDOUT_FILENO, argv[2], 1);
    }
}
聚集的泪 2024-07-14 06:14:08

我认为这就是您真正想要的: Pipe Viewer

使用 | pv -qL <速率>[k|m|g|t] | <速率限制输出> 将把管道限制为请求的速率。

I think that this is what you really want: Pipe Viewer

Using <fast input> | pv -qL <rate>[k|m|g|t] | <rate-limited output> will limit the pipe to the requested rate.

暖风昔人 2024-07-14 06:14:08

如果您愿意一次获取所有一百个字节,那么您至少可以在 shell 中使用 sleep 和普通的旧 echo 进行循环,作为第一次尝试。

If you're fine with getting all hundred bytes at a time, you could do a loop with sleep and plain old echo in the shell as a first attempt at least.

太阳哥哥 2024-07-14 06:14:08

好吧,我现在使用 nuttcp 来进行“真正的”负载测试。 它的开销似乎相当低,因此测试系统不会受到太多干扰。

Well, I'm now using nuttcp to do "real" load tests instead. It seems to have quite low overhead, so the test system is not too much disturbed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文