Ejemplo n.º 1
0
func testQueryType(t test.TB, fn func(*queryTest), itype indexType) {
	defer index.SetVerboseCorpusLogging(true)
	index.SetVerboseCorpusLogging(false)

	idx := index.NewMemoryIndex() // string key-value pairs in memory, as if they were on disk
	var err error
	var corpus *index.Corpus
	if itype == indexCorpusBuild {
		if corpus, err = idx.KeepInMemory(); err != nil {
			t.Fatal(err)
		}
	}
	qt := &queryTest{
		t:  t,
		id: indextest.NewIndexDeps(idx),
	}
	qt.id.Fataler = t
	qt.Handler = func() *Handler {
		h := NewHandler(idx, qt.id.SignerBlobRef)
		if itype == indexCorpusScan {
			if corpus, err = idx.KeepInMemory(); err != nil {
				t.Fatal(err)
			}
			idx.PreventStorageAccessForTesting()
		}
		if corpus != nil {
			h.SetCorpus(corpus)
		}
		return h
	}
	fn(qt)
}
Ejemplo n.º 2
0
func BenchmarkCorpusFromStorage(b *testing.B) {
	defer test.TLog(b)()
	buildKvOnce.Do(func() {
		kvForBenchmark = sorted.NewMemoryKeyValue()
		idx := index.New(kvForBenchmark)
		id := indextest.NewIndexDeps(idx)
		id.Fataler = b
		for i := 0; i < 10; i++ {
			fileRef, _ := id.UploadFile("file.txt", fmt.Sprintf("some file %d", i), time.Unix(1382073153, 0))
			pn := id.NewPlannedPermanode(fmt.Sprint(i))
			id.SetAttribute(pn, "camliContent", fileRef.String())
		}
	})
	defer index.SetVerboseCorpusLogging(true)
	index.SetVerboseCorpusLogging(false)

	b.ResetTimer()

	for i := 0; i < b.N; i++ {
		_, err := index.NewCorpusFromStorage(kvForBenchmark)
		if err != nil {
			b.Fatal(err)
		}
	}
}
Ejemplo n.º 3
0
// TestGetPermanodeLocationAllocs helps us making sure we keep
// Handler.getPermanodeLocation (or equivalent), allocation-free.
func TestGetPermanodeLocationAllocs(t *testing.T) {
	defer index.SetVerboseCorpusLogging(true)
	index.SetVerboseCorpusLogging(false)

	idx := index.NewMemoryIndex() // string key-value pairs in memory, as if they were on disk
	idd := indextest.NewIndexDeps(idx)
	h := NewHandler(idx, idd.SignerBlobRef)
	corpus, err := idx.KeepInMemory()
	if err != nil {
		t.Fatal(err)
	}
	h.SetCorpus(corpus)

	pn1 := idd.NewPermanode()
	lat := 45.18
	long := 5.72
	idd.SetAttribute(pn1, "latitude", fmt.Sprintf("%f", lat))
	idd.SetAttribute(pn1, "longitude", fmt.Sprintf("%f", long))

	pnVenue := idd.NewPermanode()
	idd.SetAttribute(pnVenue, "camliNodeType", "foursquare.com:venue")
	idd.SetAttribute(pnVenue, "latitude", fmt.Sprintf("%f", lat))
	idd.SetAttribute(pnVenue, "longitude", fmt.Sprintf("%f", long))
	pnCheckin := idd.NewPermanode()
	idd.SetAttribute(pnCheckin, "camliNodeType", "foursquare.com:checkin")
	idd.SetAttribute(pnCheckin, "foursquareVenuePermanode", pnVenue.String())

	br, _ := idd.UploadFile("photo.jpg", exifFileContentLatLong(lat, long), time.Now())
	pnPhoto := idd.NewPermanode()
	idd.SetAttribute(pnPhoto, "camliContent", br.String())

	const (
		blobParseAlloc = 1 // blob.Parse uses one alloc

		// allocs permitted in different tests
		latLongAttr         = 0 // latitude/longitude attr lookup musn't alloc
		altLocRef           = blobParseAlloc
		camliContentFileLoc = blobParseAlloc
	)

	for _, tt := range []struct {
		title    string
		pn       blob.Ref
		maxAlloc int
	}{
		{"explicit location from attrs", pn1, latLongAttr},
		{"referenced permanode location", pnCheckin, latLongAttr + altLocRef},
		{"location from exif photo", pnPhoto, latLongAttr + camliContentFileLoc},
	} {
		n := testing.AllocsPerRun(20, func() {
			loc, err := h.ExportGetPermanodeLocation(context.TODO(), tt.pn, time.Now())
			if err != nil {
				t.Fatal(err)
			}
			if loc.Latitude != lat {
				t.Fatalf("wrong latitude: got %v, wanted %v", loc.Latitude, lat)
			}
			if loc.Longitude != long {
				t.Fatalf("wrong longitude: got %v, wanted %v", loc.Longitude, long)
			}
		})
		t.Logf("%s: %v allocations (max %v)", tt.title, n, tt.maxAlloc)
		if int(n) != tt.maxAlloc {
			t.Errorf("LocationHandler.PermanodeLocation should not allocate more than %d", tt.maxAlloc)
		}
	}
}