Perl语言学习(2)- gen_ref_sim_log

功能描述

生成一些sim result 文件,用于后续的学习,也可作为批量生成文件的程序参考。

Perl源代码

#!/user/bin/perl -w
use strict;

my $out_dir = "./out";
my $log_num = 200;
my $tab = " "x4;
my $debug = 0;

print "\n";

&gen_real_log_files($log_num, $out_dir);


sub gen_real_log_files
{
	my ($num, $dir) = @_;
	my $start_val = 2000;
	my $max_val = 500;
	my @cases;
	my $count = 0;

	print "[DEBUG] -- Start to generate $num log files...\n\n";

	&adjust_out_dir($dir);# input dir's name
	while ($count < $num)
	{
		my $case_id = $start_val + int(rand($max_val));
		if(!(grep {$case_id =~/$_/} @cases))# if $case_id is not in @cases
		{
			push (@cases, $case_id);
			$count++;
			&gen_one_log_file($case_id, $dir);
		}
	}

	print "[DEBUG] -- Complele to generate the $count log files into $dir dir.\n\n";
}

sub gen_one_log_file
{
	my ($id, $dir) = @_;
	my $sim_file = "${dir}/sim_${id}.log";
	my $result = "";
	my $status;
	my $max_val = 4000;
	my $rand_val = int(rand($max_val));

	$status = "OK"   if($rand_val >  ($max_val / 5));
	$status = "FAIL" if($rand_val <= ($max_val / 5));

	$result .= &obtain_sim_status($status, $id);

	# output the generated simulation log for current test
	open (OUT, ">", $sim_file) or die "Can not open $sim_file for writting!\n";
	print OUT $result;
	close (OUT);
	print "${tab}[DEBUG] -- the $sim_file has been generated!\n\n" if $debug;
}

sub obtain_sim_status
{
	my ($status, $case_id) = @_;
	my $str = "";
	$str .= "#" . "*"x30 . "\n";
	$str .= "# test_id     : $case_id\n";
	$str .= "# test_status : $status\n";
	$str .= "#" . "*"x30 . "\n";
	return $str;
}

sub adjust_out_dir
{
	my $dir_ref = shift; # shift @_;
	if( -e $dir_ref) # -e : whether dir exist or not
	{
		# delete all file
		unlink glob("$dir_ref/*");
		# system("rm -rf $dir_ref/*");
		print "${tab}[DEBUG] -- $dir_ref dir exist and complete to clean up its content.\n\n";
	}
	else
	{
		mkdir "$dir_ref", 0755 or die "Can not create $dir_ref dir!\n";
		print "${tab}[DEBUG] -- $dir_ref dir do not exist and complete to create it.\n\n";
	}
}

运行结果

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/meng1506789/article/details/107394654