最近再做一个跟海量存储相关的项目测试,需要通过LR模拟用户大量上传和下载文件,请求是Rest或Soap,同时还要模拟多种大小尺寸不一的文件
通常情况下,都是使用简单的post协议即可:
方法一:
web_submit_data("importStudent.do", "Action=https://testserver/console/importStudent.do", "Method=POST", "EncType=multipart/form-data", "RecContentType=text/html", "Referer=https://testserver/console/displayStudentList.do", "Snapshot=t12.inf", "Mode=HTTP", ITEMDATA, "Name=uploadFile", "Value=D://Excel//data161955.zip", "File=yes", ENDITEM, LAST); |
后续对上传的文件进行MD5值比较,发现不一致,仔细查看后,发现上传的文件内容被loadrunner添加了几行额外的值,content-type等,无奈下,重写读文件的方式,我通过如下程序实现了1到10M文本文件的上传,如果上传二进制文件,body的写法有变化:
方法二:
vuser_init() { char fileName[] = "D:/Script/CreateObj_10M/tools.zip"; int len = 0; int readLen=0; int tmpLen=1; int runLen =104857; int cLen = 0; if ( (file_stream = fopen(fileName, "rb")) == NULL) { lr_message("open file failed!\n"); return -1; } fseek(file_stream, 0, 2); len = ftell(file_stream); lr_message("file length is: %d bytes", len); readbuf = (char *) malloc(len+1); memset(readbuf, 0, len+1); if ( runLen > len) { runLen = len; } fseek(file_stream, 0, 0); while(feof(file_stream)== 0){ tmpLen = fread(readbuf + readLen, 1,runLen , file_stream); readLen += tmpLen; cLen = readLen + runLen; if (cLen > len) { runLen = cLen - len; } if (tmpLen==0) { break; } } |
//关闭文件句柄 fclose(file_stream); //保存参数 lr_save_string(readbuf,"bodys"); } Action() { lr_start_transaction("CreateObj_1M"); web_add_header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8.."); web_add_header("Content-Type", "text/plain; charset=UTF-8"); web_add_header("Authorization", "YWS {userkey}:XXXXXXX"); web_custom_request("CreateObj", "URL=http://smartstorage{id}.yoyoyws.com:38080/test00bucket00/{IterationNum}-10M", "Method=PUT", "TargetFrame=", "Resource=0", "RecContentType=application/xml", "Referer=", "Mode=HTML", "Body={bodys}", //如果是二进制文件,可以改成BodyBinary LAST ); returnCode = web_get_int_property(HTTP_INFO_RETURN_CODE); if ( returnCode!=201 ) { lr_end_transaction("CreateObj_1M",LR_FAIL); }else{ lr_end_transaction("CreateObj_1M",LR_PASS); } return 0; } |
但是这个程序依然有问题,当打开是二进制文件,或者文件内容过大的时候,依然会出现文件内容不一致的问题,我后续试验后,会持续更新。
最新内容请见作者的GitHub页:http://qaseven.github.io/