Last active
April 5, 2020 18:11
-
-
Save bparli/f36c7bdbc0c1674a055e1d90960434a3 to your computer and use it in GitHub Desktop.
download files to test cache performance
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 ( | |
"fmt" | |
"io/ioutil" | |
"math/rand" | |
"net/http" | |
"strconv" | |
"time" | |
"github.com/montanaflynn/stats" | |
) | |
const iterations = 10000 | |
func main() { | |
var timings stats.Float64Data | |
for i := 0; i < iterations; i++ { | |
// most frequent items | |
fileNum := rand.Intn(10) | |
elapsed := fetchAndTime(fileNum) | |
timings = append(timings, elapsed) | |
// less frequent items | |
fileNum = rand.Intn(100) | |
elapsed = fetchAndTime(fileNum) | |
timings = append(timings, elapsed) | |
// finally pollute the cache. fetch a random file not likely to be needed again | |
fileNum = rand.Intn(1000) | |
for j := 0; j < 10; j++ { | |
elapsed := fetchAndTime(fileNum) | |
timings = append(timings, elapsed) | |
} | |
} | |
mean, err := stats.Mean(timings) | |
if err != nil { | |
fmt.Println("Problem with mean", err) | |
} | |
fmt.Println("The mean response time is:", mean) | |
std, err := stats.StandardDeviation(timings) | |
if err != nil { | |
fmt.Println("Problem with standard deviation", err) | |
} | |
fmt.Println("The standard deviation is:", std) | |
p95, err := stats.Percentile(timings, 95) | |
if err != nil { | |
fmt.Println("Problem with p95", err) | |
} | |
fmt.Println("The p95 is:", p95) | |
p99, err := stats.Percentile(timings, 99) | |
if err != nil { | |
fmt.Println("Problem with p99", err) | |
} | |
fmt.Println("The p99 is:", p99) | |
} | |
func fetchAndTime(fileNum int) float64 { | |
fileStr := strconv.Itoa(fileNum) | |
if fileNum < 10 { | |
fileStr = "00" + strconv.Itoa(fileNum) | |
} else if fileNum < 100 { | |
fileStr = "0" + strconv.Itoa(fileNum) | |
} | |
start := time.Now() | |
resp, err := http.Get("http://127.0.0.1:8080/file" + fileStr + ".bin") | |
if err != nil { | |
fmt.Println("Problem downloading file number", fileStr) | |
} | |
body, err := ioutil.ReadAll(resp.Body) | |
if err != nil { | |
fmt.Println("Error reading body", err) | |
} else { | |
fmt.Printf("Downloaded file %s of size %d\n", "file"+fileStr+".bin", len(body)) | |
} | |
resp.Body.Close() | |
now := time.Now() | |
return float64(now.Sub(start).Milliseconds()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment