当前位置: 首页>>代码示例>>Golang>>正文


Golang runtime.Gosched函数代码示例

本文整理汇总了Golang中runtime.Gosched函数的典型用法代码示例。如果您正苦于以下问题:Golang Gosched函数的具体用法?Golang Gosched怎么用?Golang Gosched使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了Gosched函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: TestDumpWithLotsOfMessagesAndLargeBuffer

func TestDumpWithLotsOfMessagesAndLargeBuffer(t *testing.T) {
	var bufferSize uint
	bufferSize = 200
	dump := NewDumpSink("myApp", bufferSize, loggertesthelper.Logger())
	dump.Run()

	for i := 0; i < 1000; i++ {
		logMessage := messagetesthelpers.NewMessage(t, strconv.Itoa(i), "appId")
		dump.Channel() <- logMessage
	}

	runtime.Gosched()

	logMessages := dumpAllMessages(dump, bufferSize)
	assert.Equal(t, len(logMessages), 200)
	assert.Equal(t, string(logMessages[0].GetLogMessage().GetMessage()), "800")
	assert.Equal(t, string(logMessages[1].GetLogMessage().GetMessage()), "801")

	for i := 1000; i < 2000; i++ {
		logMessage := messagetesthelpers.NewMessage(t, strconv.Itoa(i), "appId")
		dump.Channel() <- logMessage
	}

	runtime.Gosched()

	logMessages = dumpAllMessages(dump, bufferSize)
	assert.Equal(t, len(logMessages), 200)
	assert.Equal(t, string(logMessages[0].GetLogMessage().GetMessage()), "1800")
	assert.Equal(t, string(logMessages[1].GetLogMessage().GetMessage()), "1801")

	logMessages = dumpAllMessages(dump, bufferSize)
	assert.Equal(t, len(logMessages), 200)
	assert.Equal(t, string(logMessages[0].GetLogMessage().GetMessage()), "1800")
	assert.Equal(t, string(logMessages[1].GetLogMessage().GetMessage()), "1801")
}
开发者ID:uabassguy,项目名称:loggregator,代码行数:35,代码来源:dump_sink_test.go

示例2: TestConcurrency

func TestConcurrency(t *testing.T) {

	uids := make([]UID, 0)
	results := make(chan UID, MAX)

	// Fire off a number of Goroutines to grab a bunch of UIDs each
	for x := 0; x < LOOPS; x++ {
		go func(results chan UID) {
			for x := 0; x < COUNT_PER_LOOP; x++ {
				results <- <-Next
				runtime.Gosched()
			}
		}(results)
		runtime.Gosched()
	}

	// Wait for results
	for x := 0; x < MAX; x++ {
		uids = append(uids, <-results)
	}

	// Make sure all results are unique
	for x, have := range uids {
		for y, found := range uids {
			if have == found && x != y {
				t.Errorf("Duplicate UID: Cases %d & %d, have %d found %d", x, y, have, found)
			}
		}
	}
}
开发者ID:iaingmackay,项目名称:simplemud,代码行数:30,代码来源:uid_test.go

示例3: TestDumpReturnsAllRecentMessagesToMultipleDumpRequestsWithMessagesCloningInInTheMeantime

func TestDumpReturnsAllRecentMessagesToMultipleDumpRequestsWithMessagesCloningInInTheMeantime(t *testing.T) {
	var bufferSize uint
	bufferSize = 2

	dump := NewDumpSink("myApp", bufferSize, loggertesthelper.Logger())
	dump.Run()

	logMessage := messagetesthelpers.NewMessage(t, "1", "appId")
	dump.Channel() <- logMessage
	logMessage = messagetesthelpers.NewMessage(t, "2", "appId")
	dump.Channel() <- logMessage
	logMessage = messagetesthelpers.NewMessage(t, "3", "appId")
	dump.Channel() <- logMessage

	runtime.Gosched()

	logMessages := dumpAllMessages(dump, bufferSize)
	assert.Equal(t, len(logMessages), 2)
	assert.Equal(t, string(logMessages[0].GetLogMessage().GetMessage()), "2")
	assert.Equal(t, string(logMessages[1].GetLogMessage().GetMessage()), "3")

	logMessage = messagetesthelpers.NewMessage(t, "4", "appId")
	dump.Channel() <- logMessage

	runtime.Gosched()

	logMessages = dumpAllMessages(dump, bufferSize)
	assert.Equal(t, len(logMessages), 2)
	assert.Equal(t, string(logMessages[0].GetLogMessage().GetMessage()), "3")
	assert.Equal(t, string(logMessages[1].GetLogMessage().GetMessage()), "4")
}
开发者ID:uabassguy,项目名称:loggregator,代码行数:31,代码来源:dump_sink_test.go

示例4: main

