Skip to content

Commit a0c17f9

Browse files
author
WANG, Xingen
committed
add test and change the test suite.
remove unsed comma.
1 parent 3836a7e commit a0c17f9

File tree

4 files changed

+126
-7
lines changed

4 files changed

+126
-7
lines changed

src/main/java/com/whalin/MemCached/SockIOPool.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ public class SockIOPool {
203203
// set to hold sockets to close
204204
private Map<String, Map<SockIO, Long>> availPool;
205205
private Map<String, Map<SockIO, Long>> busyPool;
206-
private Map<SockIO, Integer> deadPool;;
206+
private Map<SockIO, Integer> deadPool;
207207

208208
private SchoonerSockIOPool schoonerSockIOPool;
209209

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
/**
2+
* Copyright (c) 2008 Greg Whalin
3+
* All rights reserved.
4+
*
5+
* This library is free software; you can redistribute it and/or
6+
* modify it under the terms of the BSD license
7+
*
8+
* This library is distributed in the hope that it will be
9+
* useful, but WITHOUT ANY WARRANTY; without even the implied
10+
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11+
* PURPOSE.
12+
*
13+
* You should have received a copy of the BSD License along with this
14+
* library.
15+
*
16+
* @author Greg Whalin <[email protected]>
17+
*/
18+
package com.whalin.MemCached.test;
19+
20+
import java.util.Random;
21+
22+
import com.whalin.MemCached.MemCachedClient;
23+
import com.whalin.MemCached.SockIOPool;
24+
25+
public class MemcachedAvailTest {
26+
27+
/**
28+
* This runs through some simple tests of the MemcacheClient.
29+
*
30+
* Command line args: args[0] = number of threads to spawn args[1] = number
31+
* of runs per thread args[2] = size of object to store
32+
*
33+
* @param args
34+
* the command line arguments
35+
*/
36+
public static void main(String[] args) {
37+
38+
String[] serverlist = { "dev12:11211" };
39+
40+
int threadNum = Integer.parseInt(args[0]);
41+
int runs = Integer.parseInt(args[1]);
42+
int size = 1024 * Integer.parseInt(args[2]); // how many kilobytes
43+
44+
// initialize the pool for memcache servers
45+
SockIOPool pool = SockIOPool.getInstance();
46+
pool.setServers(serverlist);
47+
48+
pool.initialize();
49+
50+
// get object to store
51+
int[] obj = new int[size];
52+
for (int i = 0; i < size; i++) {
53+
obj[i] = i;
54+
}
55+
56+
String[] keys = new String[size];
57+
for (int i = 0; i < size; i++) {
58+
keys[i] = "test_key" + i;
59+
}
60+
Bench[] bens = new Bench[threadNum];
61+
for (int i = 0; i < threadNum; i++) {
62+
bens[i] = new Bench(runs, i, obj, keys);
63+
bens[i].start();
64+
}
65+
for (Bench ben : bens) {
66+
try {
67+
ben.join();
68+
} catch (InterruptedException e) {
69+
e.printStackTrace();
70+
}
71+
}
72+
pool.shutDown();
73+
}
74+
75+
/**
76+
* Test code per thread.
77+
*/
78+
private static class Bench extends Thread {
79+
private int runs;
80+
private int threadNum;
81+
private int[] object;
82+
private String[] keys;
83+
private int size;
84+
85+
public Bench(int runs, int threadNum, int[] object, String[] keys) {
86+
this.runs = runs;
87+
this.threadNum = threadNum;
88+
this.object = object;
89+
this.keys = keys;
90+
this.size = object.length;
91+
}
92+
93+
public void run() {
94+
95+
// get client instance
96+
MemCachedClient mc = new MemCachedClient();
97+
98+
// time stores
99+
long start = System.currentTimeMillis();
100+
for (int i = 0; i < runs; i++) {
101+
mc.set(keys[i], object);
102+
}
103+
long elapse = System.currentTimeMillis() - start;
104+
float avg = (float) elapse / runs;
105+
System.out.println("thread " + threadNum + ": runs: " + runs + " stores of obj " + (size / 1024)
106+
+ "KB -- avg time per req " + avg + " ms (total: " + elapse + " ms)");
107+
Random r = new Random();
108+
for (int i = 0; i < runs; i++) {
109+
try {
110+
Thread.sleep(r.nextInt(10));
111+
} catch (InterruptedException e) {
112+
e.printStackTrace();
113+
}
114+
start = System.nanoTime();
115+
mc.get(keys[i]);
116+
elapse = (System.nanoTime() - start) / 1000000;
117+
if (elapse > avg * 3)
118+
System.err.println(elapse + "ms for get in " + Thread.currentThread().getName());
119+
}
120+
121+
}
122+
}
123+
}

src/test/java/com/whalin/MemCached/test/MemcachedBench.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public static void main(String[] args) {
2626

2727
int runs = Integer.parseInt(args[0]);
2828

29-
String[] serverlist = { "localhost:11211", "localhost:11212" };
29+
String[] serverlist = { "localhost:11211" };
3030

3131
// initialize the pool for memcache servers
3232
SockIOPool pool = SockIOPool.getInstance("test");

src/test/java/com/whalin/MemCached/test/MemcachedTest.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,18 +38,14 @@ public class MemcachedTest {
3838
*/
3939
public static void main(String[] args) {
4040

41-
String[] serverlist = { "hengtiandesk144:11211", "hengtiandesk144:11212" };
41+
String[] serverlist = { "localhost:11211" };
4242

4343
// initialize the pool for memcache servers
4444
SockIOPool pool = SockIOPool.getInstance();
4545
pool.setServers(serverlist);
4646

47-
pool.setInitConn(5);
48-
pool.setMinConn(5);
49-
pool.setMaxConn(50);
5047
// pool.setMaintSleep(30);
5148

52-
pool.setNagle(false);
5349
pool.initialize();
5450

5551
int threads = Integer.parseInt(args[0]);

0 commit comments

Comments
 (0)