Golang并发写入多个文件

我需要同时写入多个文件,但是我有这种奇怪的行为,当执行太多并发 go 例程时,写入时间会增加。我正在使用此代码示例重现此行为:


package main


import (

    "fmt"

    "log"

    "time"

    "math/rand"

    "sync"

    "os"

    "io/ioutil"

)


var wg sync.WaitGroup

var mu sync.Mutex


func WriteToFile(data []byte, fileName string) error {

    mu.Lock()

    err := ioutil.WriteFile(fileName, data, 0666)

    if err != nil {

        return err

    }

    return nil

}


func GenerateFile(index int) error {

    defer wg.Done()

    start := time.Now()

    elapsed := time.Since(start)

    buf := make([]byte, 7500000)

    rand.Read(buf) // generate random data

    randomFileName := fmt.Sprintf("/tmp/gotest-%v", rand.Int())

    err := WriteToFile(buf, randomFileName)

    if err != nil {

        return err

    }

    defer os.Remove(randomFileName)

    elapsed = time.Since(start)

    log.Printf("goroutine id: %v, generate file %s done in %s", index, randomFileName, elapsed)

    return nil

}


func main() {

    start := time.Now()

    for i := 0; i < 30; i++ {

        wg.Add(1)

        go GenerateFile(i)

    }

    wg.Wait()

    elapsed := time.Since(start)

    log.Printf("done in %s", elapsed)

}


我希望所有请求应该大致同时完成,因为我正在启动 goroutine,并且会比第一个 goroutine 花费更多的时间。如果我删除数据生成和文件写入部分,请求会同时返回。我也对工人池进行了实验,但如果我添加太多工人,总时间仍然会增加。

我不明白这种行为。有人可以给我一个解释吗?


MMTTMM
浏览 292回答 1
1回答

慕桂英546537

