Skip to content

Instantly share code, notes, and snippets.

View BetterProgramming's full-sized avatar

BetterProgramming

View GitHub Profile
func Start(w io.Writer) error {
tracing.Lock()
defer tracing.Unlock()
if err := runtime.StartTrace(); err != nil {
return err
}
go func() {
for {
data := runtime.ReadTrace()
if data == nil {
func Trace(w io.Writer) error {
if err := runtime.StartTrace(); err != nil {
return err
}
defer runtime.StopTrace()
go func() {
for {
data := runtime.ReadTrace()
if data == nil {
break
func (l *loggingT) setV(pc uintptr) Level {
fn := runtime.FuncForPC(pc)
file, _ := fn.FileLine(pc)
// The file is something like /a/b/c/d.go. We want just the d.
if strings.HasSuffix(file, ".go") {
file = file[:len(file)-3]
}
if slash := strings.LastIndex(file, "/"); slash >= 0 {
file = file[slash+1:]
}
package main
import (
"fmt"
"runtime"
)
func main() {
for i := 0 ; i< 4; i++ {
test(i)
func (s server) ServeMemProfile(ws *websocket.Conn) {
defer ws.Close()
var payload struct {
MemStats runtime.MemStats
GCStats debug.GCStats
NumGoroutine int
}
for {
payload.NumGoroutine = runtime.NumGoroutine()
runtime.ReadMemStats(&payload.MemStats)
func TestMemStats(t *testing.T) {
var m runtime.MemStats
runtime.ReadMemStats(&m)
v := reflect.ValueOf(&m).Elem()
for i := 0; i < v.NumField(); i++ {
t.Logf("%s %s %v \n",
v.Field(i).Type(),
v.Type().Field(i).Name,
v.Field(i).Interface())
}
func (c *baseGoCollector) Collect(ch chan<- Metric) {
ch <- MustNewConstMetric(c.goroutinesDesc, GaugeValue, float64(runtime.NumGoroutine()))
n := getRuntimeNumThreads()
ch <- MustNewConstMetric(c.threadsDesc, GaugeValue, n)
var stats debug.GCStats
stats.PauseQuantiles = make([]time.Duration, 5)
debug.ReadGCStats(&stats)
quantiles := make(map[float64]float64)
for idx, pq := range stats.PauseQuantiles[1:] {
quantiles[float64(idx+1)/float64(len(stats.PauseQuantiles)-1)] = pq.Seconds()
func (h *boringHMAC) Reset() {
if h.needCleanup {
C._goboringcrypto_HMAC_CTX_cleanup(&h.ctx)
} else {
h.needCleanup = true
// Note: Because of the finalizer, any time h.ctx is passed to cgo,
// that call must be followed by a call to runtime.KeepAlive(h),
// to make sure h is not collected (and finalized) before the cgo
// call returns.
runtime.SetFinalizer(h, (*boringHMAC).finalize)
func main() {
runtime.GOMAXPROCS(2)
var wg sync.WaitGroup
wg.Add(2)
fmt.Printf("Starting")
go func() {
defer wg.Done()
for n := 0; n < 10; n++ {
fmt.Printf("%d\n", n)
}
// Register the route to return all the animals
router.get('/animals', async (req: IttyRequest, env: Env) => {
let animals = await getAllAnimals(env)
return new Response(JSON.stringify(animals))
})
// Register the route to return an animal by ID
router.get('/animals/:id', async (req: IttyRequest, env: Env) => {
let animals = await getAllAnimals(env)