1
0
forked from 0ad/0ad

Fix replayprofile/ graphing tool for Profiler1

All modern browsers block ajax request to local file.
This changes extract.pl to generate a single HTML file with data
embedded.
This is now the default behaviour, --to-json to export, --from-json to
load exported.

Patch by: nwtour
Reviewed By: wraitii
Differential Revision: https://code.wildfiregames.com/D3621
This was SVN commit r24975.
This commit is contained in:
wraitii 2021-03-02 08:35:39 +00:00
parent 23aa59e3d4
commit 5e5ea5cba5
3 changed files with 63 additions and 32 deletions

View File

@ -3,13 +3,21 @@
use strict; use strict;
use warnings; use warnings;
use Getopt::Long;
use File::Basename qw(dirname);
chdir(dirname(__FILE__));
# Run the game normally to generate a replay. # Run the game normally to generate a replay.
# Run the non-visual replay like: # Run the non-visual replay like:
# ./pyrogenesis -mod=public -replay=$HOME/.local/share/0ad/replays/0.0.23/2018-03-23_0010/commands.txt # ./pyrogenesis -mod=public -replay=$HOME/.local/share/0ad/replays/0.0.23/2018-03-23_0010/commands.txt
# to generate profile.txt. # to generate profile.txt.
# Then run: # Then run:
# perl extract.pl > data.json # perl extract.pl
# then open graph.html. # OR
# perl extract.pl --to-json > data.json ... [manipulation with data.json] ... perl extract.pl --from-json
# then open graph_onepage.html.
my ($to_json, $from_json); GetOptions ("to-json" => \$to_json, "from-json" => \$from_json);
open my $f, '../../../binaries/system/profile.txt' or die $!; open my $f, '../../../binaries/system/profile.txt' or die $!;
my $turn = 0; my $turn = 0;
@ -33,15 +41,55 @@ while (<$f>) {
} }
} }
print "[\n"; my $json = "[\n";
my $n = 0; my $n = 0;
for my $k (sort keys %s) { for my $k (sort keys %s) {
print ",\n" if $n++; $json .= ",\n" if $n++;
print "{label: '$k', data:["; $json .= "{label: '$k', data:[";
for my $t (0..$#{$s{$k}}) { for my $t (0..$#{$s{$k}}) {
print "," if $t; $json .= "," if $t;
print "[$t,".($s{$k}[$t] || 0)."]"; $json .= "[$t,".($s{$k}[$t] || 0)."]";
} }
print "]}"; $json .= "]}";
} }
print "\n]\n"; $json .= "\n]\n";
my $onepage = "graph_onepage.html";
sub recursive_file_slurp {
my $filename = shift;
my $output = '';
open my $fd, $filename or die "Open $filename : $!";
$output .= "<script>\n" if $filename =~ /\.js$/;
while(<$fd>){
if(/-- include js --/) {
$output .= recursive_file_slurp("jquery.js");
$output .= recursive_file_slurp("jquery.flot.js");
$output .= recursive_file_slurp("jquery.flot.navigate.js");
}
elsif(/-- include graph js --/) {
$output .= recursive_file_slurp("graph.js");
}
elsif(/-- include data json --/) {
$output .= $json;
}
else {
$output .= $_;
}
}
$output .= "</script>\n" if $filename =~ /\.js$/;
close($fd);
return $output;
}
if($to_json) {
print $json;
exit;
}
elsif($from_json) {
$json = recursive_file_slurp("data.json");
}
open my $opfd, '>', $onepage or die $!;
print $opfd recursive_file_slurp("graph.html");
close($opfd);

View File

@ -3,16 +3,11 @@
<head> <head>
<meta charset="UTF-8"/> <meta charset="UTF-8"/>
<title>0 A.D. Simulation Profiling</title> <title>0 A.D. Simulation Profiling</title>
<script src="jquery.js"></script> <!-- include js -->
<script src="jquery.flot.js"></script> <!-- include graph js -->
<script src="jquery.flot.navigate.js"></script>
<script src="graph.js"></script>
</head> </head>
<body> <body onload="showReplayData();">
<div> <div>
<label>File:
<input type="text" id="filename" value="data.json" onKeyUp="loadReplayGraphData()"></input>
</label>
<label>Graph: <label>Graph:
<select id="replayGraphSelection" size="1" onChange="showReplayData()"> <select id="replayGraphSelection" size="1" onChange="showReplayData()">
<option value="time" selected="selected">Performance</option> <option value="time" selected="selected">Performance</option>
@ -21,7 +16,6 @@
</select> </select>
</label> </label>
</div> </div>
<div id="errorMsg">File does not exist!</div>
<div id="replayGraphContainer"> <div id="replayGraphContainer">
<div id="replayGraphLegend" style="width:300px;float:left"></div> <div id="replayGraphLegend" style="width:300px;float:left"></div>
<div> <div>

View File

@ -50,6 +50,7 @@ var graphFormat = {
function showReplayData() function showReplayData()
{ {
loadReplayGraphData();
var displayedColumn = $("#replayGraphSelection").val(); var displayedColumn = $("#replayGraphSelection").val();
$.plot($("#replayGraph"), getReplayGraphData(displayedColumn), { $.plot($("#replayGraph"), getReplayGraphData(displayedColumn), {
@ -137,20 +138,8 @@ function showTooltip(x, y, displayedColumn, label, turn, value)
function loadReplayGraphData() function loadReplayGraphData()
{ {
$.ajax({ replayData =
"dataType": "json", <!-- include data json -->
"url": $("#filename").val(),
"success": function(data) {
$("#errorMsg").hide();
$("#replayGraphContainer").show();
replayData = data;
showReplayData();
},
"error": function() {
$("#replayGraphContainer").hide();
$("#errorMsg").show();
}
});
} }
$(loadReplayGraphData); $(loadReplayGraphData);