// Inserts into Elasticsearch the transactions from the channel // Uses batches and the bulk API. func InsertInEsFromChan(es *elasticsearch.Elasticsearch, index string, transactions chan TestTransaction) (int, error) { var buf bytes.Buffer enc := json.NewEncoder(&buf) var insOp struct { Index struct { Type string `json:"_type"` } `json:"index"` } insOp.Index.Type = "trans" flush := func() error { _, err := es.Bulk(index, &buf) if err != nil { return err } buf.Reset() enc = json.NewEncoder(&buf) return nil } i := 0 for trans := range transactions { enc.Encode(insOp) enc.Encode(trans) if i%1000 == 0 { if err := flush(); err != nil { return i, err } } i++ } if err := flush(); err != nil { return i, err } _, err := es.Refresh(index) if err != nil { return i, err } return i, nil }
"field": "count", }, }, "percentile_50th": metrics.MapStr{ "percentiles": metrics.MapStr{ "field": "responsetime", "percents": []float32{50}, }, }, })) }) }) }) Context("With test ES", func() { var es *elasticsearch.Elasticsearch var index_name string var api *PerfDashApi BeforeEach(func() { index_name = fmt.Sprintf("packetbeat-unittest-%v", os.Getpid()) es = elasticsearch.NewElasticsearch() _, err := es.DeleteIndex(index_name) Expect(err).To(BeNil()) es.Refresh(index_name) ts1, _ := elasticsearch.TimeParse("2015-01-02T15:04:05.000Z") ts2, _ := elasticsearch.TimeParse("2015-01-02T15:04:05.001Z") transactions := []testdata.TestTransaction{ testdata.TestTransaction{
} transactions = gen.Generate() }) It("Should generate 1000 points for a second of data", func() { Expect(len(transactions)).To(Equal(1000)) }) It("First transaction should have Service0 and host Service0-Host0", func() { Expect(transactions[0].Service).To(Equal("Service0")) Expect(transactions[0].Host).To(Equal("Service0-Host0")) }) }) Context("generate data in Elasticsearch", func() { var es *elasticsearch.Elasticsearch index_name := fmt.Sprintf("packetbeat-unittest-%v", os.Getpid()) BeforeEach(func() { es = elasticsearch.NewElasticsearch() _, err := es.DeleteIndex(index_name) Expect(err).To(BeNil()) es.Refresh(index_name) }) AfterEach(func() { _, err := es.DeleteIndex(index_name) Expect(err).To(BeNil()) es.Refresh(index_name) })