1 package org.apache.commons.jcs.access;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.BufferedReader;
23 import java.io.IOException;
24 import java.io.InputStreamReader;
25 import java.util.Iterator;
26 import java.util.Map;
27 import java.util.Random;
28 import java.util.StringTokenizer;
29
30 import org.apache.commons.jcs.JCS;
31 import org.apache.commons.jcs.access.exception.CacheException;
32 import org.apache.commons.jcs.engine.ElementAttributes;
33 import org.apache.commons.jcs.engine.behavior.IElementAttributes;
34 import org.apache.commons.jcs.engine.control.CompositeCacheManager;
35 import org.apache.commons.jcs.engine.control.event.ElementEventHandlerMockImpl;
36 import org.apache.commons.logging.Log;
37 import org.apache.commons.logging.LogFactory;
38
39
40
41
42
43 public class TestCacheAccess
44 {
45
46 private static final Log log = LogFactory.getLog( TestCacheAccess.class );
47
48
49 private CacheAccess<String, String> cache_control = null;
50
51
52 private GroupCacheAccess<String, String> group_cache_control = null;
53
54
55 private static boolean isSysOut = false;
56
57
58 public TestCacheAccess()
59 {
60 this( "testCache1" );
61 }
62
63
64
65
66 public TestCacheAccess( String regionName )
67 {
68 try
69 {
70 cache_control = JCS.getInstance( regionName );
71 group_cache_control = JCS.getGroupCacheInstance( regionName );
72 }
73 catch ( Exception e )
74 {
75 log.error( "Problem getting cache instance", e );
76 p( e.toString() );
77 }
78 }
79
80
81
82
83 public void runLoop()
84 {
85 try
86 {
87
88 boolean notDone = true;
89 String message = null;
90
91 BufferedReader br = new BufferedReader( new InputStreamReader( System.in ) );
92
93 help();
94
95 while ( notDone )
96 {
97 p( "enter command:" );
98
99 message = br.readLine();
100
101 if ( message == null || message.startsWith( "help" ) )
102 {
103 help();
104 }
105 else if ( message.startsWith( "gc" ) )
106 {
107 System.gc();
108 }
109 else if ( message.startsWith( "getAttributeNames" ) )
110 {
111 long n_start = System.currentTimeMillis();
112 String groupName = null;
113 StringTokenizer toke = new StringTokenizer( message );
114 int tcnt = 0;
115 while ( toke.hasMoreElements() )
116 {
117 tcnt++;
118 String t = (String) toke.nextElement();
119 if ( tcnt == 2 )
120 {
121 groupName = t.trim();
122 }
123 }
124 getAttributeNames( groupName );
125 long n_end = System.currentTimeMillis();
126 p( "---got attrNames for " + groupName + " in " + String.valueOf( n_end - n_start ) + " millis ---" );
127 }
128 else if ( message.startsWith( "shutDown" ) )
129 {
130 CompositeCacheManager.getInstance().shutDown();
131
132 notDone = false;
133
134 return;
135 }
136
137
138 else if ( message.startsWith( "getm" ) )
139 {
140 processGetMultiple( message );
141 }
142 else if ( message.startsWith( "getg" ) )
143 {
144 processGetGroup( message );
145 }
146 else if ( message.startsWith( "getag" ) )
147 {
148 processGetAutoGroup( message );
149 }
150 else if ( message.startsWith( "getMatching" ) )
151 {
152 processGetMatching( message );
153 }
154 else if ( message.startsWith( "get" ) )
155 {
156 processGet( message );
157 }
158 else if ( message.startsWith( "putg" ) )
159 {
160 processPutGroup( message );
161 }
162
163 else if ( message.startsWith( "putag" ) )
164 {
165 processPutAutoGroup( message );
166 }
167 else if ( message.startsWith( "putm" ) )
168 {
169 String numS = message.substring( message.indexOf( " " ) + 1, message.length() );
170 if ( numS == null )
171 {
172 p( "usage: putm numbertoput" );
173 }
174 else
175 {
176 int num = Integer.parseInt( numS.trim() );
177 putMultiple( num );
178 }
179 }
180 else if ( message.startsWith( "pute" ) )
181 {
182 String numS = message.substring( message.indexOf( " " ) + 1, message.length() );
183 if ( numS == null )
184 {
185 p( "usage: putme numbertoput" );
186 }
187 else
188 {
189 int num = Integer.parseInt( numS.trim() );
190 long n_start = System.currentTimeMillis();
191 for ( int n = 0; n < num; n++ )
192 {
193 IElementAttributes attrp = cache_control.getDefaultElementAttributes();
194 ElementEventHandlerMockImpl hand = new ElementEventHandlerMockImpl();
195 attrp.addElementEventHandler( hand );
196 cache_control.put( "key" + n, "data" + n + " put from ta = junk", attrp );
197 }
198 long n_end = System.currentTimeMillis();
199 p( "---put " + num + " in " + String.valueOf( n_end - n_start ) + " millis ---" );
200 }
201 }
202 else if ( message.startsWith( "put" ) )
203 {
204 processPut( message );
205 }
206 else if ( message.startsWith( "removem" ) )
207 {
208 String numS = message.substring( message.indexOf( " " ) + 1, message.length() );
209 if ( numS == null )
210 {
211 p( "usage: removem numbertoremove" );
212 }
213 else
214 {
215 int num = Integer.parseInt( numS.trim() );
216 removeMultiple( num );
217 }
218 }
219 else if ( message.startsWith( "removeall" ) )
220 {
221 cache_control.clear();
222 p( "removed all" );
223 }
224 else if ( message.startsWith( "remove" ) )
225 {
226 String key = message.substring( message.indexOf( " " ) + 1, message.length() );
227 cache_control.remove( key );
228 p( "removed " + key );
229 }
230 else if ( message.startsWith( "deattr" ) )
231 {
232 IElementAttributes ae = cache_control.getDefaultElementAttributes();
233 p( "Default IElementAttributes " + ae );
234 }
235 else if ( message.startsWith( "cloneattr" ) )
236 {
237 String numS = message.substring( message.indexOf( " " ) + 1, message.length() );
238 if ( numS == null )
239 {
240 p( "usage: put numbertoput" );
241 }
242 else
243 {
244 int num = Integer.parseInt( numS.trim() );
245 IElementAttributes attrp = new ElementAttributes();
246 long n_start = System.currentTimeMillis();
247 for ( int n = 0; n < num; n++ )
248 {
249 attrp.clone();
250 }
251 long n_end = System.currentTimeMillis();
252 p( "---cloned attr " + num + " in " + String.valueOf( n_end - n_start ) + " millis ---" );
253 }
254 }
255 else if ( message.startsWith( "switch" ) )
256 {
257 String name = message.substring( message.indexOf( " " ) + 1, message.length() );
258
259 setRegion( name );
260 p( "switched to cache = " + name );
261 p( cache_control.toString() );
262 }
263 else if ( message.startsWith( "stats" ) )
264 {
265 p( cache_control.getStats() );
266 }
267 else if ( message.startsWith( "gc" ) )
268 {
269 System.gc();
270 p( "Called system.gc()" );
271 }
272 else if ( message.startsWith( "random" ) )
273 {
274 processRandom( message );
275 }
276 }
277 }
278 catch ( CacheException e )
279 {
280 p( e.toString() );
281 e.printStackTrace( System.out );
282 }
283 catch (IOException e)
284 {
285 p( e.toString() );
286 e.printStackTrace( System.out );
287 }
288 }
289
290
291
292
293 private void processGetMultiple( String message )
294 {
295 int num = 0;
296 boolean show = true;
297
298 StringTokenizer toke = new StringTokenizer( message );
299 int tcnt = 0;
300 while ( toke.hasMoreElements() )
301 {
302 tcnt++;
303 String t = (String) toke.nextElement();
304 if ( tcnt == 2 )
305 {
306 try
307 {
308 num = Integer.parseInt( t.trim() );
309 }
310 catch ( NumberFormatException nfe )
311 {
312 p( t + "not a number" );
313 }
314 }
315 else if ( tcnt == 3 )
316 {
317 show = Boolean.valueOf( t ).booleanValue();
318 }
319 }
320
321 if ( tcnt < 2 )
322 {
323 p( "usage: get numbertoget show values[true|false]" );
324 }
325 else
326 {
327 getMultiple( num, show );
328 }
329 }
330
331
332
333
334 private void processGetGroup( String message )
335 {
336 String key = null;
337 String group = null;
338 boolean show = true;
339
340 StringTokenizer toke = new StringTokenizer( message );
341 int tcnt = 0;
342 while ( toke.hasMoreElements() )
343 {
344 tcnt++;
345 String t = (String) toke.nextElement();
346 if ( tcnt == 2 )
347 {
348 key = t.trim();
349 }
350 else if ( tcnt == 3 )
351 {
352 group = t.trim();
353 }
354 else if ( tcnt == 4 )
355 {
356 show = Boolean.valueOf( t ).booleanValue();
357 }
358 }
359
360 if ( tcnt < 2 )
361 {
362 p( "usage: get key show values[true|false]" );
363 }
364 else
365 {
366 long n_start = System.currentTimeMillis();
367 try
368 {
369 Object obj = group_cache_control.getFromGroup( key, group );
370 if ( show && obj != null )
371 {
372 p( obj.toString() );
373 }
374 }
375 catch ( Exception e )
376 {
377 log.error( e );
378 }
379 long n_end = System.currentTimeMillis();
380 p( "---got " + key + " from group " + group + " in " + String.valueOf( n_end - n_start ) + " millis ---" );
381 }
382 }
383
384
385
386
387 private void processGetAutoGroup( String message )
388 {
389
390
391 int num = 0;
392 String group = null;
393 boolean show = true;
394
395 StringTokenizer toke = new StringTokenizer( message );
396 int tcnt = 0;
397 while ( toke.hasMoreElements() )
398 {
399 tcnt++;
400 String t = (String) toke.nextElement();
401 if ( tcnt == 2 )
402 {
403 num = Integer.parseInt( t.trim() );
404 }
405 else if ( tcnt == 3 )
406 {
407 group = t.trim();
408 }
409 else if ( tcnt == 4 )
410 {
411 show = Boolean.valueOf( t ).booleanValue();
412 }
413 }
414
415 if ( tcnt < 2 )
416 {
417 p( "usage: get key show values[true|false]" );
418 }
419 else
420 {
421 long n_start = System.currentTimeMillis();
422 try
423 {
424 for ( int a = 0; a < num; a++ )
425 {
426 Object obj = group_cache_control.getFromGroup( "keygr" + a, group );
427 if ( show && obj != null )
428 {
429 p( obj.toString() );
430 }
431 }
432 }
433 catch ( Exception e )
434 {
435 log.error( e );
436 }
437 long n_end = System.currentTimeMillis();
438 p( "---got " + num + " from group " + group + " in " + String.valueOf( n_end - n_start ) + " millis ---" );
439 }
440 }
441
442
443
444
445
446 private void processPutGroup( String message )
447 throws CacheException
448 {
449 String group = null;
450 String key = null;
451 StringTokenizer toke = new StringTokenizer( message );
452 int tcnt = 0;
453 while ( toke.hasMoreElements() )
454 {
455 tcnt++;
456 String t = (String) toke.nextElement();
457 if ( tcnt == 2 )
458 {
459 key = t.trim();
460 }
461 else if ( tcnt == 3 )
462 {
463 group = t.trim();
464 }
465 }
466
467 if ( tcnt < 3 )
468 {
469 p( "usage: putg key group" );
470 }
471 else
472 {
473 long n_start = System.currentTimeMillis();
474 group_cache_control.putInGroup( key, group, "data from putg ----asdfasfas-asfasfas-asfas in group " + group );
475 long n_end = System.currentTimeMillis();
476 p( "---put " + key + " in group " + group + " in " + String.valueOf( n_end - n_start ) + " millis ---" );
477 }
478 }
479
480
481
482
483
484 private void processPutAutoGroup( String message )
485 throws CacheException
486 {
487 String group = null;
488 int num = 0;
489 StringTokenizer toke = new StringTokenizer( message );
490 int tcnt = 0;
491 while ( toke.hasMoreElements() )
492 {
493 tcnt++;
494 String t = (String) toke.nextElement();
495 if ( tcnt == 2 )
496 {
497 num = Integer.parseInt( t.trim() );
498 }
499 else if ( tcnt == 3 )
500 {
501 group = t.trim();
502 }
503 }
504
505 if ( tcnt < 3 )
506 {
507 p( "usage: putag num group" );
508 }
509 else
510 {
511 long n_start = System.currentTimeMillis();
512 for ( int a = 0; a < num; a++ )
513 {
514 group_cache_control.putInGroup( "keygr" + a, group, "data " + a
515 + " from putag ----asdfasfas-asfasfas-asfas in group " + group );
516 }
517 long n_end = System.currentTimeMillis();
518 p( "---put " + num + " in group " + group + " in " + String.valueOf( n_end - n_start ) + " millis ---" );
519 }
520 }
521
522
523
524
525
526 private void processPut( String message )
527 throws CacheException
528 {
529 String key = null;
530 String val = null;
531 StringTokenizer toke = new StringTokenizer( message );
532 int tcnt = 0;
533 while ( toke.hasMoreElements() )
534 {
535 tcnt++;
536 String t = (String) toke.nextElement();
537 if ( tcnt == 2 )
538 {
539 key = t.trim();
540 }
541 else if ( tcnt == 3 )
542 {
543 val = t.trim();
544 }
545 }
546
547 if ( tcnt < 3 )
548 {
549 p( "usage: put key val" );
550 }
551 else
552 {
553
554 long n_start = System.currentTimeMillis();
555 cache_control.put( key, val );
556 long n_end = System.currentTimeMillis();
557 p( "---put " + key + " | " + val + " in " + String.valueOf( n_end - n_start ) + " millis ---" );
558 }
559 }
560
561
562
563
564 private void processRandom( String message )
565 {
566 String rangeS = "";
567 String numOpsS = "";
568 boolean show = true;
569
570 StringTokenizer toke = new StringTokenizer( message );
571 int tcnt = 0;
572 while ( toke.hasMoreElements() )
573 {
574 tcnt++;
575 String t = (String) toke.nextElement();
576 if ( tcnt == 2 )
577 {
578 rangeS = t.trim();
579 }
580 else if ( tcnt == 3 )
581 {
582 numOpsS = t.trim();
583 }
584 else if ( tcnt == 4 )
585 {
586 show = Boolean.valueOf( t ).booleanValue();
587 }
588 }
589
590 String numS = message.substring( message.indexOf( " " ) + 1, message.length() );
591
592 int range = 0;
593 int numOps = 0;
594 try
595 {
596 range = Integer.parseInt( rangeS.trim() );
597 numOps = Integer.parseInt( numOpsS.trim() );
598 }
599 catch ( Exception e )
600 {
601 p( "usage: random range numOps show" );
602 p( "ex. random 100 1000 false" );
603 }
604 if ( numS == null )
605 {
606 p( "usage: random range numOps show" );
607 p( "ex. random 100 1000 false" );
608 }
609 else
610 {
611 random( range, numOps, show );
612 }
613 }
614
615
616
617
618 private void processGet( String message )
619 {
620
621
622 String key = null;
623 boolean show = true;
624
625 StringTokenizer toke = new StringTokenizer( message );
626 int tcnt = 0;
627 while ( toke.hasMoreElements() )
628 {
629 tcnt++;
630 String t = (String) toke.nextElement();
631 if ( tcnt == 2 )
632 {
633 key = t.trim();
634 }
635 else if ( tcnt == 3 )
636 {
637 show = Boolean.valueOf( t ).booleanValue();
638 }
639 }
640
641 if ( tcnt < 2 )
642 {
643 p( "usage: get key show values[true|false]" );
644 }
645 else
646 {
647 long n_start = System.currentTimeMillis();
648 try
649 {
650 Object obj = cache_control.get( key );
651 if ( show && obj != null )
652 {
653 p( obj.toString() );
654 }
655 }
656 catch ( Exception e )
657 {
658 log.error( e );
659 }
660 long n_end = System.currentTimeMillis();
661 p( "---got " + key + " in " + String.valueOf( n_end - n_start ) + " millis ---" );
662 }
663 }
664
665
666
667
668 private void processGetMatching( String message )
669 {
670
671
672 String pattern = null;
673 boolean show = true;
674
675 StringTokenizer toke = new StringTokenizer( message );
676 int tcnt = 0;
677 while ( toke.hasMoreElements() )
678 {
679 tcnt++;
680 String t = (String) toke.nextElement();
681 if ( tcnt == 2 )
682 {
683 pattern = t.trim();
684 }
685 else if ( tcnt == 3 )
686 {
687 show = Boolean.valueOf( t ).booleanValue();
688 }
689 }
690
691 if ( tcnt < 2 )
692 {
693 p( "usage: getMatching key show values[true|false]" );
694 }
695 else
696 {
697 long n_start = System.currentTimeMillis();
698 try
699 {
700 Map<String, String> results = cache_control.getMatching( pattern );
701 if ( show && results != null )
702 {
703 p( results.toString() );
704 }
705 }
706 catch ( Exception e )
707 {
708 log.error( e );
709 }
710 long n_end = System.currentTimeMillis();
711 p( "---gotMatching [" + pattern + "] in " + String.valueOf( n_end - n_start ) + " millis ---" );
712 }
713 }
714
715
716
717
718
719 public static void main( String[] args )
720 {
721 isSysOut = true;
722 String ccfFileName = args[0];
723 if ( ccfFileName != null )
724 {
725 JCS.setConfigFilename( ccfFileName );
726 }
727 TestCacheAccess tca = new TestCacheAccess( "testCache1" );
728 tca.runLoop();
729 }
730
731
732
733
734
735
736
737
738 public void getMultiple( int num )
739 {
740 getMultiple( num, false );
741 }
742
743
744
745
746
747 public void getMultiple( int num, boolean show )
748 {
749 long n_start = System.currentTimeMillis();
750 for ( int n = 0; n < num; n++ )
751 {
752 try
753 {
754 Object obj = cache_control.get( "key" + n );
755 if ( show && obj != null )
756 {
757 p( obj.toString() );
758 }
759 }
760 catch ( Exception e )
761 {
762 log.error( e );
763 }
764 }
765 long n_end = System.currentTimeMillis();
766 p( "---got " + num + " in " + String.valueOf( n_end - n_start ) + " millis ---" );
767 }
768
769
770
771
772
773 public void putMultiple( int num )
774 {
775 try
776 {
777 long n_start = System.currentTimeMillis();
778 for ( int n = 0; n < num; n++ )
779 {
780 cache_control.put( "key" + n, "data" + n + " put from ta = junk" );
781 }
782 long n_end = System.currentTimeMillis();
783 p( "---put " + num + " in " + String.valueOf( n_end - n_start ) + " millis ---" );
784 }
785 catch ( Exception e )
786 {
787 log.error( e );
788 }
789 }
790
791
792
793
794
795 public void removeMultiple( int num )
796 {
797 try
798 {
799 long n_start = System.currentTimeMillis();
800 for ( int n = 0; n < num; n++ )
801 {
802 cache_control.remove( "key" + n );
803 }
804 long n_end = System.currentTimeMillis();
805 p( "---removed " + num + " in " + String.valueOf( n_end - n_start ) + " millis ---" );
806 }
807 catch ( Exception e )
808 {
809 log.error( e );
810 }
811 }
812
813
814
815
816
817
818
819 public void random( int range, int numOps )
820 {
821 random( range, numOps, false );
822 }
823
824
825
826
827
828
829 public void random( int range, int numOps, boolean show )
830 {
831 try
832 {
833 for ( int i = 1; i < numOps; i++ )
834 {
835 Random ran = new Random( i );
836 int n = ran.nextInt( 4 );
837 int kn = ran.nextInt( range );
838 String key = "key" + kn;
839 if ( n == 1 )
840 {
841 cache_control.put( key, "data" + i + " junk asdfffffffadfasdfasf " + kn + ":" + n );
842 if ( show )
843 {
844 p( "put " + key );
845 }
846 }
847 else if ( n == 2 )
848 {
849 cache_control.remove( key );
850 if ( show )
851 {
852 p( "removed " + key );
853 }
854 }
855 else
856 {
857
858 Object obj = cache_control.get( key );
859 if ( show && obj != null )
860 {
861 p( obj.toString() );
862 }
863 }
864
865 if ( i % 10000 == 0 )
866 {
867 p( cache_control.getStats() );
868 }
869
870 }
871 p( "Finished random cycle of " + numOps );
872 }
873 catch ( Exception e )
874 {
875 p( e.toString() );
876 e.printStackTrace( System.out );
877 }
878 }
879
880
881
882
883
884 public void setRegion( String name )
885 {
886 try
887 {
888 cache_control = JCS.getInstance( name );
889 }
890 catch ( Exception e )
891 {
892 p( e.toString() );
893 e.printStackTrace( System.out );
894 }
895
896 }
897
898
899
900
901
902
903
904 public static void p( String s )
905 {
906 if ( isSysOut )
907 {
908 System.out.println( s );
909 }
910 else
911 {
912 if ( log.isDebugEnabled() )
913 {
914 log.debug( s );
915 }
916 }
917 }
918
919
920
921
922 public static void help()
923 {
924 p( "\n\n\n\n" );
925 p( "type 'shutDown' to shutdown the cache" );
926 p( "type 'getm num show[false|true]' to get num automatically from a region" );
927 p( "type 'putm num' to put num automatically to a region" );
928 p( "type 'removeall' to remove all items in a region" );
929 p( "type 'remove key' to remove" );
930 p( "type 'removem num' to remove a number automatically" );
931 p( "type 'getMatching pattern show' to getMatching" );
932 p( "type 'get key show' to get" );
933 p( "type 'getg key group show' to get" );
934 p( "type 'getag num group show' to get automatically from a group" );
935 p( "type 'getAttributeNames group' to get a list og the group elements" );
936 p( "type 'putg key group val' to put" );
937 p( "type 'putag num group' to put automatically from a group" );
938 p( "type 'put key val' to put" );
939 p( "type 'stats' to get stats" );
940 p( "type 'deattr' to get the default element attributes" );
941 p( "type 'cloneattr num' to clone attr" );
942 p( "type 'random range numOps' to put, get, and remove randomly" );
943 p( "type 'switch name' to switch to this region name" );
944 p( "type 'gc' to call System.gc()" );
945 p( "type 'help' for commands" );
946
947 }
948
949
950
951
952
953 public void getAttributeNames( String groupName )
954 {
955 Iterator<String> iter = group_cache_control.getGroupKeys( groupName ).iterator();
956
957 while ( iter.hasNext() )
958 {
959 p( "=" + iter.next() );
960 }
961 }
962 }