Created
November 26, 2011 08:56
-
-
Save cpatni/1395327 to your computer and use it in GitHub Desktop.
Caliper Benchmarks for Analytics using Bitmaps
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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