📄 mimemediatypetest.java
字号:
for(int each = 0; each < trials; each++) { strings[each] = new String("text/xml; charset=\"UTF-XX" + each + "\""); } System.runFinalization(); System.gc(); System.gc(); start = System.currentTimeMillis(); for(int each = 0; each < trials; each++) { mimes[each] = new MimeMediaType(strings[each]); } stop = System.currentTimeMillis(); System.out.println( stop - start + "ms for " + trials + " unique types."); System.runFinalization(); System.gc(); System.gc(); start = System.currentTimeMillis(); for(int each = 0; each < trials; each++) { mimes[each] = new MimeMediaType(strings[each]).intern(); } stop = System.currentTimeMillis(); System.out.println( stop - start + "ms for " + trials + " unique interned types."); Arrays.fill(strings, null); Arrays.fill(mimes, null); for(int each = 0; each < trials; each++) { strings[each] = new String("text/xml; charset=\"UTF-XX" + (each % 100) + "\""); } System.runFinalization(); System.gc(); System.gc(); start = System.currentTimeMillis(); for(int each = 0; each < trials; each++) { mimes[each] = new MimeMediaType(strings[each]); } stop = System.currentTimeMillis(); System.out.println( stop - start + "ms for " + trials + " non-unique types."); System.runFinalization(); System.gc(); System.gc(); start = System.currentTimeMillis(); for(int each = 0; each < trials; each++) { mimes[each] = new MimeMediaType(strings[each]).intern(); } stop = System.currentTimeMillis(); System.out.println( stop - start + "ms for " + trials + " non-unique interned types."); } public void testInternContention() { final int concurrency = 50; final int trials = 50000; long stop; ExecutorService executor = Executors.newCachedThreadPool(); // Pre-create threads. for(int spawn = 0; spawn < concurrency; spawn++) { executor.execute( new Runnable() { public void run() { try { Thread.sleep(500); } catch(InterruptedException woken) { } } }); } try { Thread.sleep(500); } catch(InterruptedException woken) { } System.runFinalization(); System.gc(); System.gc(); final long start = System.currentTimeMillis(); for(int each = 0; each < concurrency; each++) { final String runcount = "Run #" + each; executor.execute( new Runnable() { public void run() { System.err.println( runcount + " start " + (System.currentTimeMillis() - start)); MimeMediaType [] mimes = new MimeMediaType[trials]; for(int each = 0; each < trials; each++) { mimes[each] = new MimeMediaType(new String("text/xml")).intern(); } System.err.println( runcount + " done " + (System.currentTimeMillis() - start)); } }); } executor.shutdown(); try { executor.awaitTermination(60, TimeUnit.SECONDS); } catch(InterruptedException woken) { } stop = System.currentTimeMillis(); System.err.flush(); System.out.flush(); System.out.println( stop - start + "ms for " + trials + " identical types on " + concurrency + " threads."); } public void testInternUniquesContention() { final int concurrency = 50; final int trials = 10000; long stop; ExecutorService executor = Executors.newCachedThreadPool(); // Pre-create threads. for(int spawn = 0; spawn < concurrency; spawn++) { executor.execute( new Runnable() { public void run() { try { Thread.sleep(500); } catch(InterruptedException woken) { } } }); } try { Thread.sleep(500); } catch(InterruptedException woken) { } System.runFinalization(); System.gc(); System.gc(); final long start = System.currentTimeMillis(); for(int each = 0; each < concurrency; each++) { final String runcount = "Run #" + each; executor.execute( new Runnable() { public void run() { System.err.println( runcount + " start " + (System.currentTimeMillis() - start)); MimeMediaType [] mimes = new MimeMediaType[trials]; for(int each = 0; each < trials; each++) { mimes[each] = new MimeMediaType(new String("text/xml; charset=\"" + runcount + each + "\"" )).intern(); } System.err.println( runcount + " done " + (System.currentTimeMillis() - start)); } }); } executor.shutdown(); try { executor.awaitTermination(60, TimeUnit.SECONDS); } catch(InterruptedException woken) { } stop = System.currentTimeMillis(); System.err.flush(); System.out.flush(); System.out.println( stop - start + "ms for " + trials + " unique types on " + concurrency + " threads."); } public void testSerialization() { MimeMediaType one = new MimeMediaType("text/xml"); try { ByteArrayOutputStream bos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(bos); oos.writeObject(one); oos.close(); bos.close(); ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray()); ObjectInputStream ois = new ObjectInputStream(bis); MimeMediaType two = (MimeMediaType) ois.readObject(); assertTrue("MimeMediaTypes should have been equal()", one.equals(two)); } catch (Throwable failure) { fail("Exception during test! " + failure.getMessage()); } } public static Test suite() { TestSuite suite = new TestSuite(MimeMediaTypeTest.class); return suite; } public static void main(java.lang.String[] args) { junit.textui.TestRunner.run(suite()); System.err.flush(); System.out.flush(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -