我有一个应用程序,每次向它发出请求时都会写出日志。日期和时间信息指的是功能的结束时间。通过使用执行时间,我们计算请求开始的时间。
以下是日志目前的情况:
year | month | day | hour | minute | seconds | Thread | UserName | ExecTime
2014 | 3 | 26 | 8 | 57 | 300 | 1 | file_download | 33.86
2014 | 3 | 26 | 8 | 57 | 45 | 1 | file_upload | 0.359
2014 | 3 | 26 | 8 | 57 | 55 | 1 | folder_browse | 0.234
2014 | 3 | 26 | 8 | 58 | 11 | 1 | sending_email | 0.14
2014 | 3 | 26 | 8 | 58 | 20 | 1 | browsing_favorites | 0.985
2014 | 3 | 26 | 8 | 58 | 29 | 1 | file_download | 0.266
2014 | 3 | 26 | 8 | 58 | 33 | 1 | file_upload | 0.296
2014 | 3 | 26 | 8 | 58 | 37 | 1 | file_zip | 0.25
2014 | 3 | 26 | 8 | 58 | 41 | 1 | view_properties | 0.0
2014 | 3 | 26 | 8 | 58 | 46 | 1 | file_download | 0.187
仅限插图
由于请求可能会持续几分钟甚至几小时,因此很难按分钟计算每个线程的利用率。以下输出说明了我之后的输出:
Thread | month | day | hour | minute | % Busy
1 | 3 | 26 | 8 | 57 | 0.5
2 | 3 | 26 | 8 | 57 | 0.1
3 | 3 | 26 | 8 | 57 | 0.9
仅限插图
例如,如果请求在12:30:30开始执行并在12:32:30结束,则上表将如下所示:
Thread | month | day | hour | minute | % Busy
1 | 3 | 26 | 12 | 29 | 0
1 | 3 | 26 | 12 | 30 | 0.5
1 | 3 | 26 | 12 | 31 | 1
1 | 3 | 26 | 12 | 32 | 0.5
1 | 3 | 26 | 12 | 33 | 0
仅限插图
输出中的分钟数是第一个请求和最后一个请求之间可以在日志中找到的分钟数。
更新
以下是@TessellatingHeckler要求澄清的摘要:
我知道这很难,但我只需要一种能够很好地转化为代码的方法。我很高兴使用perl,powershell或java。
由于
答案 0 :(得分:1)
以下内容将为您完成大部分工作。你必须弄清楚Busy实际上意味着什么:
use List::Util qw(sum);
use strict;
use warnings;
<DATA>; # Skip Header
my %thread;
while (<DATA>) {
chomp;
my @data = split /\s*\|\s*/;
push @{$thread{$data[6]}}, \@data;
}
print "Thread | month | day | hour | minute | ExecTime\n";
for my $id (sort {$a <=> $b} keys %thread) {
my $time = sum map {$_->[7]} @{$thread{$id}};
printf "%-6s | %-5s | %-3s | %-4s | %-6s | %.03f\n", $id, @{$thread{$id}[0]}[1,2,3,4], $time;
}
__DATA__
year | month | day | hour | minute | seconds | Thread | ExecTime ( seconds )
2014 | 3 | 26 | 8 | 57 | 32 | 1 | 33.86
2014 | 3 | 26 | 8 | 57 | 45 | 2 | 0.359
2014 | 3 | 26 | 8 | 57 | 55 | 3 | 0.234
2014 | 3 | 26 | 8 | 58 | 11 | 1 | 0.14
2014 | 3 | 26 | 8 | 58 | 20 | 2 | 0.985
2014 | 3 | 26 | 8 | 58 | 29 | 3 | 0.266
2014 | 3 | 26 | 8 | 58 | 33 | 3 | 0.296
2014 | 3 | 26 | 8 | 58 | 37 | 1 | 0.25
2014 | 3 | 26 | 8 | 58 | 41 | 2 | 0.0
2014 | 3 | 26 | 8 | 58 | 46 | 1 | 0.187
输出:
Thread | month | day | hour | minute | ExecTime
1 | 3 | 26 | 8 | 57 | 34.437
2 | 3 | 26 | 8 | 57 | 1.344
3 | 3 | 26 | 8 | 57 | 0.796
答案 1 :(得分:1)
我不了解您请求中的所有部分,但我在PowerShell中试了一下。该脚本执行以下操作:
% Busy
返回ExecTime / 60秒,因此60秒ExecTime将是100%利用率已知问题:
% Busy
为starttime-minute返回秒/ 60秒。所以前。停止8.57.32并且持续34秒的操作实际上将在第56分钟显示为56%忙碌。要解决这个问题,我们必须在线程组中处理每个ExecTime,并在哈希表中每分钟汇总所有ExecTime部分,直到处理完所有ExecTimes,然后根据每分钟的exectime-sum创建结果。哈希表。脚本:
$path = c:\test.txt
$data = [System.IO.File]::ReadAllText($path).Replace(" ","") | ConvertFrom-Csv -Delimiter "|"
$data | Group-Object Thread | % {
#Get start-time
$_.Group | % { $_ | Add-Member -MemberType NoteProperty -Name StartTime -Value ([datetime]("{0}/{1}/{2} {3}:{4}:{5}" -f $_.month, $_.day, $_.year, $_.hour, $_.minute, $_.seconds)).AddSeconds(-($_.ExecTime)) }
#Group by minute
$_.Group | Group-Object {$_.starttime.minute} | % {
#Report per minute
New-Object psobject -Property @{
"Thread" = $_.Group[0].thread
"Month" = $_.Group[0].starttime.month
"Day" = $_.Group[0].starttime.day
"Hour" = $_.Group[0].starttime.hour
"Minute" = $_.Group[0].starttime.minute
"% Busy" = [math]::Round((($_.Group | Measure-Object -Property ExecTime -Sum).Sum / 60 * 100),2)
}
}
#Create 0% record for missing minutes
$min = ($_.Group | Sort-Object StartTime)[0].StartTime
$max = ($_.Group | Sort-Object StartTime)[-1].StartTime
for($d = $min; $d -le $max; $d = $d.AddMinutes(1)) {
if(-not ($_.Group | ? { $_.StartTime.Minute -eq $d.Minute })) {
New-Object psobject -Property @{
"Thread" = $_.Group[0].thread
"Month" = $d.Month
"Day" = $d.Day
"Hour" = $d.Hour
"Minute" = $d.Minute
"% Busy" = 0
}
}
}
} | Sort-Object Day, Hour, Minute, Thread | Select-Object "Thread", "Month", "Day", "Hour", "Minute", "% Busy"
的test.txt
year | month | day | hour | minute | seconds | Thread | ExecTime
2014 | 3 | 26 | 8 | 57 | 32 | 1 | 33.86
2014 | 3 | 26 | 8 | 57 | 45 | 2 | 0.359
2014 | 3 | 26 | 8 | 57 | 55 | 3 | 0.234
2014 | 3 | 26 | 8 | 58 | 11 | 1 | 0.14
2014 | 3 | 26 | 8 | 58 | 20 | 2 | 0.985
2014 | 3 | 26 | 8 | 58 | 29 | 3 | 0.266
2014 | 3 | 26 | 8 | 58 | 33 | 3 | 0.296
2014 | 3 | 26 | 8 | 58 | 37 | 1 | 0.25
2014 | 3 | 26 | 8 | 58 | 41 | 2 | 0.0
2014 | 3 | 26 | 8 | 58 | 46 | 1 | 0.187
2014 | 3 | 26 | 9 | 00 | 5 | 1 | 0.0
2014 | 3 | 26 | 9 | 00 | 8 | 1 | 0.187
输出:
Thread Month Day Hour Minute % Busy
------ ----- --- ---- ------ ------
1 3 26 8 56 56,43
1 3 26 8 57 0
2 3 26 8 57 0,6
3 3 26 8 57 0,39
1 3 26 8 58 0,96
2 3 26 8 58 1,64
3 3 26 8 58 0,94
1 3 26 8 59 0
1 3 26 9 0 0,31
我可能不会在此修复任何错误,因此请随时编辑修复错误或扩展错误的答案。这只是试图让球滚动。 :)