Skip to content

Instantly share code, notes, and snippets.

@cpatni
Created November 26, 2011 08:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save cpatni/1395327 to your computer and use it in GitHub Desktop.
Save cpatni/1395327 to your computer and use it in GitHub Desktop.
Caliper Benchmarks for Analytics using Bitmaps
package blog;
import com.google.caliper.SimpleBenchmark;
import java.util.BitSet;
public class BitSetBenchmark extends SimpleBenchmark{
private BitSet bitSet;
@Override
protected void setUp() {
int howMany = 1000 * 1000 * 1000;
double probability = 0.9;
bitSet = new BitSet(howMany);
for (int i = 0; i < howMany; i++) {
if (Math.random() < probability) {
bitSet.set(i, true);
}
}
}
public void timePopulationCount(int reps) {
for (int i = 0; i < reps; i++) {
bitSet.cardinality();
}
}
}
package blog;
import com.google.caliper.SimpleBenchmark;
import redis.clients.jedis.Jedis;
import java.util.BitSet;
public class BitSetUnionBenchmark extends SimpleBenchmark {
Jedis redis;
@Override
protected void tearDown() throws Exception {
redis.quit();
redis.disconnect();
}
@Override
protected void setUp() {
redis = new Jedis("localhost");
int howMany = 128 * 1024 * 1024;
double probability = 0.9;
for (int j = 0; j < 30; j++) {
BitSet bitSet = new BitSet(howMany);
for (int i = 0; i < howMany; i++) {
if (Math.random() < probability) {
bitSet.set(i, true);
}
}
String key = "union:" + j;
redis.set(key.getBytes(), bitSet.toByteArray());
}
}
public void timeDailyUniqueCount(int reps) {
for (int i = 0; i < reps; i++) {
String key = "union:0";
byte[] bytes = redis.get(key.getBytes());
BitSet.valueOf(bytes).cardinality();
}
}
public void timeWeeklyUniqueCount(int reps) {
for (int i = 0; i < reps; i++) {
String[] keys = new String[7];
for (int j = 0; j < 7; j++) {
keys[j] = String.valueOf(j);
}
uniqueCount("union", keys);
}
}
public void timeMonthlyUniqueCount(int reps) {
for (int i = 0; i < reps; i++) {
String[] keys = new String[30];
for (int j = 0; j < 30; j++) {
keys[j] = String.valueOf(j);
}
uniqueCount("union", keys);
}
}
public int uniqueCount(String event, String[] dates) {
BitSet all = new BitSet();
for (String date : dates) {
String key = event + ":" + date;
BitSet users = BitSet.valueOf(redis.get(key.getBytes()));
all.or(users);
}
return all.cardinality();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment