-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMemcachedBenchmark.java
More file actions
119 lines (105 loc) · 2.96 KB
/
MemcachedBenchmark.java
File metadata and controls
119 lines (105 loc) · 2.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import net.spy.memcached.*;
import java.net.InetSocketAddress;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;
public class MemcachedBenchmark extends Thread {
public int id;
public long time_ms;
public MemcachedBenchmark(int i) {
super();
id = i;
}
public void runBenchmark() throws Exception {
byte[] value;
MemcachedClient c;
value = new byte[MemcachedBenchmark.valueLength];
Arrays.fill(value, (byte)'*');
c = new MemcachedClient(AddrUtil.getAddresses(MemcachedBenchmark.address));
for (long i = 0; i < MemcachedBenchmark.numRequests; i++) {
String key = String.format("%04d-%011d", id, i);
if (MemcachedBenchmark.command == 'w') {
c.set(key, 100000, value);
c.waitForQueues(Long.MAX_VALUE, TimeUnit.MILLISECONDS);
} else {
c.get(key);
}
}
c.shutdown(Long.MAX_VALUE, TimeUnit.MILLISECONDS);
}
public void run() {
long start = System.currentTimeMillis();
try {
runBenchmark();
} catch (Exception e) {
System.err.println("benchmark failed: " + e);
e.printStackTrace();
}
long end = System.currentTimeMillis();
time_ms = end - start;
}
public static void benchmark() throws Exception {
MemcachedBenchmark[] threads;
long sum = 0;
long min_ms = Long.MAX_VALUE;
long max_ms = 0;
long avg_ms;
threads = new MemcachedBenchmark[numThreads];
for (int i = 0; i < numThreads; i++) {
threads[i] = new MemcachedBenchmark(i);
}
for (int i = 0; i < numThreads; i++) {
threads[i].start();
}
for (int i = 0; i < numThreads; i++) {
threads[i].join();
}
for (int i = 0; i < numThreads; i++) {
long ms = threads[i].time_ms;
sum += ms;
min_ms = Math.min(min_ms, ms);
max_ms = Math.max(max_ms, ms);
}
avg_ms = sum / numThreads;
System.out.println(numThreads +" " + avg_ms / 1000.0 + " " +
min_ms / 1000.0 + " " + max_ms / 1000.0);
}
private static int valueLength = 100;
private static long numRequests = 100000L;
/* 'r' for Read, 'w' for Write request */
private static char command = 'w';
private static int numThreads = 8;
private static int verbose = 0;
private static String address = "127.0.0.1:21201";
private static int tcp_nodelay = 0;
public static void parseOptions(String args[]) {
for (int i = 0; i < args.length; i++) {
if (args[i].equals("-n")) {
numRequests = Long.parseLong(args[i + 1]);
i++;
} else if (args[i].equals("-l")) {
valueLength = Integer.parseInt(args[i + 1]);
i++;
} else if (args[i].equals("-s")) {
address = args[i + 1];
i++;
} else if (args[i].equals("-t")) {
numThreads = Integer.parseInt(args[i + 1]);
i++;
} else if (args[i].equals("-r")) {
command = 'r';
} else if (args[i].equals("-w")) {
command = 'w';
} else if (args[i].equals("-v")) {
verbose = 1;
} else if (args[i].equals("-d")) {
tcp_nodelay = 1;
} else {
System.err.println("invalid option");
}
}
}
public static void main(String args[]) throws Exception {
parseOptions(args);
benchmark();
}
}