问题描述
所以我正在尝试 load 测试返回JSON值的REST API.
So I am trying to load test a REST API which returns a JSON value.
为此,我正在创建perl脚本的多个实例.
To do that I am creating multiple instances of the perl script.
Perl脚本基本上会调用该URL,然后尝试进入decode_json
.显然,当产生大量负载时,它就会失败.
The Perl script basically calls that URL, and tries to decode_json
. Obviously when substantial load is generated, it fails.
现在我面临的问题是-命令提示符上显示一个错误,但未在文件中写入该错误消息.
Now the problem I face is- An error is displayed on command prompt but does not write that error message in a file.
错误消息是
malformed JSON string, neither array, object, number, string or atom, at character offset 0 (before "Can't connect to 209...") at json_load_test.pl line 39.
第39行下面的所有三种尝试均指:
In all the three attempts below line 39 refers to:
decode_json($actual_response);
我只是在命令提示符下以以下方式运行脚本:
I am simply running the script on the command prompt as:
perl json_load_test.pl >> logs/output.txt
我希望在"output.txt"中写入错误消息
我的三个失败尝试如下.
my $ua = LWP::UserAgent->new;
$ua->timeout(3);
$ua->env_proxy;
my $response = $ua->get("http://$j_env/jobs/all.json?status=active");
my $actual_response=$response->decoded_content;
decode_json($actual_response);
if ($? == -1)
{print "\n Failed to execute: $!\n"; }
尝试2:
my $ua = LWP::UserAgent->new;
$ua->timeout(3);
$ua->env_proxy;
my $response = $ua->get("http://$j_env/jobs/all.json?status=active");
my $actual_response=$response->decoded_content;
my $perl_scalar= decode_json($actual_response);
if ($perl_scalar)
{ok(1,"For process $u2 inside counter $counter ");}
else
{ok(0,"FAILED!!! process $u2 inside counter $counter");}
尝试3:
my $ua = LWP::UserAgent->new;
$ua->timeout(3);
$ua->env_proxy;
my $response = $ua->get("http://$j_env/jobs/all.json?status=active");
my $actual_response=$response->decoded_content;
decode_json($actual_response) or die "FAILED!!!!";
推荐答案
您的错误消息似乎来自stderr,而不是stdout.因此,
It looks like your error message is coming from stderr, not stdout. Thus,
perl json_load_test.pl >> logs/output.txt 2>> logs/errors.txt
或者类似的东西.如果您想将两个文件都放在一个文件中:
Or something to that effect. If you want both in one file:
perl json_load_test.pl 2>&1 >> logs/output.txt
如果出于某种原因您想在perl中捕获错误并将其发送到stdout,则可以:
If for some reason you wanted to trap the error in the perl and send it to stdout, you could:
eval {
decode_json($actual_response);
1;
} or do {
my $e = $@;
print "$e\n";
};
正如daxim在编辑说明中指出的,Try :: Tiny可能更简单:
As daxim points out in the edit notes, Try::Tiny may be simpler:
use Try::Tiny;
try {
decode_json($actual_response);
} catch {
print "$_\n";
};
这篇关于如何捕获“解码JSON失败"消息? Perl中出现错误消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!