Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.13 KB | None | 0 0
  1. import com.google.common.collect.ImmutableSet;
  2. import com.google.common.math.StatsAccumulator;
  3. import java.awt.Graphics2D;
  4. import java.awt.Image;
  5. import java.awt.image.BufferedImage;
  6. import java.io.IOException;
  7. import java.io.InputStream;
  8. import java.net.URL;
  9. import java.util.Set;
  10. import javax.imageio.ImageIO;
  11. import javax.imageio.ImageReadParam;
  12. import javax.imageio.ImageReader;
  13. import javax.imageio.stream.ImageInputStream;
  14. import javax.swing.Icon;
  15. import javax.swing.ImageIcon;
  16.  
  17. // Note: Before running this benchmark, be sure to disable Intel SpeedStep in the BIOS.
  18. public class SVGBenchmark {
  19. private static final Set<String> ICON_NAMES = ImmutableSet.of(
  20. "warning", "arrow_down", "arrow_up", "cursoredit_data", "cursoredit_formula",
  21. "cursoredit_label", "cursoredit_none", "database", "field_collapsed_join",
  22. "field_new_primitive_data", "field_new_primitive_formula", "field_new_relation",
  23. "field_primitive_data", "field_primitive_formula", "field_relation_noinst", "field_relation",
  24. "fields_relation_only", "filter_range", "folder_closed", "folder_open", "general_minus",
  25. "general_plus", "project_perspective", "project_table", "project_logo", "toolbar_edit",
  26. "toolbar_redo", "toolbar_refresh", "toolbar_stop", "toolbar_undo", "toolbar_zoom_in",
  27. "toolbar_zoom_original", "toolbar_zoom_out");
  28.  
  29. private static final ImageReader PNG_READER;
  30.  
  31. static {
  32. ImageIO.setUseCache(false);
  33. PNG_READER = ImageIO.getImageReadersByMIMEType("image/png").next();
  34. }
  35.  
  36. private static URL getResourceURL(String iconName, boolean svg) {
  37. String path = "com/somepackage/project/icons/" + iconName + "." + (svg ? "svg" : "png");
  38. URL ret = SVGBenchmark.class.getClassLoader().getResource(path);
  39. if (ret == null)
  40. throw new RuntimeException("Resource not found");
  41. return ret;
  42. }
  43.  
  44. private static void loadAndPaintOneIcon(String iconName, boolean svg) {
  45. final URL url = getResourceURL(iconName, svg);
  46. final Icon icon;
  47. if (svg) {
  48. try {
  49. icon = SVGIcon.load(url);
  50. } catch (IOException e) {
  51. throw new RuntimeException(e);
  52. }
  53. } else {
  54. Image image;
  55. // Load in the same way as ImageUtilities opens PNG files.
  56. try (ImageInputStream stream = ImageIO.createImageInputStream(url.openStream())) {
  57. ImageReadParam param = PNG_READER.getDefaultReadParam();
  58. PNG_READER.setInput(stream, true, true);
  59. image = PNG_READER.read(0, param);
  60. } catch (IOException e) {
  61. throw new RuntimeException(e);
  62. }
  63. icon = new ImageIcon(image);
  64. }
  65. BufferedImage img = new BufferedImage(
  66. icon.getIconWidth(), icon.getIconHeight(), BufferedImage.TYPE_INT_ARGB_PRE);
  67. Graphics2D g = img.createGraphics();
  68. // Assume the Component parameter isn't used by the underlying implementation.
  69. icon.paintIcon(null, g, 0, 0);
  70. g.dispose();
  71. }
  72.  
  73. private static void measureOneSample(StatsAccumulator svgStats, StatsAccumulator pngStats) {
  74. boolean order[] = Math.random() < 0.5
  75. ? new boolean[] {false, true}
  76. : new boolean[] {true, false};
  77. //boolean order[] = new boolean[] {true};
  78. for (boolean svg : order) {
  79. long bef = System.nanoTime();
  80. for (String iconName : ICON_NAMES)
  81. loadAndPaintOneIcon(iconName, svg);
  82. long aft = System.nanoTime();
  83. (svg ? svgStats : pngStats).add(aft - bef);
  84. }
  85. }
  86. private static void printStats(StatsAccumulator stats, boolean svg) {
  87. double mean = (stats.count() == 0) ? Double.NaN : stats.mean();
  88. double standardError = stats.count() < 2 ? Double.NaN :
  89. (stats.sampleStandardDeviation() / Math.sqrt(stats.count()));
  90. double standardErrorPercentage = 100 * (standardError / mean);
  91. double meanPerIcon = mean / ICON_NAMES.size();
  92. System.out.printf("Loading and painting %s took %.2f microseconds per icon +/- %.02f%%\n",
  93. svg ? "SVG" : "PNG", (meanPerIcon / 1000.0), standardErrorPercentage);
  94. }
  95.  
  96. private static void measure() {
  97. StatsAccumulator svgStats = new StatsAccumulator();
  98. StatsAccumulator pngStats = new StatsAccumulator();
  99. // Warmup.
  100. for (int i = 0; i < 300; i++)
  101. measureOneSample(svgStats, pngStats);
  102. // Throw away the warmup stats and start fresh.
  103. svgStats = new StatsAccumulator();
  104. pngStats = new StatsAccumulator();
  105. // Now do the actual measurements.
  106. for (int i = 0; i < 500; i++)
  107. measureOneSample(svgStats, pngStats);
  108. printStats(svgStats, true);
  109. printStats(pngStats, false);
  110. }
  111.  
  112. private static final void printFileSizeStats(boolean svg) {
  113. StatsAccumulator sizeStats = new StatsAccumulator();
  114. for (String iconName : ICON_NAMES) {
  115. URL url = getResourceURL(iconName, svg);
  116. try (InputStream is = url.openStream()) {
  117. sizeStats.add(is.readAllBytes().length);
  118. } catch (IOException e) {
  119. throw new RuntimeException(e);
  120. }
  121. }
  122. System.out.println("Average size of " + (svg ? "SVG" : "PNG") + " icon file is " +
  123. Math.round(sizeStats.mean()) + " bytes (max was " + (int) sizeStats.max() + " bytes)");
  124. }
  125.  
  126. public static final void main(String args[]) {
  127. printFileSizeStats(true);
  128. printFileSizeStats(false);
  129. measure();
  130. }
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement