Skip to content

Instantly share code, notes, and snippets.

@ccfrost
Last active February 15, 2022 01:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ccfrost/bc80e1f0a337ba83a998dcd1ec7c333e to your computer and use it in GitHub Desktop.
Save ccfrost/bc80e1f0a337ba83a998dcd1ec7c333e to your computer and use it in GitHub Desktop.
package main
import (
"context"
"runtime"
"strings"
"time"
)
// main simulates the memory use of a general RPC server.
func main() {
// Simulate request serving by repeatedly allocating memory,
// sleeping to simulate the server's work, and then removing
// the reference to the allocated memory.
const requestConcurrency = 128
const totalMemorySize = 64 * 1024 * 1024
const requestMemorySize = totalMemorySize / requestConcurrency
const requestProcessingDuration = 100 * time.Millisecond
for i := 0; i < requestConcurrency; i++ {
go func() {
for {
requestData := strings.Repeat("a", requestMemorySize)
time.Sleep(requestProcessingDuration)
// Reference requestData to keep it from being
// collected until the simulated request is complete.
runtime.KeepAlive(requestData)
}
}()
}
// Run the request processing without end.
<-context.Background().Done()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment