You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

FractionTest.java 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /*
  2. * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
  3. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4. *
  5. * This code is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License version 2 only, as
  7. * published by the Free Software Foundation.
  8. *
  9. * This code is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  12. * version 2 for more details (a copy is included in the LICENSE file that
  13. * accompanied this code).
  14. *
  15. * You should have received a copy of the GNU General Public License version
  16. * 2 along with this work; if not, write to the Free Software Foundation,
  17. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18. *
  19. * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20. * or visit www.oracle.com if you need additional information or have any
  21. * questions.
  22. *
  23. */
  24. package com.github.dcevm.test.eval;
  25. import com.github.dcevm.HotSwapTool;
  26. import org.junit.Before;
  27. import org.junit.Test;
  28. import java.util.ArrayList;
  29. import java.util.List;
  30. import static com.github.dcevm.test.util.HotSwapTestHelper.__toVersion__;
  31. import static com.github.dcevm.test.util.HotSwapTestHelper.__version__;
  32. /**
  33. * @author Thomas Wuerthinger
  34. */
  35. public class FractionTest {
  36. @Before
  37. public void setUp() throws Exception {
  38. __toVersion__(0);
  39. assert __version__() == 0;
  40. }
  41. // Version 0
  42. public static class NoChange {
  43. int i1;
  44. int i2;
  45. int i3;
  46. Object o1;
  47. Object o2;
  48. Object o3;
  49. }
  50. public static class Change {
  51. int i1;
  52. int i2;
  53. int i3;
  54. Object o1;
  55. Object o2;
  56. Object o3;
  57. }
  58. // Version 1
  59. public static class Change___1 {
  60. int i1;
  61. int i2;
  62. int i3;
  63. Object o1;
  64. Object o2;
  65. Object o3;
  66. Object o4;
  67. }
  68. // Version 2
  69. public static class Change___2 {
  70. int i1;
  71. int i2;
  72. int i3;
  73. Object o1;
  74. }
  75. // Version 3
  76. public static class Change___3 {
  77. int i3;
  78. int i1;
  79. int i2;
  80. Object o3;
  81. Object o1;
  82. Object o2;
  83. }
  84. // Version 3
  85. public static class Change___4 {
  86. int i1;
  87. int i2;
  88. int i3;
  89. Object o1;
  90. Object o2;
  91. Object o3;
  92. }
  93. // Version 2
  94. public static class Change___5 {
  95. }
  96. private static List<Long> measurements = new ArrayList<Long>();
  97. private final int BASE = 10;
  98. private Object[] objects;
  99. private void clear() {
  100. objects = null;
  101. System.gc();
  102. System.gc();
  103. __toVersion__(0);
  104. System.gc();
  105. System.gc();
  106. }
  107. private void init(int count, int percent) {
  108. objects = new Object[count];
  109. int changed = 0;
  110. int unchanged = 0;
  111. for (int k = 0; k < count; k++) {
  112. if ((count / BASE) * percent <= k/* && k >= 200000*/) {
  113. objects[k] = new NoChange();
  114. unchanged++;
  115. } else {
  116. objects[k] = new Change();
  117. changed++;
  118. }
  119. }
  120. System.gc();
  121. System.out.println(changed + " changed objects allocated");
  122. }
  123. @Test
  124. public void testBase() {
  125. assert __version__() == 0;
  126. final int N = 1;
  127. final int INC = 4;
  128. final int SIZE = 4000;
  129. int[] benchmarking = new int[]{SIZE};
  130. int base = BASE;
  131. int start = 0;
  132. MicroBenchmark[] benchmarks = new MicroBenchmark[]{new GCMicroBenchmark(), new IncreaseMicroBenchmark(), new DecreaseMicroBenchmark(), new ReorderMicroBenchmark(), new NoRealChangeMicroBenchmark(), new BigDecreaseMicroBenchmark()};
  133. clear();
  134. for (int k = 0; k < N; k++) {
  135. for (MicroBenchmark m : benchmarks) {
  136. for (int i : benchmarking) {
  137. System.out.println(m.getClass().getName() + " with " + i + " objects");
  138. for (int j = start; j <= base; j += INC) {
  139. System.out.println(j);
  140. m.init(i);
  141. init(i, j);
  142. m.doit(i, measurements);
  143. clear();
  144. }
  145. }
  146. }
  147. }
  148. System.out.println("Results:");
  149. for (long l : measurements) {
  150. System.out.println(l);
  151. }
  152. measurements.clear();
  153. }
  154. }
  155. abstract class MicroBenchmark {
  156. public void init(int count) {
  157. }
  158. public abstract void doit(int count, List<Long> measurements);
  159. }
  160. class GCMicroBenchmark extends MicroBenchmark {
  161. @Override
  162. public void doit(int count, List<Long> measurements) {
  163. long startTime = System.currentTimeMillis();
  164. System.gc();
  165. long curTime = System.currentTimeMillis() - startTime;
  166. measurements.add(curTime);
  167. }
  168. }
  169. class IncreaseMicroBenchmark extends MicroBenchmark {
  170. @Override
  171. public void doit(int count, List<Long> measurements) {
  172. HotSwapTool.resetTimings();
  173. __toVersion__(1);
  174. measurements.add(HotSwapTool.getTotalTime());
  175. }
  176. }
  177. class DecreaseMicroBenchmark extends MicroBenchmark {
  178. @Override
  179. public void doit(int count, List<Long> measurements) {
  180. HotSwapTool.resetTimings();
  181. __toVersion__(2);
  182. measurements.add(HotSwapTool.getTotalTime());
  183. }
  184. }
  185. class ReorderMicroBenchmark extends MicroBenchmark {
  186. @Override
  187. public void doit(int count, List<Long> measurements) {
  188. HotSwapTool.resetTimings();
  189. __toVersion__(3);
  190. measurements.add(HotSwapTool.getTotalTime());
  191. }
  192. }
  193. class NoRealChangeMicroBenchmark extends MicroBenchmark {
  194. @Override
  195. public void doit(int count, List<Long> measurements) {
  196. HotSwapTool.resetTimings();
  197. __toVersion__(4);
  198. measurements.add(HotSwapTool.getTotalTime());
  199. }
  200. }
  201. class BigDecreaseMicroBenchmark extends MicroBenchmark {
  202. @Override
  203. public void doit(int count, List<Long> measurements) {
  204. HotSwapTool.resetTimings();
  205. __toVersion__(5);
  206. measurements.add(HotSwapTool.getTotalTime());
  207. }
  208. }