Ejemplo n.º 1
0
// Create a new Heka logging interface.
func NewHekaLogger(conf JsMap) *HekaLogger {
	//Preflight
	var ok bool
	var encoder client.Encoder = nil
	var sender client.Sender = nil
	var logname string = ""
	var err error
	var tracer bool = false
	var filter int64

	pid := int32(os.Getpid())

	if _, ok = conf["heka.sender"]; !ok {
		conf["heka.sender"] = "tcp"
	}
	if _, ok = conf["heka.server_addr"]; !ok {
		conf["heka.server_addr"] = "127.0.0.1:5565"
	}
	if _, ok = conf["heka.logger_name"]; !ok {
		conf["heka.logger_name"] = "simplepush"
	}
	if _, ok = conf["heka.current_host"]; !ok {
		conf["heka.current_host"], _ = os.Hostname()
	}
	if _, ok = conf["heka.show_caller"]; ok {
		tracer, _ = strconv.ParseBool(conf["heka.show_caller"].(string))
	}
	filter, _ = strconv.ParseInt(MzGet(conf, "logger.filter", "10"), 0, 0)
	if MzGetFlag(conf, "heka.use") {
		encoder = client.NewJsonEncoder(nil)
		sender, err = client.NewNetworkSender(conf["heka.sender"].(string),
			conf["heka.server_addr"].(string))
		if err != nil {
			log.Panic("Could not create sender ", err)
		}
		logname = conf["heka.logger_name"].(string)
	}
	return &HekaLogger{encoder: encoder,
		sender:   sender,
		logname:  logname,
		pid:      pid,
		hostname: conf["heka.current_host"].(string),
		conf:     conf,
		tracer:   tracer,
		filter:   filter}
}
Ejemplo n.º 2
0
func NewHekaLogger(conf JsMap) *HekaLogger {
	//Preflight
	var ok bool
	var encoder client.Encoder
	var sender client.Sender
	var logname string
	var err error
	pid := int32(os.Getpid())
	hostname, _ := os.Hostname()
	encoder = nil
	sender = nil
	logname = ""

	if _, ok = conf["heka_sender"]; !ok {
		conf["heka_sender"] = "tcp"
	}
	if _, ok = conf["heka_server_addr"]; !ok {
		conf["heka_server_addr"] = "127.0.0.1:5565"
	}
	if _, ok = conf["heka_logger_name"]; !ok {
		conf["heka_logger_name"] = "simplepush"
	}
	if _, ok = conf["heka_no"]; !ok {
		encoder = client.NewJsonEncoder(nil)
		sender, err = client.NewNetworkSender(conf["heka_sender"].(string),
			conf["heka_server_addr"].(string))
		if err != nil {
			log.Panic("Could not create sender ", err)
		}
		logname = conf["heka_logger_name"].(string)
	}
	return &HekaLogger{encoder: encoder,
		sender:   sender,
		logname:  logname,
		pid:      pid,
		hostname: hostname}
}
Ejemplo n.º 3
0
func main() {
	configFile := flag.String("config", "flood.toml", "Flood configuration file")
	configTest := flag.String("test", "default", "Test section to load")

	flag.Parse()

	if flag.NFlag() == 0 {
		flag.PrintDefaults()
		os.Exit(0)
	}

	var config FloodConfig
	if _, err := toml.DecodeFile(*configFile, &config); err != nil {
		log.Printf("Error decoding config file: %s", err)
		return
	}
	var test FloodTest
	var ok bool
	if test, ok = config[*configTest]; !ok {
		log.Printf("Configuration test: '%s' was not found", *configTest)
		return
	}

	if test.PprofFile != "" {
		profFile, err := os.Create(test.PprofFile)
		if err != nil {
			log.Fatalln(err)
		}
		pprof.StartCPUProfile(profFile)
		defer pprof.StopCPUProfile()
	}

	sender, err := client.NewNetworkSender(test.Sender, test.IpAddress)
	if err != nil {
		log.Fatalf("Error creating sender: %s\n", err.Error())
	}

	var unsignedEncoder client.Encoder
	var signedEncoder client.Encoder
	switch test.Encoder {
	case "json":
		unsignedEncoder = client.NewJsonEncoder(nil)
		signedEncoder = client.NewJsonEncoder(&test.Signer)
	case "protobuf":
		unsignedEncoder = client.NewProtobufEncoder(nil)
		signedEncoder = client.NewProtobufEncoder(&test.Signer)
	}

	var numTestMessages = 1
	var unsignedMessages [][]byte
	var signedMessages [][]byte

	rdm := &randomDataMaker{
		src:       rand.NewSource(time.Now().UnixNano()),
		asciiOnly: test.AsciiOnly,
	}

	if test.VariableSizeMessages {
		numTestMessages = 64
		unsignedMessages = makeVariableMessage(unsignedEncoder, numTestMessages, rdm)
		signedMessages = makeVariableMessage(signedEncoder, numTestMessages, rdm)
	} else {
		if test.StaticMessageSize == 0 {
			test.StaticMessageSize = 1000
		}
		unsignedMessages = makeFixedMessage(unsignedEncoder, test.StaticMessageSize,
			rdm)
		signedMessages = makeFixedMessage(signedEncoder, test.StaticMessageSize,
			rdm)
	}
	// wait for sigint
	sigChan := make(chan os.Signal, 1)
	signal.Notify(sigChan, syscall.SIGINT)
	var msgsSent, bytesSent uint64
	var corruptPercentage, lastCorruptPercentage, signedPercentage, lastSignedPercentage float64
	var corrupt bool

	// set up counter loop
	ticker := time.NewTicker(time.Duration(time.Second))
	go timerLoop(&msgsSent, &bytesSent, ticker)

	test.CorruptPercentage /= 100.0
	test.SignedPercentage /= 100.0

	var buf []byte
	for gotsigint := false; !gotsigint; {
		runtime.Gosched()
		select {
		case <-sigChan:
			gotsigint = true
			continue
		default:
		}
		msgId := rand.Int() % numTestMessages
		corruptPercentage = math.Floor(float64(msgsSent) * test.CorruptPercentage)
		if corruptPercentage != lastCorruptPercentage {
			lastCorruptPercentage = corruptPercentage
			corrupt = true
		} else {
			corrupt = false
		}
		signedPercentage = math.Floor(float64(msgsSent) * test.SignedPercentage)
		if signedPercentage != lastSignedPercentage {
			lastSignedPercentage = signedPercentage
			buf = signedMessages[msgId]
		} else {
			buf = unsignedMessages[msgId]
		}
		bytesSent += uint64(len(buf))
		err = sendMessage(sender, buf, corrupt)
		if err != nil {
			if !strings.Contains(err.Error(), "connection refused") {
				log.Printf("Error sending message: %s\n",
					err.Error())
			}
		} else {
			msgsSent++
			if test.NumMessages != 0 && msgsSent >= test.NumMessages {
				break
			}
		}
	}
	sender.Close()
	log.Println("Clean shutdown: ", msgsSent, " messages sent")
}