Skip to content
quarry Artifact [c61bbdc2e7]
Login

Artifact [c61bbdc2e7]

Artifact c61bbdc2e724fa98c38fd6d55b97483caa0eb8ee44bf44320a530537f6fce928:


package index

import (
	"fmt"
	"path/filepath"
	"testing"
	"time"
)

// mergePairIndex is the shape of a large mirror: trunk a long chain of
// check-ins, and hundreds of open branches that each grew a few
// check-ins off some point of it, as the merge page's pair is chosen
// from. Returns the newest branch's name.
func mergePairIndex(b *testing.B) (*Index, string) {
	b.Helper()
	ix, err := Open(filepath.Join(b.TempDir(), "index.db"))
	if err != nil {
		b.Fatal(err)
	}
	b.Cleanup(func() { ix.Close() })
	const trunk, branches = 30000, 400
	start := time.Date(2012, 1, 1, 0, 0, 0, 0, time.UTC)
	at := func(i int) string {
		return start.Add(time.Duration(i) * 4 * time.Hour).Format("2006-01-02T15:04:05.000")
	}
	tx, err := ix.db.Begin()
	if err != nil {
		b.Fatal(err)
	}
	ci, _ := tx.Prepare(`INSERT INTO checkin (hash, date, user, comment, branch) VALUES (?, ?, 'u', '', ?)`)
	par, _ := tx.Prepare(`INSERT INTO checkin_parent (child, parent, ord) VALUES (?, ?, 0)`)
	br, _ := tx.Prepare(`INSERT INTO branch (name, tip, date, closed) VALUES (?, ?, ?, 0)`)
	for i := 0; i < trunk; i++ {
		h := fmt.Sprintf("t%07d", i)
		if _, err := ci.Exec(h, at(i), "trunk"); err != nil {
			b.Fatal(err)
		}
		if i > 0 {
			if _, err := par.Exec(h, fmt.Sprintf("t%07d", i-1)); err != nil {
				b.Fatal(err)
			}
		}
	}
	if _, err := br.Exec("trunk", fmt.Sprintf("t%07d", trunk-1), at(trunk-1)); err != nil {
		b.Fatal(err)
	}
	newest := ""
	for k := 0; k < branches; k++ {
		name := fmt.Sprintf("feat-%03d", k)
		off := (k + 1) * (trunk - 200) / branches
		prev := fmt.Sprintf("t%07d", off)
		var h string
		for j := 0; j < 3; j++ {
			h = fmt.Sprintf("b%03d%02d", k, j)
			if _, err := ci.Exec(h, at(off+j+1), name); err != nil {
				b.Fatal(err)
			}
			if _, err := par.Exec(h, prev); err != nil {
				b.Fatal(err)
			}
			prev = h
		}
		if _, err := br.Exec(name, h, at(off+3)); err != nil {
			b.Fatal(err)
		}
		newest = name
	}
	if err := tx.Commit(); err != nil {
		b.Fatal(err)
	}
	return ix, newest
}

// What one merge.conflicts call asks the index on a repeat request, each
// on its own, then together as the page's request does.
func BenchmarkMergePairIndex(b *testing.B) {
	ix, branch := mergePairIndex(b)
	b.Run("BranchTip_trunk", func(b *testing.B) {
		for b.Loop() {
			ix.BranchTip("trunk")
		}
	})
	b.Run("BranchTip_branch", func(b *testing.B) {
		for b.Loop() {
			ix.BranchTip(branch)
		}
	})
	b.Run("Behind", func(b *testing.B) {
		for b.Loop() {
			ix.Behind(branch, "trunk")
		}
	})
	b.Run("BranchesLimited200", func(b *testing.B) {
		for b.Loop() {
			ix.BranchesLimited(200, true)
		}
	})
	b.Run("TipOf_trunk", func(b *testing.B) {
		for b.Loop() {
			ix.TipOf("trunk")
		}
	})
}