On Sun, Feb 5, 2017 at 6:53 PM,  <[email protected]> wrote:
> I attached the code. Build and run it, and look the cpu usage.
> Each time when comment out one of serverDone case, the cpu usage will down
> about 5% in my book.
> When all the serverDone case are commented out, the cpu is still about 5%,
> not good compared to nodejs.

I don't see consistent results with the number of cases in the select.
It does seem that the number of cases tends to increase CPU usage
slightly, but it's unpredictable.  I agree that the CPU usage is
noticeable when there no cases, but after all you are starting 1000
goroutines each of which is handling 100 channel events, for a total
of 100,000 events being handled.  Of course that takes some CPU time.
I don't know how long Node.js takes for this kind of operation, but Go
and Node.js are not directly comparable in any case.

I've attached the program I ran for which I see unpredictable results.
It will only work on GNU/Linux since I'm calling clock_gettime
directly.

Ian

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.
package main

import (
	"fmt"
	"log"
	"os"
	"runtime/pprof"
	"sync"
	"syscall"
	"time"
	"unsafe"
)

var serverDone = make(chan struct{})
var serverDone1 = make(chan struct{})
var serverDone2 = make(chan struct{})
var serverDone3 = make(chan struct{})
var serverDone4 = make(chan struct{})
var serverDone5 = make(chan struct{})

func main() {
	f, err := os.Create("cpu.pprof")
	if err != nil {
		log.Fatal(err)
	}
	pprof.StartCPUProfile(f)
	defer pprof.StopCPUProfile()

	run(1, messageLoop1)
	run(2, messageLoop2)
	run(3, messageLoop3)
	run(4, messageLoop4)
	run(5, messageLoop5)
	run(6, messageLoop6)
}

func run(name int, f func()) {
	var start syscall.Timespec
	r, _, errno := syscall.Syscall(syscall.SYS_CLOCK_GETTIME, 2, uintptr(unsafe.Pointer(&start)), 0)
	if r != 0 {
		log.Fatal(errno)
	}

	var wg sync.WaitGroup
	wg.Add(1000)
	for i := 0; i < 1000; i++ {
		go func() {
			f()
			wg.Done()
		}()
	}
	<-time.After(10 * time.Second)
	close(serverDone)

	wg.Wait()

	var end syscall.Timespec
	r, _, errno = syscall.Syscall(syscall.SYS_CLOCK_GETTIME, 2, uintptr(unsafe.Pointer(&end)), 0)
	if r != 0 {
		log.Fatal(errno)
	}
	d := time.Duration(end.Sec - start.Sec) * time.Second + time.Duration(end.Nsec - start.Nsec) * time.Nanosecond
	fmt.Println(name, d)

	serverDone = make(chan struct{})
}

func messageLoop1() {
	var ticker = time.NewTicker(100 * time.Millisecond)
	defer ticker.Stop()
	var counter = 0
	for {
		select {
		case <-serverDone:
			return
		case <-ticker.C:
			counter += 1
		}
	}
}

func messageLoop2() {
	var ticker = time.NewTicker(100 * time.Millisecond)
	defer ticker.Stop()
	var counter = 0
	for {
		select {
		case <-serverDone:
			return
		case <-serverDone1:
		 	return
		case <-ticker.C:
			counter += 1
		}
	}
}

func messageLoop3() {
	var ticker = time.NewTicker(100 * time.Millisecond)
	defer ticker.Stop()
	var counter = 0
	for {
		select {
		case <-serverDone:
			return
		case <-serverDone1:
		 	return
		case <-serverDone2:
			return
		case <-ticker.C:
			counter += 1
		}
	}
}

func messageLoop4() {
	var ticker = time.NewTicker(100 * time.Millisecond)
	defer ticker.Stop()
	var counter = 0
	for {
		select {
		case <-serverDone:
			return
		case <-serverDone1:
		 	return
		case <-serverDone2:
			return
		case <-serverDone3:
			return
		case <-ticker.C:
			counter += 1
		}
	}
}

func messageLoop5() {
	var ticker = time.NewTicker(100 * time.Millisecond)
	defer ticker.Stop()
	var counter = 0
	for {
		select {
		case <-serverDone:
			return
		case <-serverDone1:
		 	return
		case <-serverDone2:
			return
		case <-serverDone3:
			return
		case <-serverDone4:
			return
		case <-ticker.C:
			counter += 1
		}
	}
}

func messageLoop6() {
	var ticker = time.NewTicker(100 * time.Millisecond)
	defer ticker.Stop()
	var counter = 0
	for {
		select {
		case <-serverDone:
			return
		case <-serverDone1:
		 	return
		case <-serverDone2:
			return
		case <-serverDone3:
			return
		case <-serverDone4:
			return
		case <-serverDone5:
			return
		case <-ticker.C:
			counter += 1
		}
	}
}

Reply via email to