Made ViewImagePerfTest extend ViewImageTest to ease automated image

render tests
This commit is contained in:
luc 2015-10-29 23:24:39 +01:00
parent 4a03cf06e1
commit 2895ab552a
2 changed files with 374 additions and 194 deletions

View File

@ -1,8 +1,12 @@
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import net.yacy.cora.util.ConcurrentLog;
import net.yacy.peers.graphics.EncodedImage;
@ -36,145 +40,190 @@ import net.yacy.server.serverObjects;
* @author luc
*
*/
public class ViewImagePerfTest {
public class ViewImagePerfTest extends ViewImageTest {
/** Default image */
private static final String DEFAULT_IMG_RESOURCE = "/viewImageTest/test/JPEG_example_JPG_RIP_100.jpg";
/** Default minimum measurement time */
private static final int DEFAULT_MIN_MEASURE_TIME = 10;
/** Default render max width (JPEG_example_JPG_RIP_100.jpg width / 10) */
private static final int DEFAULT_MAX_WIDTH = 31;
/** Default render max height (JPEG_example_JPG_RIP_100.jpg height / 10) */
private static final int DEFAULT_MAX_HEIGHT = 23;
/** Default encoding format */
private static final String DEFAUL_EXT = "png";
/**
* @param testFile
* file to load
* @return testFile content as a bytes array
* @throws IOException
* when an error occured while loading
*/
private static byte[] getBytes(File testFile) throws IOException {
InputStream inStream = new FileInputStream(testFile);
byte[] res = new byte[inStream.available()];
try {
inStream.read(res);
} finally {
inStream.close();
}
return res;
}
/** Minimum measurement time */
private int minMeasureTime;
/**
* @param args
* first item may contain file URL
* @return file to be used : specified as first in args or default one
* main parameters : args[7] may contain minimum measurement time
* in secondes. Default : 10.
*/
private static File getTestFile(String args[]) {
String fileURL;
if (args != null && args.length > 0) {
fileURL = args[0];
} else {
URL defaultURL = ViewImagePerfTest.class.getResource(DEFAULT_IMG_RESOURCE);
if (defaultURL == null) {
throw new IllegalArgumentException("File not found : " + DEFAULT_IMG_RESOURCE);
}
fileURL = defaultURL.getFile();
}
return new File(fileURL);
}
/**
* Build post parameters to use with ViewImage
*
* @param args
* main parameters : second and third items may respectively
* contain max width and max height
* @return a serverObjects instance
*/
private static serverObjects makePostParams(String args[]) {
serverObjects post = new serverObjects();
int maxWidth = DEFAULT_MAX_WIDTH;
if (args != null && args.length > 1) {
maxWidth = Integer.parseInt(args[1]);
}
post.put("maxwidth", String.valueOf(maxWidth));
int maxHeight = DEFAULT_MAX_HEIGHT;
if (args != null && args.length > 2) {
maxHeight = Integer.parseInt(args[2]);
}
post.put("maxheight", String.valueOf(maxHeight));
/* Make it square by default */
post.put("quadratic", "");
return post;
public ViewImagePerfTest(String args[]) {
this.minMeasureTime = getMinMeasurementTime(args);
}
/**
*
* @param args
* main parameters : fourth item may contain extension
* main parameters : args[7] may contain minimum measurement time
* in secondes. Default : 10.
* @return extension to use for encoding
*/
private static String getEncodingExt(String args[]) {
String ext = DEFAUL_EXT;
if (args != null && args.length > 3) {
ext = args[3];
protected int getMinMeasurementTime(String args[]) {
int time;
if (args != null && args.length > 7) {
time = Integer.parseInt(args[7]);
} else {
time = DEFAULT_MIN_MEASURE_TIME;
}
return ext;
return time;
}
/**
* Test image is parsed and rendered again and again until 20 seconds
* elapsed. Then measured statistics are displayed.
* Process inFile image, update processedFiles list and failures map, and append measurements to results_perfs.txt. All
* parameters must not be null.
*
* @param ext
* output encoding image format
* @param outDir
* output directory
* @param post
* ViewImage post parameters
* @param failures
* map failed file urls to eventual exception
* @param inFile
* file image to process
* @throws IOException
* when an read/write error occured
*/
@Override
protected void processFile(String ext, File outDir, serverObjects post, Map<String, Exception> failures,
File inFile) throws IOException {
/* Delete eventual previous result file */
System.out
.println("Measuring ViewImage render with file : " + inFile.getAbsolutePath() + " encoded To : " + ext);
File outFile = new File(outDir, inFile.getName() + "." + ext);
if (outFile.exists()) {
outFile.delete();
}
byte[] resourceb = getBytes(inFile);
String urlString = inFile.getAbsolutePath();
EncodedImage img = null;
Exception error = null;
try {
long beginTime, time, minTime = Long.MAX_VALUE, maxTime = 0, meanTime = 0, totalTime = 0;
int step = 0;
for (step = 0; (totalTime / 1000000000) < this.minMeasureTime; step++) {
beginTime = System.nanoTime();
img = ViewImage.parseAndScale(post, true, urlString, ext, false, resourceb);
time = System.nanoTime() - beginTime;
if (img == null) {
break;
}
minTime = Math.min(minTime, time);
maxTime = Math.max(maxTime, time);
totalTime += time;
}
if (img == null) {
System.out.println("Image could not be rendered!");
} else {
meanTime = totalTime / step;
PrintWriter resultsWriter = new PrintWriter(new FileWriter(new File(outDir, "results_perfs.txt"), true));
try {
writeMessage("Measured ViewImage render with file : " + inFile.getAbsolutePath() + " encoded To : "
+ ext, resultsWriter);
writeMessage("Render total time (ms) : " + (totalTime) / 1000000 + " on " + step + " steps.",
resultsWriter);
writeMessage("Render mean time (ms) : " + (meanTime) / 1000000, resultsWriter);
writeMessage("Render min time (ms) : " + (minTime) / 1000000, resultsWriter);
writeMessage("Render max time (ms) : " + (maxTime) / 1000000, resultsWriter);
} finally {
resultsWriter.close();
}
}
} catch (Exception e) {
error = e;
}
if (img == null) {
failures.put(urlString, error);
} else {
FileOutputStream outFileStream = null;
try {
outFileStream = new FileOutputStream(outFile);
img.getImage().writeTo(outFileStream);
} finally {
if (outFileStream != null) {
outFileStream.close();
}
img.getImage().close();
}
}
}
/**
* Test image(s) (default : classpath resource folder /viewImageTest/test/)
* are parsed and rendered again and again until specified time (default :
* 10 seconds) elapsed. Then rendered image is written to outDir for visual
* check and measured statistics are displayed.
*
* @param args
* may be empty or contain parameters to override defaults :
* <ul>
* <li>args[0] : input image file URL. Default :
* viewImageTest/test/JPEG_example_JPG_RIP_100.jpg</li>
* <li>args[1] : max width (in pixels) for rendered image.
* Default : default image width divided by 10.</li>
* <li>args[2] : max height (in pixels) for rendered image.
* Default : default image height divided by 10.</li>
* <li>args[3] : output format name. Default : "png".</li>
* <li>args[0] : input image file URL or folder containing image
* files URL. Default : classpath resource /viewImageTest/test/
* </li>
* <li>args[1] : output format name (for example : "jpg") for
* rendered image. Defaut : "png".</li>
* <li>args[2] : ouput folder URL. Default :
* "[system tmp dir]/ViewImageTest".</li>
* <li>args[3] : max width (in pixels) for rendered image. May be
* set to zero to specify no max width. Default : no value.</li>
* <li>args[4] : max height (in pixels) for rendered image. May
* be set to zero to specify no max height. Default : no value.
* </li>
* <li>args[5] : set to "quadratic" to render square output
* image. May be set to any string to specify no quadratic shape.
* Default : false.</li>
* <li>args[6] : set to "recursive" to process recursively sub
* folders. Default : false.</li>
* <li>args[7] : minimum measurement time in secondes. Default :
* 10.</li>
* </ul>
* @throws IOException
* when a read/write error occured
*/
public static void main(String args[]) throws IOException {
File imgFile = getTestFile(args);
byte[] resourceb = getBytes(imgFile);
String ext = getEncodingExt(args);
serverObjects post = makePostParams(args);
ViewImagePerfTest test = new ViewImagePerfTest(args);
File inFile = test.getInputURL(args);
String ext = test.getEncodingExt(args);
File outDir = test.getOuputDir(args);
boolean recursive = test.isRecursive(args);
serverObjects post = test.makePostParams(args);
outDir.mkdirs();
String urlString = imgFile.getAbsolutePath();
File[] inFiles;
if (inFile.isFile()) {
inFiles = new File[1];
inFiles[0] = inFile;
System.out.println(
"Measuring ViewImage render with file : " + inFile.getAbsolutePath() + " encoded To : " + ext);
} else if (inFile.isDirectory()) {
inFiles = inFile.listFiles();
System.out.println("Measuring ViewImage render with files in folder : " + inFile.getAbsolutePath()
+ " encoded To : " + ext);
} else {
inFiles = new File[0];
}
if (inFiles.length == 0) {
throw new IllegalArgumentException(inFile.getAbsolutePath() + " is not a valid file or folder url.");
}
System.out.println("Measuring ViewImage render with file : " + urlString + " encoded To : " + ext);
System.out.println("Rendered images will be written in dir : " + outDir.getAbsolutePath());
List<File> processedFiles = new ArrayList<File>();
Map<String, Exception> failures = new TreeMap<>();
try {
/* Max test total time (s) */
int maxTotalTime = 20;
long beginTime, time, minTime = Long.MAX_VALUE, maxTime = 0, meanTime = 0, totalTime = 0;
int step = 0;
for (step = 0; (totalTime / 1000000000) < maxTotalTime; step++) {
beginTime = System.nanoTime();
EncodedImage img = ViewImage.parseAndScale(post, true, urlString, ext, false, resourceb);
time = System.nanoTime() - beginTime;
minTime = Math.min(minTime, time);
maxTime = Math.max(maxTime, time);
totalTime += time;
if (img == null) {
throw new IOException("Image render failed");
}
}
meanTime = totalTime / step;
System.out.println("Render total time (ms) : " + (totalTime) / 1000000 + " on " + step + " steps.");
System.out.println("Render mean time (ms) : " + (meanTime) / 1000000);
System.out.println("Render min time (ms) : " + (minTime) / 1000000);
System.out.println("Render max time (ms) : " + (maxTime) / 1000000);
long time = System.nanoTime();
test.processFiles(ext, recursive, outDir, post, inFiles, processedFiles, failures);
time = System.nanoTime() - time;
test.displayResults(processedFiles, failures, time, outDir);
} finally {
ConcurrentLog.shutdown();
}

View File

@ -1,15 +1,16 @@
import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.net.URL;
import java.util.HashMap;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.apache.commons.io.filefilter.FileFileFilter;
import java.util.TreeMap;
import net.yacy.cora.util.ConcurrentLog;
import net.yacy.peers.graphics.EncodedImage;
@ -58,7 +59,7 @@ public class ViewImageTest {
* @throws IOException
* when an error occured while loading
*/
private static byte[] getBytes(File testFile) throws IOException {
protected byte[] getBytes(File testFile) throws IOException {
InputStream inStream = new FileInputStream(testFile);
byte[] res = new byte[inStream.available()];
try {
@ -76,7 +77,7 @@ public class ViewImageTest {
* @return file or folder to be used : specified as first in args or default
* one
*/
private static File getInputURL(String args[]) {
protected File getInputURL(String args[]) {
String fileURL;
if (args != null && args.length > 0) {
fileURL = args[0];
@ -98,7 +99,7 @@ public class ViewImageTest {
* @throws IllegalArgumentException
* when args[2] is not set and default is not found
*/
private static File getOuputDir(String[] args) {
protected File getOuputDir(String[] args) {
File outDir;
if (args.length > 2) {
outDir = new File(args[2]);
@ -107,7 +108,7 @@ public class ViewImageTest {
if (tmpDir == null) {
throw new IllegalArgumentException("No destination dir specified, and default not found");
}
outDir = new File(tmpDir + File.separator + ViewImageTest.class.getCanonicalName());
outDir = new File(tmpDir + File.separator + this.getClass().getCanonicalName());
}
return outDir;
}
@ -117,10 +118,12 @@ public class ViewImageTest {
*
* @param args
* main parameters : args[3] and args[4] may respectively contain
* max width and max height
* max width and max height. Set it to zero so there is no max
* width and no max height when processing. args[5] may be set to
* "quadratic" to render output images as squares.
* @return a serverObjects instance
*/
private static serverObjects makePostParams(String args[]) {
protected serverObjects makePostParams(String args[]) {
serverObjects post = new serverObjects();
if (args != null && args.length > 3) {
int maxWidth = Integer.parseInt(args[3]);
@ -132,6 +135,11 @@ public class ViewImageTest {
post.put("maxheight", String.valueOf(maxHeight));
}
boolean quadratic = isQuadratic(args);
if (quadratic) {
post.put("quadratic", "");
}
return post;
}
@ -141,7 +149,7 @@ public class ViewImageTest {
* main parameters : second item may contain extension
* @return extension to use for encoding
*/
private static String getEncodingExt(String args[]) {
protected String getEncodingExt(String args[]) {
String ext = DEFAULT_OUT_EXT;
if (args != null && args.length > 1) {
ext = args[1];
@ -150,33 +158,175 @@ public class ViewImageTest {
}
/**
* Display detailed results. All parametrers required not to be null.
*
* @param inFiles
* input image files
* @param args
* main parameters. args[5] may be set to "quadratic"
* @return true when image are supposed to be rendered as squares.
*/
protected boolean isQuadratic(String args[]) {
boolean recursive = false;
if (args != null && args.length > 5) {
recursive = "quadratic".equals(args[5]);
}
return recursive;
}
/**
*
* @param args
* main parameters. args[6] may be set to "recursive"
* @return true when folders are supposed to processed recursively
*/
protected boolean isRecursive(String args[]) {
boolean recursive = false;
if (args != null && args.length > 6) {
recursive = "recursive".equals(args[6]);
}
return recursive;
}
/**
* Write same message to both system standard output and to outWriter.
*
* @param message
* message to write
* @param outWriter
* PrintWriter writer. Must not be null.
* @throws IOException
* in case of write error
*/
protected void writeMessage(String message, PrintWriter outWriter) throws IOException {
System.out.println(message);
outWriter.println(message);
}
/**
* Display detailed results and produce a results.txt file in outDir. All
* parametrers required not to be null.
*
* @param processedFiles
* all processed image files
* @param failures
* map input file url which failed with eventual cause exception
* @param time
* total processing time in nanoseconds
* @param outDir
* directory to write results file
* @throws IOException
* when a write error occured writing the results file
*/
private static void displayResults(File[] inFiles, Map<String, Exception> failures) {
if (failures.size() > 0) {
if (failures.size() == inFiles.length) {
System.out.println("No input files could be processed :");
protected void displayResults(List<File> processedFiles, Map<String, Exception> failures, long time, File outDir)
throws IOException {
PrintWriter resultsWriter = new PrintWriter(new FileWriter(new File(outDir, "results.txt")));
try {
writeMessage(processedFiles.size() + " files processed in " + (time / 1000000) + " ms", resultsWriter);
if (failures.size() > 0) {
if (failures.size() == processedFiles.size()) {
writeMessage("No input files could be processed :", resultsWriter);
} else {
writeMessage("Some input files could not be processed :", resultsWriter);
}
for (Entry<String, Exception> entry : failures.entrySet()) {
writeMessage(entry.getKey(), resultsWriter);
if (entry.getValue() != null) {
writeMessage("cause : " + entry.getValue(), resultsWriter);
}
}
} else {
System.out.println("Some input files could not be processed :");
}
for (Entry<String, Exception> entry : failures.entrySet()) {
System.out.println(entry.getKey());
if (entry.getValue() != null) {
System.out.println("cause : " + entry.getValue());
if (processedFiles.size() > 0) {
writeMessage("All input files were successfully processed.", resultsWriter);
} else {
writeMessage("No input file was provided.", resultsWriter);
}
}
} else {
System.out.println("All input files were successfully processed.");
} finally {
resultsWriter.close();
}
}
/**
* Test image(s) (default : JPEG_example_JPG_RIP_100.jpg) are parsed and
* Process inFiles and update processedFiles list and failures map. All
* parameters must not be null.
*
* @param ext
* output encoding image format
* @param recursive
* when true, also process inFiles directories
* @param outDir
* output directory
* @param post
* ViewImage post parameters
* @param inFiles
* files or directories to process
* @param processedFiles
* list of processed files
* @param failures
* map failed file urls to eventual exception
* @throws IOException
* when an read/write error occured
*/
protected void processFiles(String ext, boolean recursive, File outDir, serverObjects post, File[] inFiles,
List<File> processedFiles, Map<String, Exception> failures) throws IOException {
for (File inFile : inFiles) {
if (inFile.isDirectory()) {
if (recursive) {
File subDir = new File(outDir, inFile.getName());
subDir.mkdirs();
processFiles(ext, recursive, subDir, post, inFile.listFiles(), processedFiles, failures);
}
} else {
processedFiles.add(inFile);
processFile(ext, outDir, post, failures, inFile);
}
}
}
/**
* Process inFile image and update processedFiles list and failures map. All
* parameters must not be null.
* @param ext output encoding image format
* @param outDir output directory
* @param post ViewImage post parameters
* @param failures map failed file urls to eventual exception
* @param inFile file image to process
* @throws IOException when an read/write error occured
*/
protected void processFile(String ext, File outDir, serverObjects post, Map<String, Exception> failures, File inFile)
throws IOException {
/* Delete eventual previous result file */
File outFile = new File(outDir, inFile.getName() + "." + ext);
if (outFile.exists()) {
outFile.delete();
}
byte[] resourceb = getBytes(inFile);
String urlString = inFile.getAbsolutePath();
EncodedImage img = null;
Exception error = null;
try {
img = ViewImage.parseAndScale(post, true, urlString, ext, false, resourceb);
} catch (Exception e) {
error = e;
}
if (img == null) {
failures.put(urlString, error);
} else {
FileOutputStream outFileStream = null;
try {
outFileStream = new FileOutputStream(outFile);
img.getImage().writeTo(outFileStream);
} finally {
if (outFileStream != null) {
outFileStream.close();
}
img.getImage().close();
}
}
}
/**
* Test image(s) (default : classpath resource folder /viewImageTest/test/) are parsed and
* rendered to an output foler. Result can then be checked with program of
* your choice.
*
@ -184,80 +334,61 @@ public class ViewImageTest {
* may be empty or contain parameters to override defaults :
* <ul>
* <li>args[0] : input image file URL or folder containing image
* files URL. Default :
* viewImageTest/test/JPEG_example_JPG_RIP_100.jpg</li>
* files URL. Default : classpath resource
* /viewImageTest/test/</li>
* <li>args[1] : output format name (for example : "jpg") for
* rendered image</li>
* <li>args[2] : ouput folder URL</li>
* <li>args[3] : max width (in pixels) for rendered image.
* Default : no value.</li>
* <li>args[4] : max height (in pixels) for rendered image.
* Default : no value.</li>
* rendered image. Defaut : "png".</li>
* <li>args[2] : ouput folder URL. Default :
* "[system tmp dir]/ViewImageTest".</li>
* <li>args[3] : max width (in pixels) for rendered image. May be
* set to zero to specify no max width. Default : no value.</li>
* <li>args[4] : max height (in pixels) for rendered image. May
* be set to zero to specify no max height. Default : no value.
* </li>
* <li>args[5] : set to "quadratic" to render square output
* image. May be set to any string to specify no quadratic shape.
* Default : false.</li>
* <li>args[6] : set to "recursive" to process recursively sub
* folders. Default : false.</li>
* </ul>
* @throws IOException
* when a read/write error occured
*/
public static void main(String args[]) throws IOException {
File inURL = getInputURL(args);
String ext = getEncodingExt(args);
File outDir = getOuputDir(args);
serverObjects post = makePostParams(args);
ViewImageTest test = new ViewImageTest();
File inFile = test.getInputURL(args);
String ext = test.getEncodingExt(args);
File outDir = test.getOuputDir(args);
boolean recursive = test.isRecursive(args);
serverObjects post = test.makePostParams(args);
outDir.mkdirs();
File[] inFiles;
if (inURL.isFile()) {
if (inFile.isFile()) {
inFiles = new File[1];
inFiles[0] = inURL;
System.out.println("Testing ViewImage rendering with input file : " + inURL.getAbsolutePath()
inFiles[0] = inFile;
System.out.println("Testing ViewImage rendering with input file : " + inFile.getAbsolutePath()
+ " encoded To : " + ext);
} else if (inURL.isDirectory()) {
FileFilter filter = FileFileFilter.FILE;
inFiles = inURL.listFiles(filter);
System.out.println("Testing ViewImage rendering with input files in folder : " + inURL.getAbsolutePath()
} else if (inFile.isDirectory()) {
inFiles = inFile.listFiles();
System.out.println("Testing ViewImage rendering with input files in folder : " + inFile.getAbsolutePath()
+ " encoded To : " + ext);
} else {
inFiles = new File[0];
}
if (inFiles.length == 0) {
throw new IllegalArgumentException(inURL.getAbsolutePath() + " is not a valid file or folder url.");
throw new IllegalArgumentException(inFile.getAbsolutePath() + " is not a valid file or folder url.");
}
System.out.println("Rendered images will be written in dir : " + outDir.getAbsolutePath());
Map<String, Exception> failures = new HashMap<String, Exception>();
List<File> processedFiles = new ArrayList<File>();
Map<String, Exception> failures = new TreeMap<>();
try {
for (File inFile : inFiles) {
/* Delete eventual previous result file */
File outFile = new File(outDir, inFile.getName() + "." + ext);
if (outFile.exists()) {
outFile.delete();
}
byte[] resourceb = getBytes(inFile);
String urlString = inFile.getAbsolutePath();
EncodedImage img = null;
Exception error = null;
try {
img = ViewImage.parseAndScale(post, true, urlString, ext, false, resourceb);
} catch (Exception e) {
error = e;
}
if (img == null) {
failures.put(urlString, error);
} else {
FileOutputStream outFileStream = null;
try {
outFileStream = new FileOutputStream(outFile);
img.getImage().writeTo(outFileStream);
} finally {
if (outFileStream != null) {
outFileStream.close();
}
img.getImage().close();
}
}
}
displayResults(inFiles, failures);
long time = System.nanoTime();
test.processFiles(ext, recursive, outDir, post, inFiles, processedFiles, failures);
time = System.nanoTime() - time;
test.displayResults(processedFiles, failures, time, outDir);
} finally {
ConcurrentLog.shutdown();
}