cmd

package
v0.0.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 6, 2017 License: MIT Imports: 20 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var BenchCmd = &cobra.Command{
	Use:   "xkb",
	Short: "Xephon K Benchmark",
	Long:  "xkb is the bechmark tool for Xephon K",
	Run: func(cmd *cobra.Command, args []string) {
		fmt.Printf("Xephon K Bench %s \n", pkg.Version)
		targetDB := bench.DBXephonK

		if strings.HasPrefix(db, "x") {
			targetDB = bench.DBXephonK
		} else if strings.HasPrefix(db, "i") {
			targetDB = bench.DBInfluxDB
		} else if strings.HasPrefix(db, "k") {
			targetDB = bench.DBKairosDB
		} else {
			log.Fatalf("unsupported target db %s", db)
			return
		}
		config := loader.NewConfig(targetDB)
		config.WorkerNum = concurrency
		config.BatchSize = batchSize
		config.Duration = time.Duration(duration) * time.Second
		config.Timeout = time.Duration(timeout) * time.Second
		fmt.Print(config)
		if !yes {
			fmt.Print("Do you want to proceed? [Y/N]")
			var choice string

			fmt.Scanf("%s", &choice)
			if strings.ToLower(choice) == "n" {
				fmt.Print("you said no, bye~\n")
				return
			}
		}
		loader := loader.NewHTTPLoader(config, &reporter.BasicReporter{})
		loader.Run()

		fmt.Print(config)
	},
}
View Source
var CollectorCmd = &cobra.Command{
	Use:   "xkc",
	Short: "Xephon K Collector",
	Long:  "xkc is the metrics collector for Xephon K",
	Run: func(cmd *cobra.Command, args []string) {
		targetDB := bench.DBXephonK

		if strings.HasPrefix(db, "x") {
			targetDB = bench.DBXephonK
		} else if strings.HasPrefix(db, "i") {
			targetDB = bench.DBInfluxDB
		} else if strings.HasPrefix(db, "k") {
			targetDB = bench.DBKairosDB
		} else {
			log.Fatalf("unsupported target db %s", db)
			return
		}

		client := http.Client{}
		var baseReq *http.Request
		var serializer serialize.Serializer
		switch targetDB {
		case bench.DBInfluxDB:
			req, err := http.NewRequest("POST", "http://localhost:8086/write?db=sb", nil)
			if err != nil {
				log.Panic(err)
				return
			}
			baseReq = req
			serializer = &serialize.InfluxDBSerialize{}
		case bench.DBXephonK:
			url := fmt.Sprintf("http://localhost:%d/write", server.DefaultPort)
			req, err := http.NewRequest("POST", url, nil)
			if err != nil {
				log.Panic(err)
				return
			}
			baseReq = req
			serializer = &serialize.XephonKSerialize{}
		case bench.DBKairosDB:
			req, err := http.NewRequest("POST", "http://localhost:8080/api/v1/datapoints", nil)
			if err != nil {
				log.Panic(err)
				return
			}
			baseReq = req
			serializer = &serialize.KairosDBSerialize{}
		default:
			log.Panic("unsupported database, no base request avaliable")
			return
		}

		config := collector.NewConfig()
		currentBatchSize := 0
		batchSize := config.BatchSize
		tickChan := time.NewTicker(config.Interval).C

		hostInfo := system.NewHostInfo()
		cpuCollector := system.StatCollector{}
		memCollector := system.MeminfoCollector{}

		enableCPU := true

		metricNames := []string{
			"mem.total", "mem.free",
		}

		cpuMetrics := []string{
			"user", "nice", "system", "idle", "iowait", "irq", "softirq", "steal", "guest", "guestnice",
		}

		cores := make([]string, hostInfo.NumCores+1)
		for i := 0; i < hostInfo.NumCores; i++ {
			cores[i] = fmt.Sprintf("cpu.%d.", i)
		}
		cores[hostInfo.NumCores] = "cpu.total."
		if enableCPU {

			for _, m := range cpuMetrics {
				for _, p := range cores {
					metricNames = append(metricNames, p+m)
				}
			}
		}

		log.Info(metricNames)

		seriesMap := make(map[string]*common.IntSeries, len(metricNames))

		for _, m := range metricNames {
			seriesMap[m] = common.NewIntSeries(m)
		}

		sigChan := make(chan os.Signal, 1)
		signal.Notify(sigChan, os.Interrupt)

		for {
			select {
			case <-sigChan:
				log.Info("you pressed ctrl + c")
				log.Info("this is dummy clean up")
				os.Exit(0)
			case <-tickChan:

				currentTime := time.Now().Unix() * 1000
				log.Debugf("tick %d", currentTime)
				if currentBatchSize >= batchSize {

					log.Info("I should flush now!")
					serializer.Start()
					for _, s := range seriesMap {
						s.Tags["host"] = hostInfo.Hostname

						serializer.WriteInt(*s)
					}
					serializer.End()
					req := new(http.Request)
					*req = *baseReq
					req.Body = serializer.ReadCloser()

					log.Debug(string(serializer.Data()))

					res, err := client.Do(req)
					if err != nil {
						log.Warn(err)
					} else {
						io.Copy(ioutil.Discard, res.Body)
						res.Body.Close()
						log.Info("flushed")
					}
					serializer.Reset()
					currentBatchSize = 0

					for _, m := range metricNames {
						seriesMap[m] = common.NewIntSeries(m)
					}
				} else {
					currentBatchSize++
				}

				cpuCollector.Update()
				memCollector.Update()
				var s *common.IntSeries
				if enableCPU {

					for i, p := range cores {
						var stat system.CPUStat
						if i != hostInfo.NumCores {
							stat = cpuCollector.CPUs[i]
						} else {
							stat = cpuCollector.CPUTotal
						}
						s = seriesMap[p+"user"]

						s.Points = append(s.Points, common.IntPoint{T: currentTime, V: int64(stat.User)})
						s = seriesMap[p+"nice"]
						s.Points = append(s.Points, common.IntPoint{T: currentTime, V: int64(stat.Nice)})
						s = seriesMap[p+"system"]
						s.Points = append(s.Points, common.IntPoint{T: currentTime, V: int64(stat.System)})
						s = seriesMap[p+"idle"]
						s.Points = append(s.Points, common.IntPoint{T: currentTime, V: int64(stat.Idle)})
						s = seriesMap[p+"iowait"]
						s.Points = append(s.Points, common.IntPoint{T: currentTime, V: int64(stat.IOWait)})
						s = seriesMap[p+"irq"]
						s.Points = append(s.Points, common.IntPoint{T: currentTime, V: int64(stat.Irq)})
						s = seriesMap[p+"softirq"]
						s.Points = append(s.Points, common.IntPoint{T: currentTime, V: int64(stat.SoftIrq)})
						s = seriesMap[p+"steal"]
						s.Points = append(s.Points, common.IntPoint{T: currentTime, V: int64(stat.Steal)})
						s = seriesMap[p+"guest"]
						s.Points = append(s.Points, common.IntPoint{T: currentTime, V: int64(stat.Guest)})
						s = seriesMap[p+"guestnice"]
						s.Points = append(s.Points, common.IntPoint{T: currentTime, V: int64(stat.GuestNice)})
					}
				}
				s = seriesMap["mem.total"]
				s.Points = append(s.Points, common.IntPoint{T: currentTime, V: int64(memCollector.MemTotal)})
				s = seriesMap["mem.free"]
				s.Points = append(s.Points, common.IntPoint{T: currentTime, V: int64(memCollector.MemFree)})
				log.Debugf("mem.free length %d", len(s.Points))
			}
		}
	},
}
View Source
var DaemonCmd = &cobra.Command{
	Use:   "xkd",
	Short: "Xephon K Daemon",
	Long:  "xkd is the server daemon for Xephon K",
	Run: func(cmd *cobra.Command, args []string) {
		fmt.Print(banner)
		srv := server.HTTPServer{Port: port, Backend: backend, CassandraHost: cassandraHost, DiskFolder: diskFolder}
		srv.Start()

	},
}

xkd -b disk --folder /home/at15/workspace/tmp

View Source
var SchemaCmd = &cobra.Command{
	Use:   "schema",
	Short: "Create schema",
	Run: func(cmd *cobra.Command, args []string) {

		cassandra.CassandraHost = cassandraHost
		log.Info("create schema for cassandra using default setting")
		cassandra.CreateSchema()
		log.Info("schema created!")
	},
}
View Source
var VersionCmd = &cobra.Command{
	Use:   "version",
	Short: "Show Xephon-K version",
	Run: func(cmd *cobra.Command, args []string) {
		fmt.Println(pkg.Version)
	},
}

Functions

func ExecuteBench

func ExecuteBench()

func ExecuteCollector

func ExecuteCollector()

func ExecuteDaemon

func ExecuteDaemon()

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL