示例#1
0
文件: log.go 项目: o1egl/gommon
func initLevels() {
	levels = []string{
		color.Blue("DEBUG"),
		color.Green("INFO"),
		color.Yellow("WARN"),
		color.Red("ERROR"),
		color.RedBg("FATAL"),
	}
}
示例#2
0
func initLevels() {
	levels = []string{
		color.Cyan("TRACE"),
		color.Blue("DEBUG"),
		color.Green("INFO"),
		color.Magenta("NOTICE"),
		color.Yellow("WARN"),
		color.Red("ERROR"),
		color.RedBg("FATAL"),
	}
}
示例#3
0
文件: log.go 项目: mattn/gommon
func (l *Logger) SetOutput(w io.Writer) {
	l.out = w
	l.err = w
	color.Disable()

	switch w := w.(type) {
	case *os.File:
		if isatty.IsTerminal(w.Fd()) {
			color.Enable()
		}
		levels = []string{
			color.Cyan("TRACE"),
			color.Blue("DEBUG"),
			color.Green("INFO"),
			color.Magenta("NOTICE"),
			color.Yellow("WARN"),
			color.Red("ERROR"),
			color.RedBg("FATAL"),
		}
	}
}
示例#4
0
文件: print.go 项目: heshed/boom
func (r *report) printStat(s *stat) {
	sort.Float64s(s.lats)

	var avgSizeTotal int64 = 0
	if len(s.lats) > 0 {
		s.rps = float64(len(s.lats)) / (float64)(s.tick)
		s.average = s.avgTotal / float64(len(s.lats))
		s.fastest = s.lats[0] * 1000
		s.slowest = s.lats[len(s.lats)-1] * 1000
		avgSizeTotal = s.sizeTotal / int64(len(s.lats))
	} else {
		s.fastest = 0
		s.slowest = 0
	}

	var now string
	if s.tick == 30 {
		now = " [ 30Sec ]"
	} else {
		now = time.Now().Local().Round(time.Second).Format("  15:04:05")
	}

	line := fmt.Sprintf("%s   %6.0f     %6.0f       %6d      %6.0f       %6.0f           %d",
		now,
		s.rps,
		s.average*1000,
		s.errorCount.Val(),
		s.fastest,
		s.slowest,
		avgSizeTotal,
	)

	if s.tick == 30 {
		fmt.Println(color.Blue(line))
	} else {
		fmt.Println(line)
	}
}
示例#5
0
文件: boom.go 项目: heshed/boom
func main() {
	flag.Usage = func() {
		fmt.Fprint(os.Stderr, fmt.Sprintf(usage, runtime.NumCPU()))
	}

	flag.Parse()
	if flag.NArg() < 1 {
		usageAndExit("")
	}

	runtime.GOMAXPROCS(*cpus)
	num := *n
	conc := *c
	q := *q

	if conc <= 0 {
		usageAndExit("c cannot be smaller than 1.")
	}

	if num > 0 && num < conc+2 {
		usageAndExit("should n >= c+2.")
	}

	if *bodyFile == "" && num <= 0 {
		num = 50 // default...
	}

	if _, err := os.Stat(*bodyFile); err != nil {
		usageAndExit("-i input file not exist.")
	}

	var (
		url, method, originalHost string
		// Username and password for basic auth
		username, password string
		// request headers
		header http.Header = make(http.Header)
	)

	method = strings.ToUpper(*m)
	url, originalHost = resolveUrl(flag.Args()[0])

	// set content-type
	header.Set("Content-Type", *contentType)
	// set any other additional headers
	if *headers != "" {
		headers := strings.Split(*headers, ";")
		for _, h := range headers {
			match, err := parseInputWithRegexp(h, headerRegexp)
			if err != nil {
				usageAndExit(err.Error())
			}
			header.Set(match[1], match[2])
		}
	}

	if *accept != "" {
		header.Set("Accept", *accept)
	}

	// set basic auth if set
	if *authHeader != "" {
		match, err := parseInputWithRegexp(*authHeader, authRegexp)
		if err != nil {
			usageAndExit(err.Error())
		}
		username, password = match[1], match[2]
	}

	// TODO
	/*
		if *output != "" {
			usageAndExit("Invalid output type.")
		}
	*/

	var proxyURL *gourl.URL
	if *proxyAddr != "" {
		var err error
		proxyURL, err = gourl.Parse(*proxyAddr)
		if err != nil {
			usageAndExit(err.Error())
		}
	}

	abspath, _ := filepath.Abs(*bodyFile)
	// print options
	fmt.Println(color.Blue(":......................................:"))
	fmt.Println(color.Blue("URL:"), url)
	fmt.Println(color.Blue("Method:"), method)
	fmt.Println(color.Blue("Body:"), *body)
	fmt.Println(color.Blue("Header:"), *headers)
	fmt.Println(color.Blue("Username:"******"Password:"******"OriginalHost:"), originalHost)
	fmt.Println(color.Blue("Number of requests to run:"), num)
	fmt.Println(color.Blue("Number of concurrency:"), conc)
	fmt.Println(color.Blue("QPS:"), q)
	fmt.Println(color.Blue("Timeout:"), *t)
	fmt.Println(color.Blue("AllowInsecure:"), *insecure)
	fmt.Println(color.Blue("DisableCompression:"), *disableCompression)
	fmt.Println(color.Blue("DisableKeepAlives:"), *disableKeepAlives)
	fmt.Println(color.Blue("ProxyAddr:"), *proxyAddr)
	fmt.Println(color.Blue("Output:"), *output)
	fmt.Println(color.Blue("BodyFile:"), abspath)
	fmt.Println(color.Blue(":......................................:"))

	reader := boomer.NewReader(*bodyFile)
	(&boomer.Boomer{
		Req: &boomer.ReqOpts{
			Method:       method,
			URL:          url,
			Body:         *body,
			Header:       header,
			Username:     username,
			Password:     password,
			OriginalHost: originalHost,
		},
		N:                  num,
		C:                  conc,
		Qps:                q,
		Timeout:            *t,
		AllowInsecure:      *insecure,
		DisableCompression: *disableCompression,
		DisableKeepAlives:  *disableKeepAlives,
		ProxyAddr:          proxyURL,
		Output:             *output,
		BodyFile:           abspath,
		BodyReader:         reader,
	}).Run()
}