var _ = Describe("EventTypeReader", func() {
	var (
		receivedCounter   *metricsreporter.Counter
		reader            *fakeReader
		valueMetricReader *eventtypereader.EventTypeReader
		eventType         events.Envelope_EventType
	)

	BeforeEach(func() {
		receivedCounter = metricsreporter.NewCounter("counter")
		reader = &fakeReader{}
	})

	JustBeforeEach(func() {
		valueMetricReader = eventtypereader.New(receivedCounter, reader, eventType, testOrigin)
	})

	Context("ValueMetrics", func() {
		BeforeEach(func() {
			eventType = events.Envelope_ValueMetric
		})

		It("should report value metrics", func() {
			reader.event = messagegenerator.BasicValueMetricEnvelope(testOrigin)

			valueMetricReader.Read()

			Expect(receivedCounter.GetValue()).To(BeEquivalentTo(1))
		})
Esempio n. 2
0
func main() {
	runtime.GOMAXPROCS(runtime.NumCPU())

	var (
		interval          time.Duration
		writeRate         int
		stopAfter         time.Duration
		concurrentWriters int
		eventType         events.Envelope_EventType
		protocol          string
		serverCert        string
		serverKey         string
		caCert            string
		burstDelay        time.Duration
	)

	flag.Var(newDurationValue(&interval, time.Second), "interval", "Interval for reported results")
	flag.IntVar(&writeRate, "writeRate", 15000, "Number of writes per second to send to metron")
	flag.Var(newDurationValue(&stopAfter, 5*time.Minute), "stopAfter", "How long to run the experiment for")
	flag.IntVar(&concurrentWriters, "concurrentWriters", 1, "Number of concurrent writers")
	flag.Var(newEventTypeValue(&eventType, events.Envelope_ValueMetric), "eventType", "The event type to test")
	flag.StringVar(&protocol, "protocol", "udp", "The protocol to configure metron to send messages over")
	flag.StringVar(&serverCert, "serverCert", "../../integration_tests/fixtures/server.crt", "The server cert file (for TLS connections)")
	flag.StringVar(&serverKey, "serverKey", "../../integration_tests/fixtures/server.key", "The server key file (for TLS connections)")
	flag.StringVar(&caCert, "caCert", "../../integration_tests/fixtures/loggregator-ca.crt", "The certificate authority cert file (for TLS connections)")
	flag.Var(newDurationValue(&burstDelay, 0), "burstDelay", "The delay between burst sequences.  If this is non-zero then writeRate is used as the number of messages to send each burst.")

	flag.Parse()

	reporter := metricsreporter.New(interval, os.Stdout)
	var generator writestrategies.MessageGenerator
	switch eventType {
	case events.Envelope_ValueMetric:
		generator = messagegenerator.NewValueMetricGenerator()
	case events.Envelope_LogMessage:
		generator = messagegenerator.NewLogMessageGenerator("fake-app-id")
	default:
		log.Fatalf("Unsupported envelope type: %v", eventType)
	}
	var dopplerURLs []string
	var reader eventtypereader.MessageReader
	switch protocol {
	case "udp":
		reader = messagereader.NewUDP(3457)
		dopplerURLs = []string{"udp://127.0.0.1:3457"}
	case "tls":
		tlsConfig, err := listeners.NewTLSConfig(
			serverCert,
			serverKey,
			caCert,
		)
		if err != nil {
			log.Printf("Error: failed to load TLS config: %s", err)
			os.Exit(1)
		}
		tlsConfig.InsecureSkipVerify = true
		reader = messagereader.NewTLS(3458, tlsConfig)
		dopplerURLs = []string{"tls://127.0.0.1:3458"}
	default:
		panic(fmt.Errorf("Unknown protocol %s", protocol))
	}
	valueMetricReader := eventtypereader.New(reporter.ReceivedCounter(), reader, eventType, "test-origin")
	exp := experiment.NewExperiment(valueMetricReader)

	for i := 0; i < concurrentWriters; i++ {
		writer := messagewriter.NewMessageWriter("localhost", 51161, "", reporter.SentCounter())
		writeStrategy := chooseStrategy(generator, writer, writeRate, burstDelay)
		exp.AddWriteStrategy(writeStrategy)
	}

	adapter := announceToEtcd(dopplerURLs...)
	defer func() {
		exp.Stop()
		reporter.Stop()
		err := adapter.Delete(nodeKey)
		if err != nil {
			log.Printf("Warning: Failed to delete etcd key %s: %s", nodeKey, err)
		}
		adapter.Disconnect()
	}()

	exp.Warmup()
	go reporter.Start()
	go exp.Start()

	timer := time.NewTimer(stopAfter)
	<-timer.C
}