实际上,Go 确实是并行写入磁盘的。问题中顺序行为的原因是math/rand。此包使用通过内部互斥锁实现的线程安全Read随机数生成器:请参阅和globalRndlockedSource这就是为什么你的GenerateFilegoroutines 几乎严格地一个接一个地运行——它们在rand.globalRnd.lk互斥锁上进行内部同步。有两种方法可以提高性能。一种是在每个线程中使用独立的PRNG,另一种是预先生成写入数据。这是一个尝试所有变体的示例程序。package mainimport (&nbsp; &nbsp; "fmt"&nbsp; &nbsp; "io/ioutil"&nbsp; &nbsp; "log"&nbsp; &nbsp; "math/rand"&nbsp; &nbsp; "os"&nbsp; &nbsp; "sort"&nbsp; &nbsp; "sync"&nbsp; &nbsp; "time")var wg sync.WaitGroupconst N = 30var elapsed_g [N]time.Durationfunc SortAndLogElapsed(prefix string) {&nbsp; &nbsp; sort.Slice(elapsed_g[:], func(i, j int) bool { return elapsed_g[i].Nanoseconds() < int64(elapsed_g[j].Nanoseconds()) })&nbsp; &nbsp; for _, elapsed := range elapsed_g {&nbsp; &nbsp; &nbsp; &nbsp; fmt.Println(prefix, elapsed)&nbsp; &nbsp; }}func GenerateFile(start time.Time, id int) error {&nbsp; &nbsp; defer wg.Done()&nbsp; &nbsp; elapsed := time.Since(start)&nbsp; &nbsp; buf := make([]byte, 7500000)&nbsp; &nbsp; rand.Read(buf) // generate random data&nbsp; &nbsp; randomFileName := fmt.Sprintf("/tmp/gotest-%v", rand.Int())&nbsp; &nbsp; err := ioutil.WriteFile(randomFileName, buf, 0666)&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; return err&nbsp; &nbsp; }&nbsp; &nbsp; defer os.Remove(randomFileName)&nbsp; &nbsp; elapsed = time.Since(start)&nbsp; &nbsp; // log.Printf("generate file %s done in %s", randomFileName, elapsed)&nbsp; &nbsp; elapsed_g[id] = elapsed&nbsp; &nbsp; return nil}func RunWithCommonPrng() {&nbsp; &nbsp; start := time.Now()&nbsp; &nbsp; for i := 0; i < N; i++ {&nbsp; &nbsp; &nbsp; &nbsp; wg.Add(1)&nbsp; &nbsp; &nbsp; &nbsp; go GenerateFile(start, i)&nbsp; &nbsp; }&nbsp; &nbsp; wg.Wait()&nbsp; &nbsp; elapsed := time.Since(start)&nbsp; &nbsp; SortAndLogElapsed("common PRNG: ")&nbsp; &nbsp; log.Printf("done in %s", elapsed)}func GenerateFilePrivatePrng(id int, prng rand.Source, start time.Time) error {&nbsp; &nbsp; defer wg.Done()&nbsp; &nbsp; elapsed := time.Since(start)&nbsp; &nbsp; buf := make([]byte, 7500000)&nbsp; &nbsp; rand.New(prng).Read(buf) // generate random data&nbsp; &nbsp; randomFileName := fmt.Sprintf("/tmp/gotest-%v", prng.Int63())&nbsp; &nbsp; err := ioutil.WriteFile(randomFileName, buf, 0666)&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; return err&nbsp; &nbsp; }&nbsp; &nbsp; defer os.Remove(randomFileName)&nbsp; &nbsp; elapsed = time.Since(start)&nbsp; &nbsp; elapsed_g[id] = elapsed&nbsp; &nbsp; // log.Printf("generate file %s with private source: done in %s", randomFileName, elapsed)&nbsp; &nbsp; return nil}func RunWithPrivatePrng() {&nbsp; &nbsp; start := time.Now()&nbsp; &nbsp; for i := 0; i < N; i++ {&nbsp; &nbsp; &nbsp; &nbsp; wg.Add(1)&nbsp; &nbsp; &nbsp; &nbsp; go GenerateFilePrivatePrng(i, rand.NewSource(int64(i)), start)&nbsp; &nbsp; }&nbsp; &nbsp; wg.Wait()&nbsp; &nbsp; elapsed := time.Since(start)&nbsp; &nbsp; SortAndLogElapsed("Private PRNG: ")&nbsp; &nbsp; log.Printf("done in %s", elapsed)}func GenerateFileWithGivenData(id int, buf []byte, start time.Time) error {&nbsp; &nbsp; defer wg.Done()&nbsp; &nbsp; randomFileName := fmt.Sprintf("/tmp/gotest-%v", rand.Int())&nbsp; &nbsp; err := ioutil.WriteFile(randomFileName, buf, 0666)&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; return err&nbsp; &nbsp; }&nbsp; &nbsp; defer os.Remove(randomFileName)&nbsp; &nbsp; elapsed := time.Since(start)&nbsp; &nbsp; elapsed_g[id] = elapsed&nbsp; &nbsp; // log.Printf("generate file %s with data: done in %s", randomFileName, elapsed)&nbsp; &nbsp; return nil}func RunWithCommonData() {&nbsp; &nbsp; buf := make([]byte, 7500000)&nbsp; &nbsp; rand.Read(buf) // generate random data&nbsp; &nbsp; start := time.Now()&nbsp; &nbsp; for i := 0; i < N; i++ {&nbsp; &nbsp; &nbsp; &nbsp; wg.Add(1)&nbsp; &nbsp; &nbsp; &nbsp; go GenerateFileWithGivenData(i, buf, start)&nbsp; &nbsp; }&nbsp; &nbsp; wg.Wait()&nbsp; &nbsp; elapsed := time.Since(start)&nbsp; &nbsp; SortAndLogElapsed("Common data: ")&nbsp; &nbsp; log.Printf("done in %s", elapsed)}func main() {&nbsp; &nbsp; log.Printf("Used CPUs / Max CPUs: %d/%d", runtime.GOMAXPROCS(0), runtime.NumCPU())&nbsp; &nbsp; RunWithCommonPrng()&nbsp; &nbsp; RunWithPrivatePrng()&nbsp; &nbsp; RunWithCommonData()}在 8 核 CPU 和 SSD 上的输出是这样的:2022/10/02 00:00:08 Used CPUs / Max CPUs: 8/8common PRNG:&nbsp; 9.943335mscommon PRNG:&nbsp; 15.12122mscommon PRNG:&nbsp; 20.856216mscommon PRNG:&nbsp; 26.636462mscommon PRNG:&nbsp; 32.041066mscommon PRNG:&nbsp; 37.450744mscommon PRNG:&nbsp; 43.286644mscommon PRNG:&nbsp; 48.695199mscommon PRNG:&nbsp; 54.518533mscommon PRNG:&nbsp; 59.858065mscommon PRNG:&nbsp; 65.620084mscommon PRNG:&nbsp; 71.111171mscommon PRNG:&nbsp; 76.388583mscommon PRNG:&nbsp; 81.609326mscommon PRNG:&nbsp; 87.465878mscommon PRNG:&nbsp; 92.623557mscommon PRNG:&nbsp; 98.35468mscommon PRNG:&nbsp; 103.606529mscommon PRNG:&nbsp; 109.28623mscommon PRNG:&nbsp; 114.981873mscommon PRNG:&nbsp; 120.26626mscommon PRNG:&nbsp; 125.530811mscommon PRNG:&nbsp; 131.222195mscommon PRNG:&nbsp; 136.399946mscommon PRNG:&nbsp; 142.305635mscommon PRNG:&nbsp; 147.687525mscommon PRNG:&nbsp; 153.002392mscommon PRNG:&nbsp; 158.769948mscommon PRNG:&nbsp; 164.241503mscommon PRNG:&nbsp; 169.531355ms2022/10/02 00:00:08 done in 170.273377msPrivate PRNG:&nbsp; 16.255543msPrivate PRNG:&nbsp; 17.155624msPrivate PRNG:&nbsp; 17.477437msPrivate PRNG:&nbsp; 17.49527msPrivate PRNG:&nbsp; 17.521759msPrivate PRNG:&nbsp; 18.363554msPrivate PRNG:&nbsp; 19.800906msPrivate PRNG:&nbsp; 30.340522msPrivate PRNG:&nbsp; 31.551496msPrivate PRNG:&nbsp; 40.583626msPrivate PRNG:&nbsp; 54.682705msPrivate PRNG:&nbsp; 54.832006msPrivate PRNG:&nbsp; 54.983126msPrivate PRNG:&nbsp; 55.143073msPrivate PRNG:&nbsp; 56.517272msPrivate PRNG:&nbsp; 56.577967msPrivate PRNG:&nbsp; 57.718msPrivate PRNG:&nbsp; 58.770033msPrivate PRNG:&nbsp; 59.246808msPrivate PRNG:&nbsp; 59.608246msPrivate PRNG:&nbsp; 59.789123msPrivate PRNG:&nbsp; 60.028814msPrivate PRNG:&nbsp; 68.533662msPrivate PRNG:&nbsp; 69.606317msPrivate PRNG:&nbsp; 69.837988msPrivate PRNG:&nbsp; 71.488161msPrivate PRNG:&nbsp; 71.770842msPrivate PRNG:&nbsp; 72.036881msPrivate PRNG:&nbsp; 72.23509msPrivate PRNG:&nbsp; 73.037337ms2022/10/02 00:00:08 done in 73.694825msCommon data:&nbsp; 5.220506msCommon data:&nbsp; 5.220523msCommon data:&nbsp; 5.220524msCommon data:&nbsp; 5.220526msCommon data:&nbsp; 5.221125msCommon data:&nbsp; 5.221169msCommon data:&nbsp; 5.222472msCommon data:&nbsp; 6.977304msCommon data:&nbsp; 13.601358msCommon data:&nbsp; 13.614532msCommon data:&nbsp; 13.859067msCommon data:&nbsp; 14.75378msCommon data:&nbsp; 16.00253msCommon data:&nbsp; 16.111086msCommon data:&nbsp; 16.263291msCommon data:&nbsp; 16.42076msCommon data:&nbsp; 17.024946msCommon data:&nbsp; 17.313631msCommon data:&nbsp; 17.749351msCommon data:&nbsp; 18.18497msCommon data:&nbsp; 18.83511msCommon data:&nbsp; 21.789867msCommon data:&nbsp; 22.308659msCommon data:&nbsp; 22.308701msCommon data:&nbsp; 22.546815msCommon data:&nbsp; 23.298865msCommon data:&nbsp; 23.482138msCommon data:&nbsp; 23.610855msCommon data:&nbsp; 23.667347msCommon data:&nbsp; 24.500486ms2022/10/02 00:00:08 done in 25.205652ms“公共数据”用于预生成的缓冲区。它表明 Golang 确实并行写入磁盘。它在线程之间分配 goroutines,这些 goroutines 占用 CPU 核心,直到 I/O 完成。更新这是打印 Linux 线程 ID 和 CPU 编号的代码。package main/*#define _GNU_SOURCE#include <sched.h>*/import "C"import (&nbsp; &nbsp; "fmt"&nbsp; &nbsp; "io/ioutil"&nbsp; &nbsp; "log"&nbsp; &nbsp; "math/rand"&nbsp; &nbsp; "os"&nbsp; &nbsp; "runtime"&nbsp; &nbsp; "sort"&nbsp; &nbsp; "sync"&nbsp; &nbsp; "syscall"&nbsp; &nbsp; "time"&nbsp; &nbsp; "github.com/pkg/profile")func GetCpu() int {&nbsp; &nbsp; var ret C.int = C.sched_getcpu()&nbsp; &nbsp; return int(ret)}func GetThreadId() int {&nbsp; &nbsp; return syscall.Gettid()}var wg sync.WaitGroupconst N = 30var elapsed_g [N]time.Durationfunc SortAndLogElapsed(prefix string) {&nbsp; &nbsp; sort.Slice(elapsed_g[:], func(i, j int) bool { return elapsed_g[i].Nanoseconds() < int64(elapsed_g[j].Nanoseconds()) })&nbsp; &nbsp; for _, elapsed := range elapsed_g {&nbsp; &nbsp; &nbsp; &nbsp; fmt.Println(prefix, elapsed)&nbsp; &nbsp; }}func GenerateFileWithGivenData(id int, buf []byte, start time.Time) error {&nbsp; &nbsp; defer wg.Done()&nbsp; &nbsp; randomFileName := fmt.Sprintf("/tmp/gotest-%v", rand.Int())&nbsp; &nbsp; tid := GetThreadId()&nbsp; &nbsp; cpu := GetCpu()&nbsp; &nbsp; before := time.Now()&nbsp; &nbsp; fmt.Printf("Before WriteFile:\t----\t%d\t%d\t%d\t%s\n", id, tid, cpu, before.String())&nbsp; &nbsp; err := ioutil.WriteFile(randomFileName, buf, 0666)&nbsp; &nbsp; after := time.Now()&nbsp; &nbsp; tid = GetThreadId()&nbsp; &nbsp; cpu = GetCpu()&nbsp; &nbsp; fmt.Printf("After WriteFile:\t%d\t%d\t%d\t%d\t%s\n", after.Sub(before).Microseconds(), id, tid, cpu, after.String())&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; return err&nbsp; &nbsp; }&nbsp; &nbsp; defer os.Remove(randomFileName)&nbsp; &nbsp; elapsed := time.Since(start)&nbsp; &nbsp; elapsed_g[id] = elapsed&nbsp; &nbsp; // log.Printf("generate file %s with data: done in %s", randomFileName, elapsed)&nbsp; &nbsp; return nil}func RunWithCommonData() {&nbsp; &nbsp; buf := make([]byte, 7500000)&nbsp; &nbsp; rand.Read(buf) // generate random data&nbsp; &nbsp; fmt.Printf("&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \tElapsed\tG\tTID\tCPU\ttime\n")&nbsp; &nbsp; start := time.Now()&nbsp; &nbsp; println("")&nbsp; &nbsp; for i := 0; i < N; i++ {&nbsp; &nbsp; &nbsp; &nbsp; wg.Add(1)&nbsp; &nbsp; &nbsp; &nbsp; go GenerateFileWithGivenData(i, buf, start)&nbsp; &nbsp; }&nbsp; &nbsp; wg.Wait()&nbsp; &nbsp; elapsed := time.Since(start)&nbsp; &nbsp; SortAndLogElapsed("Common data: ")&nbsp; &nbsp; log.Printf("done in %s", elapsed)}func main() {&nbsp; &nbsp; log.Printf("Used CPUs / Max CPUs: %d/%d", runtime.GOMAXPROCS(0), runtime.NumCPU())&nbsp; &nbsp; // RunWithCommonPrng()&nbsp; &nbsp; // RunWithPrivatePrng()&nbsp; &nbsp; defer profile.Start(profile.CPUProfile).Stop()&nbsp; &nbsp; RunWithCommonData()}我系统的输出是(G internal goroutine ID, TID - Linux thread id, CPU - CPU number, last column is elapsed time)按时间排序:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Elapsed&nbsp; &nbsp; G&nbsp; TID&nbsp; &nbsp; &nbsp;CPU&nbsp; &nbsp; &nbsp;timeBefore WriteFile:&nbsp; &nbsp;----&nbsp; &nbsp; 29&nbsp; 23379&nbsp; &nbsp;0&nbsp; &nbsp;2022-10-03 20:24:47.35247545 +0900 KST m=+0.006016977Before WriteFile:&nbsp; &nbsp;----&nbsp; &nbsp; 0&nbsp; &nbsp;23380&nbsp; &nbsp;1&nbsp; &nbsp;2022-10-03 20:24:47.352475589 +0900 KST m=+0.006017128Before WriteFile:&nbsp; &nbsp;----&nbsp; &nbsp; 14&nbsp; 23383&nbsp; &nbsp;7&nbsp; &nbsp;2022-10-03 20:24:47.352506383 +0900 KST m=+0.006047950Before WriteFile:&nbsp; &nbsp;----&nbsp; &nbsp; 7&nbsp; &nbsp;23381&nbsp; &nbsp;2&nbsp; &nbsp;2022-10-03 20:24:47.352572666 +0900 KST m=+0.006114235Before WriteFile:&nbsp; &nbsp;----&nbsp; &nbsp; 10&nbsp; 23377&nbsp; &nbsp;6&nbsp; &nbsp;2022-10-03 20:24:47.352634156 +0900 KST m=+0.006175692Before WriteFile:&nbsp; &nbsp;----&nbsp; &nbsp; 8&nbsp; &nbsp;23384&nbsp; &nbsp;4&nbsp; &nbsp;2022-10-03 20:24:47.352727575 +0900 KST m=+0.006269119Before WriteFile:&nbsp; &nbsp;----&nbsp; &nbsp; 9&nbsp; &nbsp;23385&nbsp; &nbsp;5&nbsp; &nbsp;2022-10-03 20:24:47.352766795 +0900 KST m=+0.006308348After WriteFile:&nbsp; &nbsp; 4133&nbsp; &nbsp; 14&nbsp; 23383&nbsp; &nbsp;7&nbsp; &nbsp;2022-10-03 20:24:47.356640341 +0900 KST m=+0.010181880After WriteFile:&nbsp; &nbsp; 4952&nbsp; &nbsp; 7&nbsp; &nbsp;23381&nbsp; &nbsp;2&nbsp; &nbsp;2022-10-03 20:24:47.357525386 +0900 KST m=+0.011066917After WriteFile:&nbsp; &nbsp; 5049&nbsp; &nbsp; 29&nbsp; 23379&nbsp; &nbsp;0&nbsp; &nbsp;2022-10-03 20:24:47.357525403 +0900 KST m=+0.011066934After WriteFile:&nbsp; &nbsp; 4758&nbsp; &nbsp; 9&nbsp; &nbsp;23385&nbsp; &nbsp;5&nbsp; &nbsp;2022-10-03 20:24:47.3575254 +0900 KST m=+0.011066928After WriteFile:&nbsp; &nbsp; 4892&nbsp; &nbsp; 10&nbsp; 23377&nbsp; &nbsp;6&nbsp; &nbsp;2022-10-03 20:24:47.357526773 +0900 KST m=+0.011068303After WriteFile:&nbsp; &nbsp; 5051&nbsp; &nbsp; 0&nbsp; &nbsp;23380&nbsp; &nbsp;1&nbsp; &nbsp;2022-10-03 20:24:47.35752678 +0900 KST m=+0.011068311After WriteFile:&nbsp; &nbsp; 4801&nbsp; &nbsp; 8&nbsp; &nbsp;23384&nbsp; &nbsp;4&nbsp; &nbsp;2022-10-03 20:24:47.357529101 +0900 KST m=+0.011070629Before WriteFile:&nbsp; &nbsp;----&nbsp; &nbsp; 12&nbsp; 23380&nbsp; &nbsp;1&nbsp; &nbsp;2022-10-03 20:24:47.357554923 +0900 KST m=+0.011096462Before WriteFile:&nbsp; &nbsp;----&nbsp; &nbsp; 13&nbsp; 23377&nbsp; &nbsp;6&nbsp; &nbsp;2022-10-03 20:24:47.357555161 +0900 KST m=+0.011096695Before WriteFile:&nbsp; &nbsp;----&nbsp; &nbsp; 1&nbsp; &nbsp;23381&nbsp; &nbsp;2&nbsp; &nbsp;2022-10-03 20:24:47.357555163 +0900 KST m=+0.011096697Before WriteFile:&nbsp; &nbsp;----&nbsp; &nbsp; 2&nbsp; &nbsp;23381&nbsp; &nbsp;2&nbsp; &nbsp;2022-10-03 20:24:47.35756292 +0900 KST m=+0.011104452Before WriteFile:&nbsp; &nbsp;----&nbsp; &nbsp; 11&nbsp; 23377&nbsp; &nbsp;6&nbsp; &nbsp;2022-10-03 20:24:47.3575642 +0900 KST m=+0.011105730Before WriteFile:&nbsp; &nbsp;----&nbsp; &nbsp; 21&nbsp; 23385&nbsp; &nbsp;5&nbsp; &nbsp;2022-10-03 20:24:47.357570038 +0900 KST m=+0.011111568Before WriteFile:&nbsp; &nbsp;----&nbsp; &nbsp; 25&nbsp; 23383&nbsp; &nbsp;7&nbsp; &nbsp;2022-10-03 20:24:47.357572217 +0900 KST m=+0.011113747Before WriteFile:&nbsp; &nbsp;----&nbsp; &nbsp; 26&nbsp; 23379&nbsp; &nbsp;0&nbsp; &nbsp;2022-10-03 20:24:47.358768915 +0900 KST m=+0.012310542Before WriteFile:&nbsp; &nbsp;----&nbsp; &nbsp; 27&nbsp; 23384&nbsp; &nbsp;4&nbsp; &nbsp;2022-10-03 20:24:47.361560776 +0900 KST m=+0.015102306After WriteFile:&nbsp; &nbsp; 4020&nbsp; &nbsp; 25&nbsp; 23383&nbsp; &nbsp;7&nbsp; &nbsp;2022-10-03 20:24:47.361593063 +0900 KST m=+0.015134592After WriteFile:&nbsp; &nbsp; 4873&nbsp; &nbsp; 12&nbsp; 23380&nbsp; &nbsp;1&nbsp; &nbsp;2022-10-03 20:24:47.362428015 +0900 KST m=+0.015969540After WriteFile:&nbsp; &nbsp; 4858&nbsp; &nbsp; 21&nbsp; 23385&nbsp; &nbsp;5&nbsp; &nbsp;2022-10-03 20:24:47.362428103 +0900 KST m=+0.015969632After WriteFile:&nbsp; &nbsp; 4865&nbsp; &nbsp; 2&nbsp; &nbsp;23381&nbsp; &nbsp;2&nbsp; &nbsp;2022-10-03 20:24:47.362428238 +0900 KST m=+0.015969769After WriteFile:&nbsp; &nbsp; 4864&nbsp; &nbsp; 11&nbsp; 23377&nbsp; &nbsp;6&nbsp; &nbsp;2022-10-03 20:24:47.362428347 +0900 KST m=+0.015969877Before WriteFile:&nbsp; &nbsp;----&nbsp; &nbsp; 15&nbsp; 23385&nbsp; &nbsp;5&nbsp; &nbsp;2022-10-03 20:24:47.362454039 +0900 KST m=+0.015995570Before WriteFile:&nbsp; &nbsp;----&nbsp; &nbsp; 28&nbsp; 23380&nbsp; &nbsp;1&nbsp; &nbsp;2022-10-03 20:24:47.362454041 +0900 KST m=+0.015995573Before WriteFile:&nbsp; &nbsp;----&nbsp; &nbsp; 23&nbsp; 23377&nbsp; &nbsp;6&nbsp; &nbsp;2022-10-03 20:24:47.362454121 +0900 KST m=+0.015995651Before WriteFile:&nbsp; &nbsp;----&nbsp; &nbsp; 16&nbsp; 23385&nbsp; &nbsp;5&nbsp; &nbsp;2022-10-03 20:24:47.362462845 +0900 KST m=+0.016004374Before WriteFile:&nbsp; &nbsp;----&nbsp; &nbsp; 22&nbsp; 23377&nbsp; &nbsp;6&nbsp; &nbsp;2022-10-03 20:24:47.362479715 +0900 KST m=+0.016021242After WriteFile:&nbsp; &nbsp; 4902&nbsp; &nbsp; 26&nbsp; 23379&nbsp; &nbsp;0&nbsp; &nbsp;2022-10-03 20:24:47.363671623 +0900 KST m=+0.017213150Before WriteFile:&nbsp; &nbsp;----&nbsp; &nbsp; 18&nbsp; 23386&nbsp; &nbsp;6&nbsp; &nbsp;2022-10-03 20:24:47.365182522 +0900 KST m=+0.018724057After WriteFile:&nbsp; &nbsp; 8764&nbsp; &nbsp; 13&nbsp; 23383&nbsp; &nbsp;7&nbsp; &nbsp;2022-10-03 20:24:47.366320071 +0900 KST m=+0.019861611Before WriteFile:&nbsp; &nbsp;----&nbsp; &nbsp; 17&nbsp; 23379&nbsp; &nbsp;0&nbsp; &nbsp;2022-10-03 20:24:47.366374805 +0900 KST m=+0.019916338After WriteFile:&nbsp; &nbsp; 4902&nbsp; &nbsp; 27&nbsp; 23384&nbsp; &nbsp;4&nbsp; &nbsp;2022-10-03 20:24:47.366463028 +0900 KST m=+0.020004556After WriteFile:&nbsp; &nbsp; 4729&nbsp; &nbsp; 28&nbsp; 23380&nbsp; &nbsp;1&nbsp; &nbsp;2022-10-03 20:24:47.367183315 +0900 KST m=+0.020724852After WriteFile:&nbsp; &nbsp; 4720&nbsp; &nbsp; 16&nbsp; 23385&nbsp; &nbsp;5&nbsp; &nbsp;2022-10-03 20:24:47.367183317 +0900 KST m=+0.020724850Before WriteFile:&nbsp; &nbsp;----&nbsp; &nbsp; 19&nbsp; 23385&nbsp; &nbsp;5&nbsp; &nbsp;2022-10-03 20:24:47.367230069 +0900 KST m=+0.020771602Before WriteFile:&nbsp; &nbsp;----&nbsp; &nbsp; 20&nbsp; 23384&nbsp; &nbsp;4&nbsp; &nbsp;2022-10-03 20:24:47.367748633 +0900 KST m=+0.021290163Before WriteFile:&nbsp; &nbsp;----&nbsp; &nbsp; 3&nbsp; &nbsp;23391&nbsp; &nbsp;3&nbsp; &nbsp;2022-10-03 20:24:47.368046383 +0900 KST m=+0.021587923Before WriteFile:&nbsp; &nbsp;----&nbsp; &nbsp; 5&nbsp; &nbsp;23388&nbsp; &nbsp;1&nbsp; &nbsp;2022-10-03 20:24:47.36857915 +0900 KST m=+0.022120682Before WriteFile:&nbsp; &nbsp;----&nbsp; &nbsp; 4&nbsp; &nbsp;23380&nbsp; &nbsp;1&nbsp; &nbsp;2022-10-03 20:24:47.368590097 +0900 KST m=+0.022131628Before WriteFile:&nbsp; &nbsp;----&nbsp; &nbsp; 6&nbsp; &nbsp;23393&nbsp; &nbsp;2&nbsp; &nbsp;2022-10-03 20:24:47.370493582 +0900 KST m=+0.024035118After WriteFile:&nbsp; &nbsp; 10260&nbsp; &nbsp;22&nbsp; 23377&nbsp; &nbsp;6&nbsp; &nbsp;2022-10-03 20:24:47.372740578 +0900 KST m=+0.026282112After WriteFile:&nbsp; &nbsp; 5326&nbsp; &nbsp; 20&nbsp; 23384&nbsp; &nbsp;4&nbsp; &nbsp;2022-10-03 20:24:47.37307519 +0900 KST m=+0.026616720After WriteFile:&nbsp; &nbsp; 10922&nbsp; &nbsp;23&nbsp; 23387&nbsp; &nbsp;0&nbsp; &nbsp;2022-10-03 20:24:47.373376163 +0900 KST m=+0.026917695After WriteFile:&nbsp; &nbsp; 5613&nbsp; &nbsp; 3&nbsp; &nbsp;23391&nbsp; &nbsp;3&nbsp; &nbsp;2022-10-03 20:24:47.373660058 +0900 KST m=+0.027201605After WriteFile:&nbsp; &nbsp; 5332&nbsp; &nbsp; 4&nbsp; &nbsp;23380&nbsp; &nbsp;1&nbsp; &nbsp;2022-10-03 20:24:47.373922339 +0900 KST m=+0.027463865After WriteFile:&nbsp; &nbsp; 8871&nbsp; &nbsp; 18&nbsp; 23377&nbsp; &nbsp;6&nbsp; &nbsp;2022-10-03 20:24:47.374053982 +0900 KST m=+0.027595513After WriteFile:&nbsp; &nbsp; 7880&nbsp; &nbsp; 17&nbsp; 23384&nbsp; &nbsp;4&nbsp; &nbsp;2022-10-03 20:24:47.374255159 +0900 KST m=+0.027796694After WriteFile:&nbsp; &nbsp; 12127&nbsp; &nbsp;15&nbsp; 23387&nbsp; &nbsp;0&nbsp; &nbsp;2022-10-03 20:24:47.37458126 +0900 KST m=+0.028122790After WriteFile:&nbsp; &nbsp; 7422&nbsp; &nbsp; 19&nbsp; 23391&nbsp; &nbsp;3&nbsp; &nbsp;2022-10-03 20:24:47.374652483 +0900 KST m=+0.028194020Before WriteFile:&nbsp; &nbsp;----&nbsp; &nbsp; 24&nbsp; 23377&nbsp; &nbsp;6&nbsp; &nbsp;2022-10-03 20:24:47.375338247 +0900 KST m=+0.028879777After WriteFile:&nbsp; &nbsp; 5111&nbsp; &nbsp; 6&nbsp; &nbsp;23393&nbsp; &nbsp;2&nbsp; &nbsp;2022-10-03 20:24:47.375605341 +0900 KST m=+0.029146871After WriteFile:&nbsp; &nbsp; 19459&nbsp; &nbsp;1&nbsp; &nbsp;23392&nbsp; &nbsp;5&nbsp; &nbsp;2022-10-03 20:24:47.377014458 +0900 KST m=+0.030555986After WriteFile:&nbsp; &nbsp; 3847&nbsp; &nbsp; 24&nbsp; 23377&nbsp; &nbsp;6&nbsp; &nbsp;2022-10-03 20:24:47.379185393 +0900 KST m=+0.032726920After WriteFile:&nbsp; &nbsp; 10778&nbsp; &nbsp;5&nbsp; &nbsp;23388&nbsp; &nbsp;0&nbsp; &nbsp;2022-10-03 20:24:47.379358058 +0900 KST m=+0.032899584它表明 goroutines 在我的 8 核 CPU 的所有内核上的许多不同线程中运行。看起来最快的 IO 是在那些保留了线程和 CPU 的 goroutine 中。而且似乎停放/取消停放线程会使阻塞 IO 变慢。我用 100 个 goroutines 运行相同的代码。最坏的情况有 60 毫秒那么大,但它不是最后一个,中间的一个。即使在最后,也有 5.5 毫秒的快速写入。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go