Last active
September 8, 2015 11:13
-
-
Save heshed/f3d6bc3f415150afd918 to your computer and use it in GitHub Desktop.
file create example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"flag" | |
"fmt" | |
"log" | |
"os" | |
"runtime" | |
"sync/atomic" | |
"time" | |
) | |
var ( | |
filePath = flag.String("f", "/tmp/mylog", "output file") | |
duration = flag.Duration("d", time.Second*10, "Duration of test") | |
intervals = flag.Duration("i", time.Millisecond*100, "Intervals of write per second") | |
goroutines = flag.Int("g", 10, "Number of goroutines") | |
) | |
func init() { | |
flag.Parse() | |
} | |
type Counter struct { | |
ops uint64 | |
} | |
func (c *Counter) Inc() { | |
atomic.AddUint64(&c.ops, 1) | |
runtime.Gosched() | |
} | |
func (c *Counter) Val() uint64 { | |
return atomic.LoadUint64(&c.ops) | |
} | |
func check(e error) { | |
if e != nil { | |
log.Panic(e) | |
} | |
} | |
func print_summary() { | |
log.Println("Output :", *filePath) | |
log.Println("Duration :", duration) | |
log.Println("Intervals :", intervals) | |
log.Println("Start!!") | |
} | |
func repeat_somthing() { | |
print_summary() | |
count := &Counter{} | |
ticker := time.NewTicker(*intervals) | |
for i := 0; i < *goroutines; i++ { | |
go func() { | |
for _ = range ticker.C { | |
count.Inc() | |
write_extlog(count.Val()) | |
} | |
}() | |
} | |
noti := time.NewTicker(time.Second * 2) | |
go func() { | |
for _ = range noti.C { | |
log.Print(".") | |
} | |
}() | |
time.Sleep(*duration) | |
ticker.Stop() | |
noti.Stop() | |
print_summary() | |
log.Println("Stop!! Counter", count.Val()) | |
} | |
func write_extlog(number uint64) { | |
f, err := os.OpenFile(*filePath, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0666) | |
check(err) | |
defer f.Close() | |
_, err = fmt.Fprintf(f, "test %d\n", number) | |
check(err) | |
} | |
func main() { | |
repeat_somthing() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment