Skip to content

Commit 80f1543

Browse files
exrmetrics: fix json output, better testing (AcademySoftwareFoundation#1990)
Signed-off-by: Peter Hillman <peterh@wetafx.co.nz> Co-authored-by: Cary Phillips <cary@ilm.com>
1 parent ede55b2 commit 80f1543

File tree

2 files changed

+43
-31
lines changed

2 files changed

+43
-31
lines changed

src/bin/exrmetrics/main.cpp

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "ImfVersion.h"
1515

1616
#include <algorithm>
17+
#include <iomanip>
1718
#include <iostream>
1819
#include <iterator>
1920
#include <list>
@@ -30,6 +31,7 @@ using std::list;
3031
using std::max;
3132
using std::min;
3233
using std::sort;
34+
using std::quoted;
3335

3436
using std::numeric_limits;
3537
using std::ostream;
@@ -234,7 +236,6 @@ printPartStats (
234236
out << indent << "\"re-read time\": ";
235237
printTiming (data.rereadPerf, out, raw, stats);
236238
}
237-
if (output) { out << '\n'; }
238239
}
239240

240241
void
@@ -260,7 +261,7 @@ jsonStats (
260261
out << " },\n";
261262
}
262263
out << " {\n"
263-
<< " \"file\": \"" << run.file << "\",\n";
264+
<< " \"file\":" << quoted(run.file) << ",\n";
264265
lastFileName = run.file;
265266
if (outputSizeData)
266267
{
@@ -341,10 +342,10 @@ jsonStats (
341342
}
342343
out << " ],\n";
343344
}
344-
out << " \"metrics\":\n";
345-
out << " [";
346-
firstEntryForFile = true;
347345
}
346+
out << " \"metrics\":\n";
347+
out << " [";
348+
firstEntryForFile = true;
348349
}
349350

350351
string compName;
@@ -358,38 +359,47 @@ jsonStats (
358359
out << '\n';
359360
out << " {\n";
360361
out << " \"compression\": \"" << compName << "\",\n";
361-
out << " \"pixel mode\": \"" << modeName (run.mode) << "\",\n";
362+
out << " \"pixel mode\": \"" << modeName (run.mode) << "\"";
362363

363364
if (outputSizeData)
364365
{
365-
out << " \"output size\": " << run.metrics.outputFileSize
366-
<< ",\n";
366+
out << ",\n";
367+
out << " \"output size\": " << run.metrics.outputFileSize;
367368
}
369+
if(timing)
370+
{
371+
out << ",\n";
372+
printPartStats ( out, run.metrics.totalStats, " ", timing, raw, stats);
368373

369-
printPartStats (
370-
out, run.metrics.totalStats, " ", timing, raw, stats);
371-
if (run.metrics.stats.size () > 1)
374+
}
375+
if (timing && run.metrics.stats.size () > 1)
372376
{
377+
out << ",\n";
373378
out << " \"parts\":\n";
374379
out << " [\n";
375380
//first print total statistics, then print all part data, unless there's only one part
376381
for (size_t part = 0; part < run.metrics.stats.size (); ++part)
377382
{
378383
out << " {\n";
379384
out << " \"part\": " << part << ",\n";
385+
380386
printPartStats (
381387
out,
382388
run.metrics.stats[part],
383389
" ",
384390
timing,
385391
raw,
386392
stats);
387-
out << " }";
393+
out << "\n }";
388394
if (part < run.metrics.stats.size () - 1) { out << ','; }
389395
out << endl;
390396
}
391397
out << " ]\n";
392398
}
399+
else
400+
{
401+
out << "\n";
402+
}
393403
out << " }";
394404
firstEntryForFile = false;
395405
}
@@ -482,8 +492,6 @@ main (int argc, char** argv)
482492

483493
if (opts.parse (argc, argv)) { return 1; }
484494

485-
if (opts.inFiles.size () == 0) { return 0; }
486-
487495
list<runData> data;
488496
try
489497
{

src/test/bin/test_exrmetrics.py

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -55,22 +55,26 @@ def cleanup():
5555
assert(result.returncode != 0), "\n"+result.stderr
5656
assert("Missing" in result.stderr),"expected 'Missing argument' error"
5757

58-
command = [exrmetrics]
59-
image = f"{image_dir}/TestImages/GrayRampsHorizontal.exr"
60-
command += ["-i",image, "--passes","2","-o",outimage]
61-
62-
result = run (command, stdout=PIPE, stderr=PIPE, universal_newlines=True)
63-
print(" ".join(result.args))
64-
print(result.returncode)
65-
print(result.stdout)
66-
print(result.stderr)
67-
assert(result.returncode == 0), "\n"+result.stderr
68-
assert(os.path.isfile(outimage)), "\nMissing " + outimage
69-
70-
# confirm data is valid JSON (will not be true if filename contains quotes)
71-
data = json.loads(result.stdout)
72-
assert(len(data)==1),"\n Unexpected list size in JSON object"
73-
for x in ['file','pixels','compression','part type','total raw size']:
74-
assert(x in data[0]),"\n Missing field "+x
58+
for image in [f"{image_dir}/TestImages/GrayRampsHorizontal.exr",f"{image_dir}/Beachball/multipart.0001.exr"]:
59+
for time in ["none","read","write","reread","read,write","read,reread","read,write,reread"]:
60+
for passes in ["1","2"]:
61+
for nosize in range(0,2):
62+
command = [exrmetrics]
63+
command += ["-i",image, "--passes",passes,"--time",time,"-o",outimage]
64+
if nosize:
65+
command += ['--no-size']
66+
result = run (command, stdout=PIPE, stderr=PIPE, universal_newlines=True)
67+
print(" ".join(result.args))
68+
print(result.returncode)
69+
print(result.stderr)
70+
assert(result.returncode == 0), "\n"+result.stderr
71+
assert(os.path.isfile(outimage)), "\nMissing " + outimage
72+
if len(result.stdout):
73+
# confirm data is valid JSON (will not be true if filename contains quotes)
74+
data = json.loads(result.stdout)
75+
assert(len(data)==1),"\n Unexpected list size in JSON object"
76+
if not nosize:
77+
for x in ['file','pixels','compression','part type','total raw size']:
78+
assert(x in data[0]),"\n Missing field "+x
7579

7680
print("success")

0 commit comments

Comments
 (0)