comparison examples/statsclient/stats/TestClient.java @ 0:3dc0c5604566

Initial checkin of blitz 2.0 fcs - no installer yet.
author Dan Creswell <dan.creswell@gmail.com>
date Sat, 21 Mar 2009 11:00:06 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:3dc0c5604566
1 package stats;
2
3 import java.rmi.RMISecurityManager;
4
5 import java.net.InetAddress;
6
7 import net.jini.space.JavaSpace;
8
9 import net.jini.core.entry.Entry;
10
11 import net.jini.core.lease.Lease;
12
13 import net.jini.admin.Administrable;
14
15 import org.dancres.blitz.remote.StatsAdmin;
16
17 import org.dancres.blitz.stats.*;
18
19 /**
20 A Blitz specific example. This client locates a JavaSpace and checks
21 for the Blitz admin interfaces. If the JavaSpace has these interfaces
22 the program proceeds to recover the currently active statistics,
23 activates some additional statistics, generates some activity (via a
24 Write to the space) and then prints out the new statistics.
25 */
26 public class TestClient {
27 public static void main(String args[]) {
28 try {
29 /*
30 Make sure we've installed the RMI security manager otherwise we
31 won't be able to download code.
32 */
33 if (System.getSecurityManager() == null)
34 System.setSecurityManager(new RMISecurityManager());
35
36 /*
37 Locates an already running service - WILL NOT notice Space
38 instances run after Lookup is started.
39 */
40 Lookup myFinder = new Lookup(JavaSpace.class);
41
42 /*
43 Locate JavaSpace service.
44 */
45 JavaSpace mySpace = (JavaSpace) myFinder.getService();
46
47 /*
48 Blitz is Administrable so if the current proxy isn't we haven't
49 found a Blitz instance
50 */
51 if (! (mySpace instanceof Administrable)) {
52 System.out.println("Whoops, this space isn't administrable");
53 return;
54 }
55
56 Object myAdmin = ((Administrable) mySpace).getAdmin();
57
58 /*
59 If the AdminProxy doesn't have a StatsAdmin it's not Blitz.
60 */
61 if (! (myAdmin instanceof StatsAdmin)) {
62 System.out.println("Whoops, this space hasn't got a StatsAdmin - can't be Blitz");
63 return;
64 }
65
66 /*
67 Stats access is via StatsAdmin on the Admin proxy.
68 */
69 StatsAdmin myStatsAdmin = (StatsAdmin) myAdmin;
70
71 /*
72 Display current stats
73 */
74 Stat[] myStats = myStatsAdmin.getStats();
75
76 System.out.println("Stats are currently:");
77 dumpStats(myStats);
78
79 /*
80 Enable tracking of takes and writes for TestEntry
81 (we could enable tracking for all types)
82 */
83 OpSwitch myOpSwitch1 = new OpSwitch(TestEntry.class.getName(),
84 OpSwitch.WRITE_OPS, true);
85 OpSwitch myOpSwitch2 = new OpSwitch(TestEntry.class.getName(),
86 OpSwitch.TAKE_OPS, true);
87
88 /*
89 Enable instance counts for all types (could specify
90 particular types if we wish).
91 */
92 InstanceSwitch myInstSwitch =
93 new InstanceSwitch(InstanceSwitch.ALL_TYPES, true);
94
95 Switch[] mySwitches = new Switch[3];
96 mySwitches[0] = myOpSwitch1;
97 mySwitches[1] = myOpSwitch2;
98 mySwitches[2] = myInstSwitch;
99
100 myStatsAdmin.setSwitches(mySwitches);
101
102
103 System.out.println();
104 System.out.println("Set switches, doing write");
105 System.out.println();
106
107 /*
108 Write a TestEntry
109 */
110 mySpace.write(new TestEntry("blah"), null, Lease.FOREVER);
111
112 myStats = myStatsAdmin.getStats();
113
114 /*
115 Does it appear in the stats?
116 */
117 System.out.println("Stats are now:");
118 dumpStats(myStats);
119
120 } catch (Exception aRE) {
121 System.err.println("Ooops");
122 aRE.printStackTrace(System.err);
123 }
124 }
125
126 public static class TestEntry implements Entry {
127 public String theName;
128
129 public TestEntry(String aName) {
130 theName = aName;
131 }
132 }
133
134 private static void dumpStats(Stat[] aStats) {
135 for (int i = 0; i < aStats.length; i++) {
136
137 if (aStats[i] instanceof InstanceCount) {
138 InstanceCount myCount = (InstanceCount) aStats[i];
139 System.out.println("Instances of type " +
140 myCount.getType() + " = " +
141 myCount.getCount());
142
143 } else if (aStats[i] instanceof TxnStat) {
144 TxnStat myTxns = (TxnStat) aStats[i];
145 System.out.println("Total active txns: " +
146 myTxns.getActiveTxnCount());
147
148 } else if (aStats[i] instanceof OpStat) {
149 OpStat myOp = (OpStat) aStats[i];
150 System.out.println("Total " + myOp.getOpTypeAsString() +
151 " on " + myOp.getType() + " = " +
152 myOp.getCount());
153
154 } else if (aStats[i] instanceof MemoryStat) {
155 MemoryStat myMem = (MemoryStat) aStats[i];
156 System.out.println("Memory used: " +
157 myMem.getCurrentMemory() + " out of " +
158 myMem.getMaxMemory());
159 } else if (aStats[i] instanceof BlockingOpsStat) {
160 BlockingOpsStat myBlockers = (BlockingOpsStat) aStats[i];
161 System.out.println("Blocking reads: " +
162 myBlockers.getReaders() + " takes: " +
163 myBlockers.getTakers());
164 } else if (aStats[i] instanceof HostStat) {
165 HostStat myHostStat = (HostStat) aStats[i];
166
167 System.out.println("Default host address: " +
168 myHostStat.getHostAddr());
169 InetAddress[] myIfAddrs = myHostStat.getAllAddr();
170
171 System.out.println("All known interface addresses");
172 for (int j = 0; j < myIfAddrs.length; j++) {
173 System.out.println(myIfAddrs[j]);
174 }
175 } else
176 System.out.println(aStats[i]);
177 }
178 }
179 }