func main() {
	u, err := user.Current()
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		// Let the test pass.
		os.Exit(0)
	}

	var wg sync.WaitGroup
	for i := 0; i < 20; i++ {
		wg.Add(2)
		go func() {
			defer wg.Done()
			for i := 0; i < 1000; i++ {
				user.Lookup(u.Username)
				runtime.Gosched()
			}
		}()
		go func() {
			defer wg.Done()
			for i := 0; i < 1000; i++ {
				p := C.malloc(C.size_t(len(u.Username) + 1))
				runtime.Gosched()
				C.free(p)
			}
		}()
	}
	wg.Wait()
}
开发者ID:Greentor,项目名称:go,代码行数:29,代码来源:tsan5.go

示例5: TestDumpWithLotsOfMessagesAndLargeBuffer

func TestDumpWithLotsOfMessagesAndLargeBuffer(t *testing.T) {
	dump := NewDumpSink("myApp", 200, loggertesthelper.Logger(), make(chan Sink, 1), time.Second)

	go dump.Run()

	for i := 0; i < 1000; i++ {
		logMessage := messagetesthelpers.NewMessage(t, strconv.Itoa(i), "appId")
		dump.Channel() <- logMessage
	}

	runtime.Gosched()

	logMessages := dump.Dump()
	assert.Equal(t, len(logMessages), 200)
	assert.Equal(t, string(logMessages[0].GetLogMessage().GetMessage()), "800")
	assert.Equal(t, string(logMessages[1].GetLogMessage().GetMessage()), "801")

	for i := 1000; i < 2000; i++ {
		logMessage := messagetesthelpers.NewMessage(t, strconv.Itoa(i), "appId")
		dump.Channel() <- logMessage
	}

	runtime.Gosched()

	logMessages = dump.Dump()
	assert.Equal(t, len(logMessages), 200)
	assert.Equal(t, string(logMessages[0].GetLogMessage().GetMessage()), "1800")
	assert.Equal(t, string(logMessages[1].GetLogMessage().GetMessage()), "1801")

	logMessages = dump.Dump()
	assert.Equal(t, len(logMessages), 200)
	assert.Equal(t, string(logMessages[0].GetLogMessage().GetMessage()), "1800")
	assert.Equal(t, string(logMessages[1].GetLogMessage().GetMessage()), "1801")
}
开发者ID:james-masson,项目名称:loggregator,代码行数:34,代码来源:dump_sink_test.go

示例6: Manage

// 数据转储输出
func (self *Collector) Manage() {
	// reporter.Log.Println("**************开启输出管道************")

	// 标记开始,令self.Ctrl长度不为零
	self.CtrlW()

	// 开启文件输出协程
	go self.SaveFile()

	// 只有当收到退出通知并且通道内无数据时,才退出循环
	for !(self.CtrlLen() == 0 && len(self.DataChan) == 0) {
		// reporter.Log.Println("**************断点 8 ***********")
		select {
		case data := <-self.DataChan:
			self.dockerOne(data)

		default:
			// time.Sleep(1e7) //0.1秒
			runtime.Gosched()
		}
	}

	// 将剩余收集到但未输出的数据输出
	self.goOutput(self.Curr)

	// 等待所有输出完成
	for (self.outCount[0] > self.outCount[1]) || (self.outCount[2] > self.outCount[3]) || len(self.FileChan) > 0 {
		// time.Sleep(5e8)
		runtime.Gosched()
	}

	// 返回报告
	self.Report()
}
开发者ID:Cdim,项目名称:pholcus,代码行数:35,代码来源:collector.go

示例7: TestAddPort

func TestAddPort(t *testing.T) {
	l := NewListener()
	gcnt := runtime.NumGoroutine()
	l.AddPort(56561)
	if 1 != len(l.ports) {
		t.Errorf("Length of ports array should be 1, got %d", len(l.ports))
	}
	if runtime.Gosched(); gcnt >= runtime.NumGoroutine() {
		t.Errorf("Expected more than %d goroutines after AddPort, %d running", gcnt, runtime.NumGoroutine())
	}
	if listener, ok := l.ports[56561]; ok {
		if listener == nil {
			t.Errorf("Port listener should not be nil")
		}
	} else {
		t.Errorf("Listener should have entry for port 56561, got %v", l.ports)
	}
	gcnt = runtime.NumGoroutine()
	l.Close()
	if 0 != len(l.ports) {
		t.Errorf("After Close(), ports should have 0 entries, got %d", len(l.ports))
	}
	if runtime.Gosched(); gcnt <= runtime.NumGoroutine() {
		t.Errorf("Expected fewer than %d goroutines after Close(), %d running", gcnt, runtime.NumGoroutine())
	}
}
开发者ID:suapapa,项目名称:ircd-blight,代码行数:26,代码来源:listener_test.go

示例8: listenAction

