修改centos6源,添加Atomic源

Atomic源支持Fedora,RHEL和CentOS的YUM包管理。这里系统是CentOS6.2。

安装方法很简单:

wget -q -O – http://www.atomicorp.com/installers/atomic | sh

在导入key的界面等待几秒会自动导入,  然后输入yes可以设置成默认源

安装完后在/etc/yum.repos.d/目录会有一个atomic.repo文件。

AB(Apache Benchmark)测试工具使用

安装Apache Benchmark

Install apr-util(need to run ab):

yum install apr-util

Install yum-utils:

yum install yum-utils

Download httpd an extract ab:

mkdir ~/httpd
cd ~/httpd
yumdownloader httpd
rpm2cpio httpd-2.2.3-43.el5.centos.3.i386.rpm | cpio -idmv
mv usr/bin/ab /usr/bin/ab
cd ~
rm -rf ~/httpd

常用命令

  • -c    并发数
  • -n    总请求次数
  • -t     等待最大秒数
  • -s    每个响应最大超时时间

官方文档

执行结果

$ ab -c 10 -n 10000 http://localhost:8080/ 
This is ApacheBench, Version 2.3 <$Revision: 1663405 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking localhost (be patient)
Completed 1000 requests
Completed 2000 requests
Completed 3000 requests
Completed 4000 requests
Completed 5000 requests
Completed 6000 requests
Completed 7000 requests
Completed 8000 requests
Completed 9000 requests
Completed 10000 requests
Finished 10000 requests


Server Software:        
Server Hostname:        localhost
Server Port:            8080

Document Path:          /
Document Length:        0 bytes

Concurrency Level:      10
Time taken for tests:   0.290 seconds
Complete requests:      10000
Failed requests:        0
Total transferred:      1160000 bytes
HTML transferred:       0 bytes
Requests per second:    34461.01 [#/sec] (mean)
Time per request:       0.290 [ms] (mean)
Time per request:       0.029 [ms] (mean, across all concurrent requests)
Transfer rate:          3903.79 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.0      0       2
Processing:     0    0   0.2      0       5
Waiting:        0    0   0.2      0       5
Total:          0    0   0.2      0       5

Percentage of the requests served within a certain time (ms)
  50%      0
  66%      0
  75%      0
  80%      0
  90%      0
  95%      1
  98%      1
  99%      1
 100%      5 (longest request)

 

Golang学习笔记

Go安装

1.linux下编译安装

安装gcc等工具

sudo apt-get install gcc libc6-dev

下载源码,运行all.bash即可

hg clone -u release https://code.google.com/p/go
cd go/src
./all.bash

2.windows下安装

下载.msi文件安装即可

3.环境变量配置

  1. 添加C:Go\bin目录到path (直接使用命令调用go)
  2. 添加C:Go\目录到GOROOT (配置Go安装目录,编译时需要调用扩展包)
  3. 添加C:\Users\wang\Desktop\golang (项目地址,底下包含三个目录bin,pkg,src, src存放源代码)

linux 使用export GOPATH=/home/apple/mygo  添加,或者加入~/.bash_profile(单一用户)

信号处理

通常程序需要监听系统信号来实现平稳退出等操作,golang可以很方便的处理系统信号

import (
	"os/signal"
	"syscall"
	"os"
)

func main() {
	sigs := make(chan os.Signal, 1)
	signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
	<-sigs //阻塞等待信号
}

可以将<-sigs 这一步单独交给一个线程来监控

linux信号列表

$ kill -l
 1) SIGHUP	 2) SIGINT	 3) SIGQUIT	 4) SIGILL	 5) SIGTRAP
 6) SIGABRT	 7) SIGBUS	 8) SIGFPE	 9) SIGKILL	10) SIGUSR1
11) SIGSEGV	12) SIGUSR2	13) SIGPIPE	14) SIGALRM	15) SIGTERM
16) SIGSTKFLT	17) SIGCHLD	18) SIGCONT	19) SIGSTOP	20) SIGTSTP
21) SIGTTIN	22) SIGTTOU	23) SIGURG	24) SIGXCPU	25) SIGXFSZ
26) SIGVTALRM	27) SIGPROF	28) SIGWINCH	29) SIGIO	30) SIGPWR
31) SIGSYS	34) SIGRTMIN	35) SIGRTMIN+1	36) SIGRTMIN+2	37) SIGRTMIN+3
38) SIGRTMIN+4	39) SIGRTMIN+5	40) SIGRTMIN+6	41) SIGRTMIN+7	42) SIGRTMIN+8
43) SIGRTMIN+9	44) SIGRTMIN+10	45) SIGRTMIN+11	46) SIGRTMIN+12	47) SIGRTMIN+13
48) SIGRTMIN+14	49) SIGRTMIN+15	50) SIGRTMAX-14	51) SIGRTMAX-13	52) SIGRTMAX-12
53) SIGRTMAX-11	54) SIGRTMAX-10	55) SIGRTMAX-9	56) SIGRTMAX-8	57) SIGRTMAX-7
58) SIGRTMAX-6	59) SIGRTMAX-5	60) SIGRTMAX-4	61) SIGRTMAX-3	62) SIGRTMAX-2
63) SIGRTMAX-1	64) SIGRTMAX

其中SIGINT 一般为Ctrl+C

参考linux信号列表

Golang原子操作

golang提供了sync/*原子操作方法,其中常用的包括:

只执行一次某个函数Once

package main
import (
    "sync"
)
var once sync.Once
onceFunc(){
    //todo....
}
func main(){
    go  once.Do(onceFunc)
    go  once.Do(onceFunc)
    //sleep ..
}

原子计数操作atomic

package main
import (
    "sync/atomic"
)
var count int32 = 0
func add(){
    atomic.AddInt32(&count, 1)
    Println("count:", atomic.LoadInt32(&count))
}
func main(){
    go add()
    go add()
    //sleep ..
}

等待一组线程结束WaitGroup

package main
import (
    "sync"
)
var count int32 = 0
func EchoNumber(n int){
    Println(n)
}
func main(){
    var wg sync.WaitGroup
	for i := 0; i < 5; i = i + 1 {
		wg.Add(1)
		go func(n int) {
			defer wg.Add(-1)
			EchoNumber(n)
		}(i)
	}
    wg.Wait()
}