bench

package
v0.0.0-...-0f051d9 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 7, 2022 License: MIT Imports: 2 Imported by: 2

Documentation

Overview

Example (Cgo_sleep)

演示了每个阻塞的cgo调用都消耗一个新线程

package main

import (
	"bytes"
	"fmt"
	"runtime"
	"sync"
	"testing"
)

func CgoBenchmark(b *testing.B) {
	CallCgo(b.N)
}

// 必须使用“-gcflags-l”调用GoBenchmark以避免内联
func GoBenchmark(b *testing.B) {
	CallGo(b.N)
}

// 演示了每个阻塞的cgo调用都消耗一个新线程
func main() {
	const maxThreads = 50

	var wg sync.WaitGroup
	wg.Add(2 * maxThreads)
	for i := 0; i < 2*maxThreads; i++ {
		StartSleeper(&wg)
	}
	// 确保所有的goroutine都在运行
	wg.Wait()

	buf := make([]byte, 1<<16)
	buf = buf[:runtime.Stack(buf, true)]
	if bytes.Count(buf, []byte("locked to thread")) > maxThreads {
		fmt.Printf("> %d threads are running", maxThreads)
	}
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func CallCgo

func CallCgo(n int)

func CallGo

func CallGo(n int)

func StartSleeper

func StartSleeper(wg *sync.WaitGroup)

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL