1 package org.apache.jcs.auxiliary.disk.block;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import junit.framework.TestCase;
23
24 /***
25 * Simple tests for the element descriptor
26 * <p>
27 * @author Aaron Smuts
28 */
29 public class BlockDiskElementDescriptorUnitTest
30 extends TestCase
31 {
32
33 /***
34 * Verify that the memory used per element is reasonable.
35 * <p>
36 * TODO figure out a more precise expectation.
37 * <p>
38 * @throws Exception
39 */
40 public void testMemorySize()
41 throws Exception
42 {
43
44 long memoryBefore = measureMemoryUse();
45 System.out.println( "Before: " + memoryBefore );
46
47 int numElements = 25000;
48 BlockDiskElementDescriptor[] elements = new BlockDiskElementDescriptor[numElements];
49
50 long memoryStart = measureMemoryUse();
51 System.out.println( "Start: " + memoryStart );
52
53
54 for ( int i = 0; i < numElements; i++ )
55 {
56 BlockDiskElementDescriptor descriptor = new BlockDiskElementDescriptor();
57 descriptor.setKey( new Integer( i ) );
58 descriptor.setBlocks( new int[] { 1, 2 } );
59 elements[i] = descriptor;
60 }
61
62
63 long memoryEnd = measureMemoryUse();
64 System.out.println( "End: " + memoryEnd );
65
66 long diff = memoryEnd - memoryStart;
67 System.out.println( "diff: " + diff );
68
69 long perDiff = diff / numElements;
70 System.out.println( "per diff: " + perDiff );
71
72
73 assertTrue( "Too much was used.", perDiff < 75 );
74 }
75
76 /***
77 * Measure memory used by the VM.
78 * @return
79 * @throws InterruptedException
80 */
81 protected long measureMemoryUse()
82 throws InterruptedException
83 {
84 System.gc();
85 Thread.sleep( 3000 );
86 System.gc();
87 return Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
88 }
89 }