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