-
-
Save ccfrost/122d9462ae2d33080cbb4729d959a9d8 to your computer and use it in GitHub Desktop.
This is a description
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() { | |
// cacheData represents the memory used for a cache. | |
const cacheDataSize = 1024 * 1024 * 1024 | |
cacheData := strings.Repeat("a", cacheDataSize) | |
// 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) | |
// Reference cacheData to keep it from being collected | |
// while requests are being processed. | |
runtime.KeepAlive(cacheData) | |
} | |
}() | |
} | |
// 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