|
| 1 | +/* |
| 2 | + * Copyright 2009 Joubin Houshyar |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.jredis.examples; |
| 18 | + |
| 19 | +import static org.jredis.ri.alphazero.support.DefaultCodec.toLong; |
| 20 | +import static org.jredis.ri.alphazero.support.DefaultCodec.toStr; |
| 21 | +import java.util.Random; |
| 22 | +import java.util.concurrent.ExecutionException; |
| 23 | +import java.util.concurrent.Future; |
| 24 | +import org.jredis.JRedisFuture; |
| 25 | +import org.jredis.ProviderException; |
| 26 | +import org.jredis.RedisException; |
| 27 | +import org.jredis.bench.Util; |
| 28 | +import org.jredis.bench.Util.Timer; |
| 29 | +import org.jredis.connector.ConnectionSpec; |
| 30 | +import org.jredis.connector.ConnectionSpec.SocketProperty; |
| 31 | +import org.jredis.protocol.ResponseStatus; |
| 32 | +import org.jredis.ri.alphazero.JRedisPipeline; |
| 33 | +import org.jredis.ri.alphazero.connection.DefaultConnectionSpec; |
| 34 | +import org.jredis.ri.alphazero.support.Log; |
| 35 | + |
| 36 | +/** |
| 37 | + * Pipelines are an order of magnitude faster than the request/reply connectors. |
| 38 | + * Get a sense of how much faster it is. |
| 39 | + * |
| 40 | + * <p>use JVM flags -server -Xms512m -Xmx2560m for better results (adjust your |
| 41 | + * mem settings per your box's limits.) |
| 42 | + * |
| 43 | + * @author Joubin Houshyar ([email protected]) |
| 44 | + * @version alpha.0, Nov 5, 2009 |
| 45 | + * @since alpha.0 |
| 46 | + * |
| 47 | + */ |
| 48 | + |
| 49 | +public class PipelineInAction { |
| 50 | + public static void main (String[] args) { |
| 51 | + final ConnectionSpec spec = DefaultConnectionSpec.newSpec(); |
| 52 | + spec.setCredentials("jredis".getBytes()); |
| 53 | + spec.setDatabase(13); |
| 54 | + spec.setSocketProperty(SocketProperty.SO_RCVBUF, 1024 * 512); |
| 55 | + spec.setSocketProperty(SocketProperty.SO_SNDBUF, 1024 * 512); |
| 56 | + |
| 57 | + usingSynchSemantics(spec); |
| 58 | + final boolean forever = true; |
| 59 | + |
| 60 | + runJRedisPipelineSET (spec, 10000, 3, forever); |
| 61 | + } |
| 62 | + /** |
| 63 | + * @param spec |
| 64 | + */ |
| 65 | + private static void usingSynchSemantics (ConnectionSpec spec) { |
| 66 | + JRedisPipeline pipeline = new JRedisPipeline(spec); |
| 67 | + try { |
| 68 | + long start = System.currentTimeMillis(); |
| 69 | + pipeline.ping(); |
| 70 | + pipeline.flushall(); |
| 71 | + String cntrKey = "my-cntr"; |
| 72 | + |
| 73 | + Random rand = new Random(); |
| 74 | + byte[] data = new byte[8]; |
| 75 | + for(int i=0; i<100000; i++) |
| 76 | + pipeline.incr(cntrKey); |
| 77 | + |
| 78 | + long cntr = toLong (pipeline.sync().get(cntrKey)); |
| 79 | + |
| 80 | + for(int i=0; i<100000; i++){ |
| 81 | + rand.nextBytes(data); |
| 82 | + pipeline.set("random:"+i, "value:" + rand.nextInt()); |
| 83 | + } |
| 84 | + String randomVal = toStr (pipeline.sync().get("random:"+999)); |
| 85 | + System.out.format ("end using sync() = %d msec\n", System.currentTimeMillis() - start); |
| 86 | + |
| 87 | + System.out.format("%s => %d\n", cntrKey, cntr); |
| 88 | + System.out.format("%s => %s\n", "random:"+999, randomVal); |
| 89 | + |
| 90 | + } |
| 91 | + catch (RedisException e) { |
| 92 | + Log.problem("RedisException: " + e); |
| 93 | + } |
| 94 | + finally{ |
| 95 | + pipeline.sync().quit(); |
| 96 | + } |
| 97 | + |
| 98 | + } |
| 99 | + /** |
| 100 | + * pipelines SET reqCnt times and then waits on response of the last INCR. |
| 101 | + * If foverever flag is true, will do so forever. Each cycle prints out timing stats. |
| 102 | + * @param spec |
| 103 | + * @param reqCnt |
| 104 | + * @param forever |
| 105 | + */ |
| 106 | + @SuppressWarnings("unused") |
| 107 | + private static void runJRedisPipelineGET (ConnectionSpec spec, int reqCnt, int size, boolean forever) { |
| 108 | + long totTime = 0; |
| 109 | + long avgRespTime = 0; |
| 110 | + float avgThroughput = (float)0; |
| 111 | + long iters = 0; |
| 112 | + JRedisFuture pipeline = new JRedisPipeline(spec); |
| 113 | + try { |
| 114 | + String key = "pipeKey"; |
| 115 | + byte[] data = new byte[size]; |
| 116 | + (new Random()).nextBytes(data); |
| 117 | + pipeline.del(key); |
| 118 | + pipeline.set(key, data); |
| 119 | + |
| 120 | + do { |
| 121 | + int cnt = 0; |
| 122 | + Util.Timer timer = Timer.startNewTimer(); |
| 123 | + Future<byte[]> futureBytes = null; |
| 124 | + while(cnt < reqCnt){ |
| 125 | + futureBytes = pipeline.get(key); |
| 126 | + cnt++; |
| 127 | + } |
| 128 | + long reqDoneTime = timer.mark(); |
| 129 | + byte[] value = futureBytes.get(); |
| 130 | + long respDoneTime = timer.mark(); |
| 131 | +// System.out.format("JRedisPipeline: %d GETs invoked @ %5d (%.2f ops/s)\n", cnt, reqDoneTime, timer.opsPerSecAtDelta(cnt, reqDoneTime)); |
| 132 | + float throughput = timer.opsPerSecAtMark(cnt); |
| 133 | +// System.out.format("JRedisPipeline: %d GETs completed @ %5d (%.2f ops/s) [%d msecs to comp] \n", cnt, timer.deltaAtMark(), throughput, respDoneTime-reqDoneTime); |
| 134 | + if(iters > 0){ |
| 135 | + totTime += respDoneTime; |
| 136 | + avgRespTime = (totTime) / (long)iters; |
| 137 | + avgThroughput =(float)( reqCnt * 1000) / (float) avgRespTime; |
| 138 | + System.out.format("JRedisPipeline: %d GETs [%d bytes/GET] average response time @ %dms (%.2f ops/s) last: %dms\n", cnt, data.length, avgRespTime, avgThroughput, respDoneTime); |
| 139 | +// Assert.isEquivalent(data, value); |
| 140 | + } |
| 141 | + iters ++; |
| 142 | +// System.out.println (); |
| 143 | + } while(forever); |
| 144 | + |
| 145 | + pipeline.quit(); |
| 146 | + } |
| 147 | + catch (ProviderException e) { |
| 148 | + e.printStackTrace(); |
| 149 | + } |
| 150 | + catch (InterruptedException e) { |
| 151 | + e.printStackTrace(); |
| 152 | + } |
| 153 | + catch (ExecutionException e) { |
| 154 | + e.printStackTrace(); |
| 155 | + } |
| 156 | + } |
| 157 | + @SuppressWarnings("unused") |
| 158 | + private static void runJRedisPipelinePING (ConnectionSpec spec, int reqCnt, int size, boolean forever) { |
| 159 | + long totTime = 0; |
| 160 | + long avgRespTime = 0; |
| 161 | + float avgThroughput = (float)0; |
| 162 | + long iters = 0; |
| 163 | + JRedisFuture pipeline = new JRedisPipeline(spec); |
| 164 | + try { |
| 165 | + do { |
| 166 | + int cnt = 0; |
| 167 | + Util.Timer timer = Timer.startNewTimer(); |
| 168 | + Future<ResponseStatus> futureStat = null; |
| 169 | + while(cnt < reqCnt){ |
| 170 | + futureStat = pipeline.ping(); |
| 171 | + cnt++; |
| 172 | + } |
| 173 | + long reqDoneTime = timer.mark(); |
| 174 | + futureStat.get(); |
| 175 | + long respDoneTime = timer.mark(); |
| 176 | +// System.out.format("JRedisPipeline: %d PINGs invoked @ %5d (%.2f ops/s)\n", cnt, reqDoneTime, timer.opsPerSecAtDelta(cnt, reqDoneTime)); |
| 177 | + float throughput = timer.opsPerSecAtMark(cnt); |
| 178 | +// System.out.format("JRedisPipeline: %d PINGs completed @ %5d (%.2f ops/s) [%d msecs to comp] \n", cnt, timer.deltaAtMark(), throughput, respDoneTime-reqDoneTime); |
| 179 | + if(iters > 0){ |
| 180 | + totTime += reqDoneTime; |
| 181 | + avgRespTime = (totTime) / (long)iters; |
| 182 | + avgThroughput =(float)( reqCnt * 1000) / (float) avgRespTime; |
| 183 | + System.out.print("\r"); |
| 184 | + System.out.format("JRedisPipeline: %d PINGs average response time @ %dms (%.2f ops/s)", cnt, avgRespTime, avgThroughput); |
| 185 | + } |
| 186 | + iters ++; |
| 187 | +// System.out.println (); |
| 188 | + } while(forever); |
| 189 | + |
| 190 | + pipeline.quit(); |
| 191 | + } |
| 192 | + catch (ProviderException e) { |
| 193 | + e.printStackTrace(); |
| 194 | + } |
| 195 | + catch (InterruptedException e) { |
| 196 | + e.printStackTrace(); |
| 197 | + } |
| 198 | + catch (ExecutionException e) { |
| 199 | + e.printStackTrace(); |
| 200 | + } |
| 201 | + } |
| 202 | + private static void runJRedisPipelineLPUSH (ConnectionSpec spec, int reqCnt, int size, boolean forever) { |
| 203 | + JRedisFuture pipeline = new JRedisPipeline(spec); |
| 204 | + long totTime = 0; |
| 205 | + long avgRespTime = 0; |
| 206 | + float avgThroughput = (float)0; |
| 207 | + long iters = 0; |
| 208 | + try { |
| 209 | + String key = "pipeKey"; |
| 210 | + byte[] data = new byte[size]; |
| 211 | + (new Random()).nextBytes(data); |
| 212 | + Future<Boolean> futureBool = pipeline.del(key); |
| 213 | + futureBool.get(); |
| 214 | + do { |
| 215 | + int cnt = 0; |
| 216 | + Util.Timer timer = Timer.startNewTimer(); |
| 217 | + Future<ResponseStatus> futureStat = null; |
| 218 | + while(cnt < reqCnt){ |
| 219 | + futureStat = pipeline.lpush(key, data); |
| 220 | + cnt++; |
| 221 | + } |
| 222 | + long reqDoneTime = timer.mark(); |
| 223 | + futureStat.get(); |
| 224 | + long respDoneTime = timer.mark(); |
| 225 | + System.out.format("JRedisPipeline: %d LPUSHs invoked @ %5d (%.2f ops/s)\n", cnt, reqDoneTime, timer.opsPerSecAtDelta(cnt, reqDoneTime)); |
| 226 | + System.out.format("JRedisPipeline: %d LPUSHs completed @ %5d (%.2f ops/s) [%d msecs to comp] \n", cnt, timer.deltaAtMark(), timer.opsPerSecAtMark(cnt), respDoneTime-reqDoneTime); |
| 227 | + if(iters > 0){ |
| 228 | + totTime += reqDoneTime; |
| 229 | + avgRespTime = (totTime) / (long)iters; |
| 230 | + avgThroughput =(float)( reqCnt * 1000) / (float) avgRespTime; |
| 231 | + System.out.format("JRedisPipeline: %d LPUSHs average response time @ %dms (%.2f ops/s) \n", cnt, avgRespTime, avgThroughput); |
| 232 | + } |
| 233 | + iters ++; |
| 234 | + System.out.println (); |
| 235 | + } while(forever); |
| 236 | + |
| 237 | + pipeline.quit(); |
| 238 | + } |
| 239 | + catch (ProviderException e) { |
| 240 | + e.printStackTrace(); |
| 241 | + } |
| 242 | + catch (InterruptedException e) { |
| 243 | + e.printStackTrace(); |
| 244 | + } |
| 245 | + catch (ExecutionException e) { |
| 246 | + e.printStackTrace(); |
| 247 | + } |
| 248 | + } |
| 249 | + |
| 250 | + /** |
| 251 | + * pipelines SET reqCnt times and then waits on response of the last INCR. |
| 252 | + * If foverever flag is true, will do so forever. Each cycle prints out timing stats. |
| 253 | + * @param spec |
| 254 | + * @param reqCnt |
| 255 | + * @param forever |
| 256 | + */ |
| 257 | + private static void runJRedisPipelineSET (ConnectionSpec spec, int reqCnt, int size, boolean forever) { |
| 258 | + JRedisFuture pipeline = new JRedisPipeline(spec); |
| 259 | + long totTime = 0; |
| 260 | + long avgRespTime = 0; |
| 261 | + float avgThroughput = (float)0; |
| 262 | + long iters = 0; |
| 263 | + try { |
| 264 | + String key = "pipeKey"; |
| 265 | + byte[] data = new byte[size]; |
| 266 | + (new Random()).nextBytes(data); |
| 267 | + Future<Boolean> futureBool = pipeline.del(key); |
| 268 | + futureBool.get(); |
| 269 | + do { |
| 270 | + int cnt = 0; |
| 271 | + Util.Timer timer = Timer.startNewTimer(); |
| 272 | + Future<ResponseStatus> futureStat = null; |
| 273 | + while(cnt < reqCnt){ |
| 274 | + futureStat = pipeline.set(key, data); |
| 275 | + cnt++; |
| 276 | + } |
| 277 | + long reqDoneTime = timer.mark(); |
| 278 | + futureStat.get(); |
| 279 | + long respDoneTime = timer.mark(); |
| 280 | +// System.out.format("JRedisPipeline: %d SETs invoked @ %5d (%.2f ops/s)\n", cnt, reqDoneTime, timer.opsPerSecAtDelta(cnt, reqDoneTime)); |
| 281 | +// System.out.format("JRedisPipeline: %d SETs completed @ %5d (%.2f ops/s) [%d msecs to comp] \n", cnt, timer.deltaAtMark(), timer.opsPerSecAtMark(cnt), respDoneTime-reqDoneTime); |
| 282 | + if(iters > 0){ |
| 283 | + totTime += respDoneTime; |
| 284 | + avgRespTime = (totTime) / (long)iters; |
| 285 | + avgThroughput =(float)( reqCnt * 1000) / (float) avgRespTime; |
| 286 | + System.out.format("JRedisPipeline: %d SETs [%d bytes/GET] average response time @ %dms (%.2f ops/s) \n", cnt, data.length, avgRespTime, avgThroughput); |
| 287 | + } |
| 288 | + iters ++; |
| 289 | +// System.out.println (); |
| 290 | + } while(forever); |
| 291 | + |
| 292 | + pipeline.quit(); |
| 293 | + } |
| 294 | + catch (ProviderException e) { |
| 295 | + e.printStackTrace(); |
| 296 | + } |
| 297 | + catch (InterruptedException e) { |
| 298 | + e.printStackTrace(); |
| 299 | + } |
| 300 | + catch (ExecutionException e) { |
| 301 | + e.printStackTrace(); |
| 302 | + } |
| 303 | + } |
| 304 | + /** |
| 305 | + * pipelines INCRs reqCnt times and then waits on response of the last INCR. |
| 306 | + * If foverever flag is true, will do so forever. Each cycle prints out timing stats. |
| 307 | + * @param spec |
| 308 | + * @param reqCnt |
| 309 | + * @param forever |
| 310 | + */ |
| 311 | + private static void runJRedisPipelineINCR (ConnectionSpec spec, int reqCnt, int size, boolean forever) { |
| 312 | + JRedisFuture pipeline = new JRedisPipeline(spec); |
| 313 | + long totTime = 0; |
| 314 | + long avgRespTime = 0; |
| 315 | + float avgThroughput = (float)0; |
| 316 | + long iters = 0; |
| 317 | + try { |
| 318 | + String key = "pipeCounter"; |
| 319 | + Future<Boolean> futureBool = pipeline.del(key); |
| 320 | + futureBool.get(); |
| 321 | + do { |
| 322 | + int cnt = 0; |
| 323 | + Util.Timer timer = Timer.startNewTimer(); |
| 324 | + Future<Long> futureLong = null; |
| 325 | + while(cnt < reqCnt){ |
| 326 | + futureLong = pipeline.incr(key); |
| 327 | + cnt++; |
| 328 | + } |
| 329 | + long reqDoneTime = timer.mark(); |
| 330 | + long counter = futureLong.get(); |
| 331 | + long respDoneTime = timer.mark(); |
| 332 | + System.out.format("JRedisPipeline: %d INCRs invoked @ %5d (%.2f ops/s)\n", cnt, reqDoneTime, timer.opsPerSecAtDelta(cnt, reqDoneTime)); |
| 333 | + System.out.format("JRedisPipeline: %d INCRs completed @ %5d (%.2f ops/s) [%d msecs to comp] \n", cnt, timer.deltaAtMark(), timer.opsPerSecAtMark(cnt), respDoneTime-reqDoneTime); |
| 334 | + System.out.format ("counter is now: %d\n\n", counter); |
| 335 | + if(iters > 0){ |
| 336 | + totTime += reqDoneTime; |
| 337 | + avgRespTime = (totTime) / (long)iters; |
| 338 | + avgThroughput =(float)( reqCnt * 1000) / (float) avgRespTime; |
| 339 | + System.out.format("JRedisPipeline: %d INCRs average response time @ %dms (%.2f ops/s) \n", cnt, avgRespTime, avgThroughput); |
| 340 | + } |
| 341 | + iters ++; |
| 342 | + System.out.println (); |
| 343 | + } while(forever); |
| 344 | + |
| 345 | + pipeline.quit(); |
| 346 | + } |
| 347 | + catch (ProviderException e) { |
| 348 | + e.printStackTrace(); |
| 349 | + } |
| 350 | + catch (InterruptedException e) { |
| 351 | + e.printStackTrace(); |
| 352 | + } |
| 353 | + catch (ExecutionException e) { |
| 354 | + e.printStackTrace(); |
| 355 | + } |
| 356 | + } |
| 357 | +} |
0 commit comments