Skip to content

Commit 3d26db2

Browse files
committed
Add multi-thread support for performance test
Change-Id: Ibf42e4e926688acb42b0fd1d4d90d6e64426ad06
1 parent 4ad5c69 commit 3d26db2

File tree

1 file changed

+39
-59
lines changed

1 file changed

+39
-59
lines changed

src/test/com/schooner/MemCached/MemcachedPerfTest.java

Lines changed: 39 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,10 @@
2828
******************************************************************************/
2929
package com.schooner.MemCached;
3030

31-
import java.util.Hashtable;
32-
3331
import com.danga.MemCached.MemCachedClient;
3432

3533
public class MemcachedPerfTest {
3634

37-
// store results from threads
38-
private static Hashtable<Integer, StringBuilder> threadInfo = new Hashtable<Integer, StringBuilder>();
39-
4035
/**
4136
* This runs through some simple tests of the MemcacheClient.
4237
*
@@ -62,34 +57,37 @@ public static void main(String[] args) {
6257
pool.setNagle(false);
6358
pool.initialize();
6459

65-
int threads = Integer.parseInt(args[0]);
66-
int runs = Integer.parseInt(args[1]);
67-
int size = 1024 * Integer.parseInt(args[2]); // how many kilobytes
60+
int threads = Integer.parseInt(args[1]);
61+
int runs = Integer.parseInt(args[2]);
62+
int size = 1024 * Integer.parseInt(args[3]); // how many kilobytes
6863

6964
// get object to store
7065
int[] obj = new int[size];
7166
for (int i = 0; i < size; i++) {
7267
obj[i] = i;
7368
}
74-
69+
bench[] b = new bench[threads];
7570
for (int i = 0; i < threads; i++) {
76-
bench b = new bench(runs, i, obj);
77-
b.start();
71+
b[i] = new bench(runs, obj, args[0]);
7872
}
7973

80-
int i = 0;
81-
while (i < threads) {
82-
if (threadInfo.containsKey(new Integer(i))) {
83-
System.out.println(threadInfo.get(new Integer(i)));
84-
i++;
85-
} else {
86-
try {
87-
Thread.sleep(1000);
88-
} catch (InterruptedException e) {
89-
e.printStackTrace();
90-
}
74+
long start, elapse;
75+
float avg;
76+
start = System.currentTimeMillis();
77+
for (int i = 0; i < threads; i++) {
78+
b[i].start();
79+
}
80+
for (int i = 0; i < threads; i++) {
81+
try {
82+
b[i].join();
83+
} catch (InterruptedException e) {
84+
e.printStackTrace();
9185
}
9286
}
87+
elapse = System.currentTimeMillis() - start;
88+
avg = (float) elapse * 1000 / (runs * threads);
89+
System.out.println(args[0] + " runs: " + runs * threads + " stores of obj " + (size / 1024)
90+
+ "KB -- avg time per req " + avg + " us (total: " + elapse + " ms)");
9391

9492
pool.shutDown();
9593
System.exit(1);
@@ -100,54 +98,36 @@ public static void main(String[] args) {
10098
*/
10199
private static class bench extends Thread {
102100
private int runs;
103-
private int threadNum;
101+
private String threadName;
104102
private int[] object;
105-
private int size;
103+
private String opKind;
106104

107-
public bench(int runs, int threadNum, int[] object) {
105+
public bench(int runs, int[] object, String opKind) {
108106
this.runs = runs;
109-
this.threadNum = threadNum;
107+
this.threadName = getName();
110108
this.object = object;
111-
this.size = object.length;
109+
this.opKind = opKind;
112110
}
113111

114112
public void run() {
115-
116-
StringBuilder result = new StringBuilder();
117-
118113
// get client instance
119114
MemCachedClient mc = new MemCachedClient("test");
120115

121-
// time stores
122-
long start = System.currentTimeMillis();
123-
for (int i = 0; i < runs; i++) {
124-
mc.set(getName() + " " + i, object);
125-
}
126-
long elapse = System.currentTimeMillis() - start;
127-
float avg = (float) elapse / runs;
128-
result.append("\nthread " + threadNum + ": runs: " + runs + " stores of obj " + (size / 1024)
129-
+ "KB -- avg time per req " + avg + " ms (total: " + elapse + " ms)");
130-
131-
start = System.currentTimeMillis();
132-
for (int i = 0; i < runs; i++) {
133-
mc.get(getName() + " " + i);
134-
}
135-
elapse = System.currentTimeMillis() - start;
136-
avg = (float) elapse / runs;
137-
result.append("\nthread " + threadNum + ": runs: " + runs + " gets of obj " + (size / 1024)
138-
+ "KB -- avg time per req " + avg + " ms (total: " + elapse + " ms)");
139-
140-
// time deletes
141-
start = System.currentTimeMillis();
142-
for (int i = 0; i < runs; i++) {
143-
mc.delete(getName() + " " + i);
116+
if (opKind.equals("set")) {
117+
// time stores
118+
for (int i = 0; i < runs; i++) {
119+
mc.set(threadName + "_" + i, object);
120+
}
121+
} else if (opKind.equals("get")) {
122+
for (int i = 0; i < runs; i++) {
123+
mc.get(threadName + "_" + i);
124+
}
125+
} else {
126+
// time deletes
127+
for (int i = 0; i < runs; i++) {
128+
mc.delete(threadName + "_" + i);
129+
}
144130
}
145-
elapse = System.currentTimeMillis() - start;
146-
avg = (float) elapse / runs;
147-
result.append("\nthread " + threadNum + ": runs: " + runs + " deletes of obj " + (size / 1024)
148-
+ "KB -- avg time per req " + avg + " ms (total: " + elapse + " ms)");
149-
150-
threadInfo.put(new Integer(threadNum), result);
151131
}
152132
}
153133
}

0 commit comments

Comments
 (0)