golang多线程控制,小文件写入测试

最近遇到需要测试共享盘小文件写入的问题,所以自己写了一个程序,测试小文件写入速度,源码如下

package main

import (
	"flag"
	"fmt"
	"io/ioutil"
	"log"
	"os"
	"runtime"
	"time"
)

var (
	limitChan   chan bool 
	t         = time.Tick(time.Second)
)

func watching() {
	fmt.Printf("NumGoroutine: %d\n", runtime.NumGoroutine())
}
func write_file(path string, data []byte) {
	err := ioutil.WriteFile(path, data, os.ModeAppend)
	if err != nil {
		log.Print("write file error:", err.Error())
	}
	defer func() {
		<-limitChan //释放队列
	}()
}
func init() {
	go func() {
		for {
			select {
			case <-t:
				watching() //每秒打印一次goroutine数量
			}
		}
	}()
}
func main() {

	path := flag.String("path", "./testdir", "test path")
	template := flag.String("template", "template.txt", "write file template")
	number := flag.Int("num", 1000, "number of files")
	ChanNum := flag.Int("gonum", 10, "number of goroutine")
	flag.Parse()

	limitChan = make(chan bool, *ChanNum) //重新定义

	filecount, err := ioutil.ReadFile(*template)
	if err != nil {
		log.Fatal("read template error:", err)
	}
	file_len := float32(len(filecount))
	util_mb := float32(1024 * 1024)
	log.Printf("template lengh:%f mb", file_len/util_mb)

	_, err = os.Stat(*path)
	if err != nil {
		os.MkdirAll(*path, os.ModePerm)
	}

	t1 := time.Now().UnixNano()
	i := 1
	var size float32 = 0.00
	for {
		size += file_len
		//ioutil.WriteFile(, filecount, os.ModeAppend)
		limitChan <- true
		go write_file(fmt.Sprintf("%s/%d.txt", *path, i), filecount)
		i++
		if i == *number {
			break
		}
	}
	t2 := time.Now().UnixNano()
	t_size := size / util_mb
	t_ms := (t2 - t1) / 1e6
	t_s := (t2 - t1) / 1e9
	if t_s == 0 {
		t_s = 1
	}
	log.Printf("write lengh:%f mb", t_size)
	log.Printf("this time is:%d ms", t_ms)
	log.Printf("write file speed:%d files of 1 second", int64(*number)/t_s)
}

本程序加入了队列控制goroutine的数量,由于对goroutine数量控制不是很好,之前写了一个小程序进行验证

package main

import (
	"fmt"
	"time"
)

var (
	limt chan bool
	t    = time.Tick(time.Second)
)

func main() {
	go func() {
		for {
			select {
			case <-t:

				for i := 1; i <= 5; i++ {
					<-limt
				}
			}
		}
	}()
	limt = make(chan bool, 10)
	i := 1
	for {
		fmt.Println(i)
		limt <- true
		i++
	}
}

参考goroutine控制
http://blog.csdn.net/yxw2014/article/details/20957429
http://blog.csdn.net/miao0916/article/details/55045894