Skip to content

Instantly share code, notes, and snippets.

@alphazero
Created December 5, 2009 21:02
Show Gist options
  • Save alphazero/249861 to your computer and use it in GitHub Desktop.
Save alphazero/249861 to your computer and use it in GitHub Desktop.
/*
* Copyright 2009 Joubin Houshyar
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package test;
import org.jredis.JRedis;
import org.jredis.RedisException;
import org.jredis.connector.ConnectionSpec;
import org.jredis.ri.alphazero.JRedisClient;
import org.jredis.ri.alphazero.connection.DefaultConnectionSpec;
public class Bench {
public static void main (String[] args) { new Bench().run();}
public void run() {
final Bench self=this;
Process ping1 = new Process() { public void run (Object ctx) {self.connectToPing((Integer)ctx); }};
Process ping2 = new Process() { public void run (Object ctx) {self.connectThenPing((Integer)ctx); }};
for(int i=0; i<20; i++)
bench("connect and ping in each iteration ", ping1, 10000);
for(int i=0; i<20; i++)
bench("connect then ping in each iteration", ping2, 10000);
}
public void bench(String name, Process proc, int iterations) {
long start = System.currentTimeMillis();
proc.run(new Integer(iterations));
long delta = System.currentTimeMillis() - start;
float ops = ((float)iterations/(delta)*1000);
System.out.format("%s: %d iterations in %6d msec @ %7f ops/sec\n", name, iterations, delta, ops);
}
public interface Process { public void run(Object context); }
public void connectToPing(int iterations){
ConnectionSpec spec = DefaultConnectionSpec.newSpec();
JRedis jredis = null;
int i = 0;
try {
for(; i<iterations; i++){
jredis = new JRedisClient(spec);
jredis.ping();
jredis.quit();
}
}
catch (Throwable e) { System.out.println (i); e.printStackTrace(); }
}
public void connectThenPing(int iterations){
ConnectionSpec spec = DefaultConnectionSpec.newSpec();
JRedis jredis = null;
try {
jredis = new JRedisClient(spec);
for(int i=0; i<iterations; i++){
jredis.ping();
}
jredis.quit();
}
catch (RedisException e) { e.printStackTrace(); }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment