From d58d1406f51ecff4acba3fa8ea9746983963561a Mon Sep 17 00:00:00 2001 From: Alan Bernstein Date: Thu, 8 Dec 2016 19:14:24 -0600 Subject: [PATCH] Add zipf benchmark with efficient random ID permutations --- bench/permutations.go | 66 +++++++++++++++++++ bench/zipf.go | 122 +++++++++++++++++++++++++++++++++++ cmd/pilosactl/main.go | 3 + cmd/pilosactl/zipfspawn.json | 10 +++ 4 files changed, 201 insertions(+) create mode 100644 bench/permutations.go create mode 100644 bench/zipf.go create mode 100644 cmd/pilosactl/zipfspawn.json diff --git a/bench/permutations.go b/bench/permutations.go new file mode 100644 index 000000000..4f4a78961 --- /dev/null +++ b/bench/permutations.go @@ -0,0 +1,66 @@ +package bench + +// A PermutationGenerator provides a way to pass integer IDs through a permutation +// map that is pseudorandom but repeatable. This could be done with rand.Perm, +// but that would require storing a [Iterations]int64 array, which we want to avoid +// for large values of Iterations. +// It works by using a Linear Congruence Generator (https://en.wikipedia.org/wiki/Linear_congruential_generator) +// with modulus = Iterations, +// c = an arbitrary prime, +// a = computed to ensure the full period. + +// relevant stackoverflow: http://cs.stackexchange.com/questions/29822/lazily-computing-a-random-permutation-of-the-positive-integers + +type PermutationGenerator struct { + a int64 + c int64 + m int64 +} + +func NewPermutationGenerator(m int64, seed int64) *PermutationGenerator { + // figure out 'a' and 'c', return PermutationGenerator + a := LCGmultiplierFromModulus(m, seed) + c := int64(22695479) + return &PermutationGenerator{a, c, m} +} + +func (p *PermutationGenerator) Next(n int64) int64 { + // run one step of the LCG + return (n*p.a + p.c) % p.m +} + +func LCGmultiplierFromModulus(m int64, seed int64) int64 { + // LCG parameters must satisfy three conditions: + // 1. m and c are relatively prime (satisfied for prime c != m) + // 2. a-1 is divisible by all prime factors of m + // 3. a-1 is divisible by 4 if m is divisible by 4 + // Additionally, a seed can be used to select between different permutations + factors := primeFactors(m) + product := int64(1) + for p := range factors { + // satisfy condition 2 + product *= p + } + + if m%4 == 0 { + // satisfy condition 3 + product *= 2 + } + + return product*seed + 1 +} + +func primeFactors(n int64) map[int64]int { + // Returns map of {integerFactor: count, ...} + // This is a naive algorithm that will not work well for large prime n. + factors := make(map[int64]int) + for i := int64(2); i <= n; i++ { + div, mod := n/i, n%i + for mod == 0 { + factors[i] += 1 + n = div + div, mod = n/i, n%i + } + } + return factors +} diff --git a/bench/zipf.go b/bench/zipf.go new file mode 100644 index 000000000..0a45fcf8a --- /dev/null +++ b/bench/zipf.go @@ -0,0 +1,122 @@ +package bench + +import ( + "fmt" + + "flag" + "io/ioutil" + + "context" + "math/rand" + "time" +) + +// ZipfSetBits sets bits randomly and deterministically based on a seed, according to the Zipf distribution +type ZipfSetBits struct { + HasClient + BaseBitmapID int64 + BaseProfileID int64 + BitmapIDRange int64 + ProfileIDRange int64 + Iterations int // number of bits that will be set + Seed int64 + BitmapExponent float64 + BitmapOffset float64 + ProfileExponent float64 + ProfileOffset float64 + DB string // DB to use in pilosa. + +} + +func (b *ZipfSetBits) Usage() string { + return ` +zipf-set-bits sets random bits according to Zipf distribution + +Usage: zipf-set-bits [arguments] + +The following arguments are available: + + -base-bitmap-id int + bits being set will all be greater than BaseBitmapID + + -bitmap-id-range int + number of possible bitmap ids that can be set + + -base-profile-id int + profile id num to start from + + -profile-id-range int + number of possible profile ids that can be set + + -iterations int + number of bits to set + + -seed int + Seed for RNG + + -db string + pilosa db to use + + BitmapExponent float64 + BitmapOffset float64 + ProfileExponent float64 + ProfileOffset float64 + + -client-type string + Can be 'single' (all agents hitting one host) or 'round_robin' +`[1:] +} + +func (b *ZipfSetBits) ConsumeFlags(args []string) ([]string, error) { + fs := flag.NewFlagSet("ZipfSetBits", flag.ContinueOnError) + fs.SetOutput(ioutil.Discard) + fs.Int64Var(&b.BaseBitmapID, "base-bitmap-id", 0, "") + fs.Int64Var(&b.BitmapIDRange, "bitmap-id-range", 100000, "") + fs.Int64Var(&b.BaseProfileID, "base-profile-id", 0, "") + fs.Int64Var(&b.ProfileIDRange, "profile-id-range", 100000, "") + fs.Int64Var(&b.Seed, "seed", 1, "") + fs.IntVar(&b.Iterations, "iterations", 100, "") + fs.StringVar(&b.DB, "db", "benchdb", "") + fs.Float64Var(&b.BitmapExponent, "bitmap-exponent", 1.01, "") + fs.Float64Var(&b.BitmapOffset, "bitmap-offset", 1, "") + fs.Float64Var(&b.ProfileExponent, "profile-exponent", 1.01, "") + fs.Float64Var(&b.ProfileOffset, "profile-offset", 1, "") + fs.StringVar(&b.ClientType, "client-type", "single", "") + + if err := fs.Parse(args); err != nil { + return nil, err + } + return fs.Args(), nil +} + +// Run runs the ZipfSetBits benchmark +func (b *ZipfSetBits) Run(ctx context.Context, agentNum int) map[string]interface{} { + rnd := rand.New(rand.NewSource(b.Seed + int64(agentNum))) + bitmapRng := rand.NewZipf(rnd, b.BitmapExponent, b.BitmapOffset, uint64(b.BitmapIDRange)) + profileRng := rand.NewZipf(rnd, b.ProfileExponent, b.ProfileOffset, uint64(b.ProfileIDRange)) + bitmapPerm := NewPermutationGenerator(b.BitmapIDRange, b.Seed) + profilePerm := NewPermutationGenerator(b.ProfileIDRange, b.Seed) + + results := make(map[string]interface{}) + if b.cli == nil { + results["error"] = fmt.Errorf("No client set for ZipfSetBits agent: %v", agentNum) + return results + } + s := NewStats() + var start time.Time + for n := 0; n < b.Iterations; n++ { + // generate IDs from Zipf distribution + bitmapIDOriginal := bitmapRng.Uint64() + profIDOriginal := profileRng.Uint64() + // permute IDs randomly, but repeatably + bitmapID := bitmapPerm.Next(int64(bitmapIDOriginal)) + profID := profilePerm.Next(int64(profIDOriginal)) + + query := fmt.Sprintf("SetBit(%d, 'frame.n', %d)", b.BaseBitmapID+int64(bitmapID), b.BaseProfileID+int64(profID)) + start = time.Now() + b.cli.ExecuteQuery(ctx, b.DB, query, true) + s.Add(time.Now().Sub(start)) + } + AddToResults(s, results) + return results +} diff --git a/cmd/pilosactl/main.go b/cmd/pilosactl/main.go index 6408794df..41bcec7fd 100644 --- a/cmd/pilosactl/main.go +++ b/cmd/pilosactl/main.go @@ -1162,6 +1162,8 @@ func (cmd *BagentCommand) ParseFlags(args []string) error { bm = &bench.DiagonalSetBits{} case "random-set-bits": bm = &bench.RandomSetBits{} + case "zipf-set-bits": + bm = &bench.ZipfSetBits{} case "multi-db-set-bits": bm = &bench.MultiDBSetBits{} case "random-query": @@ -1208,6 +1210,7 @@ The following arguments are available: subcommands: diagonal-set-bits random-set-bits + zipf-set-bits multi-db-set-bits random-query import diff --git a/cmd/pilosactl/zipfspawn.json b/cmd/pilosactl/zipfspawn.json new file mode 100644 index 000000000..2d9320357 --- /dev/null +++ b/cmd/pilosactl/zipfspawn.json @@ -0,0 +1,10 @@ +{ + "CreatorArgs": ["-type", "local", "-serverN", "3", "-replicaN", "1"], + "Agents": { "Type": "local" }, + "Benchmarks": [ + { + "Num": 3, + "Args": ["zipf-set-bits", "-iterations", "30000", "-profile-id-range", "1000000", "-bitmap-id-range", "1000000", "-seed", "2345", "-client-type", "round_robin", "-bitmap-exponent", "1.5", "-profile-exponent", "1.5"] + } + ] +}