-
-
Save ccfrost/bc80e1f0a337ba83a998dcd1ec7c333e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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