comparison src/org/dancres/blitz/remote/perf/Proxy.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 org.dancres.blitz.remote.perf;
2
3 import java.io.Serializable;
4
5 import java.rmi.RemoteException;
6 import java.rmi.MarshalledObject;
7
8 import net.jini.space.JavaSpace;
9
10 import net.jini.core.entry.Entry;
11 import net.jini.core.entry.UnusableEntryException;
12
13 import net.jini.core.transaction.Transaction;
14 import net.jini.core.transaction.TransactionException;
15
16 import net.jini.core.event.EventRegistration;
17 import net.jini.core.event.RemoteEventListener;
18
19 import net.jini.core.lease.Lease;
20
21 import net.jini.lookup.entry.ServiceInfo;
22 import net.jini.lookup.entry.Name;
23
24 import com.sun.jini.lookup.entry.BasicServiceType;
25
26 import net.jini.admin.Administrable;
27
28 import java.io.IOException;
29 import java.io.ObjectInputStream;
30 import java.io.FileInputStream;
31
32 import net.jini.id.Uuid;
33 import net.jini.id.ReferentUuid;
34 import net.jini.id.ReferentUuids;
35
36 import org.dancres.blitz.mangler.EntryMangler;
37 import org.dancres.blitz.mangler.MangledEntry;
38
39 import org.dancres.blitz.VersionInfo;
40
41 import com.sun.jini.proxy.MarshalledWrapper;
42
43 /**
44 */
45 class Proxy implements Serializable {
46 private Server theStub;
47 private transient EntryMangler theMangler;
48
49 public static void main(String anArgs[]) {
50 try {
51 String myStubFile = ServiceImpl.STUB_FILE;
52
53 if (anArgs.length > 1)
54 myStubFile = anArgs[1];
55
56 int myThreads = 1;
57
58 if (anArgs.length > 0)
59 myThreads = Integer.parseInt(anArgs[0]);
60
61 ObjectInputStream myOIS =
62 new ObjectInputStream(new FileInputStream(myStubFile));
63
64 Server myStub = (Server) myOIS.readObject();
65
66 System.out.println(myStub);
67
68 Proxy myProxy = new Proxy(myStub);
69
70 Thrasher[] myThrashers = new Thrasher[myThreads];
71
72 for (int i = 0; i < myThreads; i++) {
73 myThrashers[i] = new Thrasher(myProxy);
74 myThrashers[i].start();
75 }
76
77 new Watcher(myThrashers).start();
78
79 } catch (Exception anE) {
80 System.err.println("Failed to init proxy");
81 anE.printStackTrace(System.err);
82 }
83 }
84
85 private static class Watcher extends Thread {
86 private Thrasher[] theThrashers;
87
88 Watcher(Thrasher[] aThrashers) {
89 theThrashers = aThrashers;
90 }
91
92 public void run() {
93 while(true) {
94 try {
95 Thread.sleep(60000);
96
97 for (int i = 0; i < theThrashers.length; i++) {
98 theThrashers[i].dumpCounts();
99 }
100 } catch (InterruptedException anIE) {
101 System.err.println("Dead watcher");
102 }
103 }
104 }
105 }
106
107 private static class Thrasher extends Thread {
108 private Proxy theProxy;
109 private long theCount;
110
111 Thrasher(Proxy aProxy) {
112 theProxy = aProxy;
113 }
114
115 public void run() {
116 while (true) {
117 try {
118 DummyEntry myPackedEntry = new DummyEntry("rhubarb");
119
120 theProxy.read(myPackedEntry, null, 0);
121 theProxy.write(myPackedEntry, null, 0);
122
123 synchronized(this) {
124 ++theCount;
125 }
126 } catch (Exception anE) {
127 System.err.println("Thrasher died: " + anE);
128 }
129 }
130 }
131
132 public void dumpCounts() {
133 synchronized(this) {
134 System.out.println("Write/read combos: " + theCount);
135 theCount = 0;
136 }
137 }
138 }
139
140 Proxy(Server aServer) {
141 theStub = aServer;
142 }
143
144 private synchronized EntryMangler getMangler() {
145 if (theMangler == null)
146 theMangler = new EntryMangler();
147
148 return theMangler;
149 }
150
151 private MangledEntry packEntry(Entry anEntry) {
152 if (anEntry == null)
153 return MangledEntry.NULL_TEMPLATE;
154 // Is it a snapshot?
155 else if (anEntry instanceof MangledEntry)
156 return (MangledEntry) anEntry;
157 else
158 return getMangler().mangle(anEntry);
159 }
160
161 public Lease write(Entry entry, Transaction txn, long lease)
162 throws TransactionException, RemoteException {
163
164 return theStub.write(packEntry(entry), txn, lease);
165 }
166
167 public Entry read(Entry tmpl, Transaction txn, long timeout)
168 throws UnusableEntryException, TransactionException,
169 InterruptedException, RemoteException {
170
171 MangledEntry myResult =
172 theStub.read(packEntry(tmpl), txn, timeout);
173
174 return (myResult != null) ?
175 getMangler().unMangle(myResult) : null;
176 }
177
178 static final class SnapshotEntry implements Entry {
179 private MangledEntry thePackage;
180
181 SnapshotEntry(MangledEntry aPackage) {
182 thePackage = aPackage;
183 }
184
185 MangledEntry getPackage() {
186 return thePackage;
187 }
188 }
189 }