func (tcp *TcpSer) listenAction() {

	defer func() {
		if err := recover(); err != nil {
			debug.PrintStack()
			glog.Error("handleInput error(%v)", err)
		} else {
		}
	}()

	for {
		conn, err := tcp.listener.Accept()

		if err != nil {
			if nerr, ok := err.(net.Error); ok && nerr.Temporary() {
				log.Printf("NOTICE: temporary Accept() failure - %s", err.Error())
				runtime.Gosched()
				continue
			}
			// theres no direct way to detect this error because it is not exposed
			if !strings.Contains(err.Error(), "use of closed network connection") {
				log.Printf("ERROR: listener.Accept() - %s", err.Error())
			}
			break
		}

		if tcp.ConnCount+1 == tcp.MaxConn {
			glog.Warningf("Too many connections! Active Denial %s\n", conn.RemoteAddr().String())
			conn.Close()
			runtime.Gosched()
			continue
		}
		go tcp.createNewItem(tcp, conn)
	}
}
开发者ID:izouxv,项目名称:raftX,代码行数:35,代码来源:baseSer.go

示例9: main

func main() {
	runtime.GOMAXPROCS(runtime.NumCPU()) // 모든 CPU 사용

	var data = []int{}
	var mutex = new(sync.Mutex)

	go func() { // 고루틴에서
		for i := 0; i < 1000; i++ { // 1000번 반복하면서
			mutex.Lock()           // 뮤텍스 잠금, data 슬라이스 보호 시작
			data = append(data, 1) // data 슬라이스에 1을 추가
			mutex.Unlock()         // 뮤텍스 잠금 해제, data 슬라이스 보호 종료

			runtime.Gosched() // 다른 고루틴이 CPU를 사용할 수 있도록 양보
		}
	}()

	go func() { // 고루틴에서
		for i := 0; i < 1000; i++ { // 1000번 반복하면서
			mutex.Lock()           // 뮤텍스 잠금, data 슬라이스 보호 시작
			data = append(data, 1) // data 슬라이스에 1을 추가
			mutex.Unlock()         // 뮤텍스 잠금 해제, data 슬라이스 보호 종료

			runtime.Gosched() // 다른 고루틴이 CPU를 사용할 수 있도록 양보
		}
	}()

	time.Sleep(2 * time.Second) // 2초 대기

	fmt.Println(len(data)) // data 슬라이스의 길이 출력
}
开发者ID:jemoonkim,项目名称:golangbook,代码行数:30,代码来源:mutex_2.go

示例10: sleep

func sleep() {
	<-ticker;
	<-ticker;
	runtime.Gosched();
	runtime.Gosched();
	runtime.Gosched();
}
开发者ID:8l,项目名称:go-learn,代码行数:7,代码来源:nonblock.go

示例11: TestDumpWithLotsOfMessages

func TestDumpWithLotsOfMessages(t *testing.T) {
	dump := NewDumpSink("myApp", 2, loggertesthelper.Logger())

	for i := 0; i < 100; i++ {
		logMessage := messagetesthelpers.NewMessage(t, strconv.Itoa(i), "appId")
		dump.Channel() <- logMessage
	}

	runtime.Gosched()

	logMessages := dumpAllMessages(dump)
	assert.Equal(t, len(logMessages), 2)
	assert.Equal(t, string(logMessages[0].GetLogMessage().GetMessage()), "98")
	assert.Equal(t, string(logMessages[1].GetLogMessage().GetMessage()), "99")

	for i := 100; i < 200; i++ {
		logMessage := messagetesthelpers.NewMessage(t, strconv.Itoa(i), "appId")
		dump.Channel() <- logMessage
	}

	runtime.Gosched()

	logMessages = dumpAllMessages(dump)
	assert.Equal(t, len(logMessages), 2)
	assert.Equal(t, string(logMessages[0].GetLogMessage().GetMessage()), "198")
	assert.Equal(t, string(logMessages[1].GetLogMessage().GetMessage()), "199")

	logMessages = dumpAllMessages(dump)
	assert.Equal(t, len(logMessages), 2)
	assert.Equal(t, string(logMessages[0].GetLogMessage().GetMessage()), "198")
	assert.Equal(t, string(logMessages[1].GetLogMessage().GetMessage()), "199")
}
开发者ID:pxie,项目名称:loggregator,代码行数:32,代码来源:dump_sink_test.go

示例12: Manage

