|
|
|
@ -85,3 +85,77 @@ func TestContext(t *testing.T) { |
|
|
|
Clear(r) |
|
|
|
assertEqual(len(data), 0) |
|
|
|
} |
|
|
|
|
|
|
|
func parallelReader(r *http.Request, key string, iterations int, wait, done chan struct{}) { |
|
|
|
<-wait |
|
|
|
for i := 0; i < iterations; i++ { |
|
|
|
Get(r, key) |
|
|
|
} |
|
|
|
done <- struct{}{} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
func parallelWriter(r *http.Request, key, value string, iterations int, wait, done chan struct{}) { |
|
|
|
<-wait |
|
|
|
for i := 0; i < iterations; i++ { |
|
|
|
Get(r, key) |
|
|
|
} |
|
|
|
done <- struct{}{} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
func benchmarkMutex(b *testing.B, numReaders, numWriters, iterations int) { |
|
|
|
|
|
|
|
b.StopTimer() |
|
|
|
r, _ := http.NewRequest("GET", "http://localhost:8080/", nil) |
|
|
|
done := make(chan struct{}) |
|
|
|
b.StartTimer() |
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ { |
|
|
|
wait := make(chan struct{}) |
|
|
|
|
|
|
|
for i := 0; i < numReaders; i++ { |
|
|
|
go parallelReader(r, "test", iterations, wait, done) |
|
|
|
} |
|
|
|
|
|
|
|
for i := 0; i < numWriters; i++ { |
|
|
|
go parallelWriter(r, "test", "123", iterations, wait, done) |
|
|
|
} |
|
|
|
|
|
|
|
close(wait) |
|
|
|
|
|
|
|
for i := 0; i < numReaders+numWriters; i++ { |
|
|
|
<-done |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
func BenchmarkMutexSameReadWrite1(b *testing.B) { |
|
|
|
benchmarkMutex(b, 1, 1, 32) |
|
|
|
} |
|
|
|
func BenchmarkMutexSameReadWrite2(b *testing.B) { |
|
|
|
benchmarkMutex(b, 2, 2, 32) |
|
|
|
} |
|
|
|
func BenchmarkMutexSameReadWrite4(b *testing.B) { |
|
|
|
benchmarkMutex(b, 4, 4, 32) |
|
|
|
} |
|
|
|
func BenchmarkMutex1(b *testing.B) { |
|
|
|
benchmarkMutex(b, 2, 8, 32) |
|
|
|
} |
|
|
|
func BenchmarkMutex2(b *testing.B) { |
|
|
|
benchmarkMutex(b, 16, 4, 64) |
|
|
|
} |
|
|
|
func BenchmarkMutex3(b *testing.B) { |
|
|
|
benchmarkMutex(b, 1, 2, 128) |
|
|
|
} |
|
|
|
func BenchmarkMutex4(b *testing.B) { |
|
|
|
benchmarkMutex(b, 128, 32, 256) |
|
|
|
} |
|
|
|
func BenchmarkMutex5(b *testing.B) { |
|
|
|
benchmarkMutex(b, 1024, 2048, 64) |
|
|
|
} |
|
|
|
func BenchmarkMutex6(b *testing.B) { |
|
|
|
benchmarkMutex(b, 2048, 1024, 512) |
|
|
|
} |