为什么这段代码需要很长时间才能在服务器上执行

时间:2017-03-06 05:21:14

标签: c loadrunner

我们使用LoadRunner为应用程序运行性能测试。测试通常用Ansi-C编写。

我们有一个简单的base64编码函数:

void base64encode(char *src, char *dest, int srcLen, int destLen)
{
  int i=0;
  int slen= srcLen;
  for(i=0;i<slen && i<destLen;i+=3,src+=3)
  {
    *(dest++)=base64encode_lut[(*src&0xFC)>>0x2];
    *(dest++)=base64encode_lut[(*src&0x3)<<0x4|(*(src+1)&0xF0)>>0x4];
    *(dest++)=((i+1)<slen)?base64encode_lut[(*(src+1)&0xF)<<0x2|(*(src+2)&0xC0)>>0x6]:'=';
    *(dest++)=((i+2)<slen)?base64encode_lut[*(src+2)&0x3F]:'=';
  }
  *dest='\0';
}

此代码在开发人员计算机(64位Windows 10计算机)上运行时,代码在一秒钟内运行以获得简单图像(srcLen大约7k)。 在生产服务器(32位Windows 2012,VM)上运行时,对于同一图像,执行需要10到20 分钟

任何人都可以解释为什么以及如何避免这个问题?我不确定它是否是LoadRunner或责任代码。

编辑:添加调用编码函数的代码:

long infile; // file pointer
char *buffer; // buffer to read file contents into
char *filename = "DFC_COLOR.jpg"; // file to read
int fileLen; // file size
int bytesRead; // bytes read from file
char *encoded;
int dest_size;

web_set_max_html_param_len("999999999");
infile = fopen(filename, "rb");
if (!infile) {
    lr_error_message("Unable to open file %s", filename);
    return;
}

// get the file length
fseek(infile, 0, SEEK_END);
fileLen = ftell(infile);
fseek(infile, 0, SEEK_SET);
lr_log_message("File length is: %9d bytes.", fileLen);


// Allocate memory for buffer to read file
buffer = (char *)malloc(fileLen + 1);
if (!buffer) {
    lr_error_message("Could not malloc %10d bytes", fileLen + 1);
    fclose(infile);
    return;
}

// Read file contents into buffer
bytesRead = fread(buffer, 1, fileLen, infile);
if (bytesRead != fileLen)
{
    lr_error_message("File length is %10d bytes but only read %10d bytes", fileLen, bytesRead);
}
else
{
    lr_log_message("Successfully read %9d bytes from file: ", bytesRead);
}
fclose(infile);


// Save the buffer to a loadrunner parameter
lr_save_var(buffer, bytesRead, 0, "fileDataParameter");

// calculate the destination size
dest_size = 1 + ((bytesRead + 2) / 3 * 4);
encoded = (char *)malloc(dest_size);
memset(encoded, 0, dest_size);

// encode the buffer
base64encode(buffer, encoded, bytesRead, dest_size);

1 个答案:

答案 0 :(得分:0)

在非共享/代理资源物理计算机上运行单个虚拟用户,看看您是否具有相同的性能。您无需确定虚拟机环境中的优先级和资源池。您可能正在高度过载的VM主机上运行,​​并且您只需要等待,因为管理程序会决定谁在哪个时间获取给定的资源集。将单个虚拟用户移动到单个硬件主机可提供常见的苹果到梨(橘子到金橘),以便进行比较。

LoadRunner还包含符合RFC的base64编码和解码算法作为其核心集的一部分,因此您无需重新编码。通常,这用作SMTP或DNS虚拟用户的一部分,但您可以加载DLL,对函数进行原型设计并继续前进。你可以在\ include \ mic_socket.h头文件中找到现有的函数原型。这是一篇关于如何实现它的好文章。我知道编辑会尖叫并将其降级为外部链接,因此您可能希望快速捕获链接

https://northwaysolutions.com/blog/loadrunner-vugen-encoding-and-decoding-base64/#.WL1vZxLytlc

处理此问题的另一种方法是作为数据格式扩展。解码已作为基本扩展进行了覆盖,因此您只需要根据需要处理编码。使用Google为适当的项目提取对Loadrunner,DFE和base64的引用。您还可以在本地文档集中找到DFE开发人员指南,该指南随您的LoadRunner副本一起安装。