// 数据转储输出
func (self *Collector) Manage() {
	// 标记开始,令self.Ctrl长度不为零
	self.CtrlW()

	// 开启文件输出协程
	go self.SaveFile()

	// 只有当收到退出通知并且通道内无数据时,才退出循环
	for !(self.CtrlLen() == 0 && len(self.DataChan) == 0) {
		select {
		case data := <-self.DataChan:
			self.Dockers[self.Curr] = append(self.Dockers[self.Curr], data)
			// 检查是否更换缓存块
			if len(self.Dockers[self.Curr]) >= cache.Task.DockerCap {
				// curDocker存满后输出
				self.goOutput(self.Curr)
				// 更换一个空Docker用于curDocker
				self.DockerQueue.Change()
			}
		default:
			runtime.Gosched()
		}
	}

	// 将剩余收集到但未输出的数据输出
	self.goOutput(self.Curr)

	// 等待所有输出完成
	for (self.outCount[0] > self.outCount[1]) || (self.outCount[2] > self.outCount[3]) || len(self.FileChan) > 0 {
		runtime.Gosched()
	}

	// 返回报告
	self.Report()
}
开发者ID:ReinhardHsu,项目名称:pholcus,代码行数:36,代码来源:collector.go

示例13: Push

// 添加请求到队列,并发安全
func (self *Matrix) Push(req *request.Request) {
	if sdl.checkStatus(status.STOP) {
		return
	}

	// 禁止并发,降低请求积存量
	self.Lock()
	defer self.Unlock()

	// 达到请求上限,停止该规则运行
	if self.maxPage >= 0 {
		return
	}

	// 暂停状态时等待,降低请求积存量
	waited := false
	for sdl.checkStatus(status.PAUSE) {
		waited = true
		runtime.Gosched()
	}
	if waited && sdl.checkStatus(status.STOP) {
		return
	}

	// 资源使用过多时等待,降低请求积存量
	waited = false
	for self.resCount > sdl.avgRes() {
		waited = true
		runtime.Gosched()
	}
	if waited && sdl.checkStatus(status.STOP) {
		return
	}

	// 不可重复下载的req
	if !req.IsReloadable() {
		hash := makeUnique(req)
		// 已存在成功记录时退出
		if self.hasHistory(hash) {
			return
		}
		// 添加到临时记录
		self.insertTempHistory(hash)
	}

	var priority = req.GetPriority()

	// 初始化该蜘蛛下该优先级队列
	if _, found := self.reqs[priority]; !found {
		self.priorities = append(self.priorities, priority)
		sort.Ints(self.priorities) // 从小到大排序
		self.reqs[priority] = []*request.Request{}
	}

	// 添加请求到队列
	self.reqs[priority] = append(self.reqs[priority], req)

	// 大致限制加入队列的请求量,并发情况下应该会比maxPage多
	atomic.AddInt64(&self.maxPage, 1)
}
开发者ID:clock145,项目名称:pholcus,代码行数:61,代码来源:matrix.go

示例14: TestOutputChannel

func TestOutputChannel(t *testing.T) {
	o := &testOutput{}
	oc := NewOutputChannel(o)
	defer oc.Close()
	oc.input <- F{"foo": "bar"}
	assert.Nil(t, o.last)
	runtime.Gosched()
	assert.Equal(t, F{"foo": "bar"}, F(o.last))

	// Trigger error path
	buf := bytes.NewBuffer(nil)
	oldCritialLogger := critialLogger
	defer func() { critialLogger = oldCritialLogger }()
	critialLogger = func(v ...interface{}) {
		fmt.Fprint(buf, v...)
	}
	o.err = errors.New("some error")
	oc.input <- F{"foo": "bar"}
	// Wait for log output to go through
	runtime.Gosched()
	for i := 0; i < 10 && buf.Len() == 0; i++ {
		time.Sleep(10 * time.Millisecond)
	}
	assert.Contains(t, buf.String(), "cannot write log message: some error")
}
开发者ID:patrickToca,项目名称:xlog,代码行数:25,代码来源:output_test.go

示例15: TestClosePort

func TestClosePort(t *testing.T) {
	l := NewListener()
	l.AddPort(56561)
	gcnt := runtime.Goroutines()
	l.ClosePort(56561)
	// ClosePort is not synchronized, so give it some time (on mac, dialog pops up)
	for i := 0; i < 100 && 0 != len(l.ports); i++ {
		time.Sleep(1e6)
	}
	if runtime.Gosched(); 0 != len(l.ports) {
		t.Errorf("After ClosePort(), ports should have 0 entries, got %d", len(l.ports))
	}
	if runtime.Gosched(); gcnt <= runtime.Goroutines() {
		t.Errorf("Expected fewer than %d goroutines after ClosePort(), %d running", gcnt,
			runtime.Goroutines())
	}
	l.Close()
	if 0 != len(l.ports) {
		t.Errorf("After Close(), ports should have 0 entries, got %d", len(l.ports))
	}
	if runtime.Gosched(); gcnt <= runtime.Goroutines() {
		t.Errorf("Expected fewer than %d goroutines after Close(), %d running", gcnt,
			runtime.Goroutines())
	}
}
开发者ID:kylelemons,项目名称:ircd-blight,代码行数:25,代码来源:listener_test.go


注:本文中的runtime.Gosched函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。