2008-10-28 78 views
6

对于我的应用程序(在Linux下)的负载测试,我正在寻找一种工具,以特定速率(如100字节/秒)在标准输出上输出数据,以便管道输出到netcat,将其发送到我的应用程序。 dd的某些选项会很理想,但到目前为止我没有找到任何东西。打印什么样的数据并不重要(NUL字节正常)。任何提示?以特定速度执行stdout输出

+0

一位记者向我表示,使用管道连接netcat可能意味着管道可能是一个限制因素。因此,以您所寻求的速度直接写入套接字更为可靠。 – 2008-10-28 11:26:11

回答

3

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

使用<fast input> | pv -qL <rate>[k|m|g|t] | <rate-limited output>将限制管道到要求的速率。

2

如果您一次只能获取所有的一百个字节,您可以至少在第一次尝试时在shell中执行一个与sleep和普通旧版echo的循环。

+0

好主意 - 类似于“while [true];做echo -n”1234567890“;使用10000次;完成”已经有效。 – oliver 2008-10-28 10:16:57

0

那么,我现在使用nuttcp来做“真正的”负载测试。它的开销似乎很低,所以测试系统不会受到太多干扰。

5

我写了一个快速程序,它带一个参数,每秒打印多少个A字符到标准输出(负参数表示没有速率限制)。希望这可以帮助! :-)(在GNU libc上,您需要将程序链接到-lrt。)

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

#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); 
    } 
} 
+1

使用最新版本,您现在可以指定0作为费率。从技术上讲,它不会永远等待(数学上应该如此);相反,它一直等到time_t(2038年1月,在具有32位time_t的平台上)结束时。尽管如此,还是有一段时间需要等待。 :-D – 2008-10-28 10:58:00