-
-
Save brett19/b6d8906065d51b3eb4c3063f219351dd 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 ( | |
"fmt" | |
"github.com/couchbase/gocb" | |
"gopkg.in/couchbase/gocbcore.v7" | |
"runtime" | |
"time" | |
) | |
func main() { | |
// Configuration Options | |
useGoCbCore := true | |
numGoThreads := 4 | |
numConcurrent := 25 | |
numEach := 5000 | |
// Lets increase the number of go threads | |
runtime.GOMAXPROCS(numGoThreads) | |
// Connect to the cluster | |
cluster, err := gocb.Connect("couchbase://localhost") | |
fmt.Printf("OpenCluster: %v, %v\n", cluster, err) | |
if err != nil { | |
return | |
} | |
// Authenticate with the cluster | |
err = cluster.Authenticate(gocb.PasswordAuthenticator{ | |
"admin", | |
"123456", | |
}) | |
fmt.Printf("Authenticate: %v\n", err) | |
if err != nil { | |
return | |
} | |
// Open the default bucket to test ops against | |
bucket, err := cluster.OpenBucket("default", "") | |
fmt.Printf("OpenBucket: %v, %v\n", bucket, err) | |
if err != nil { | |
return | |
} | |
// Generate 256 bytes of random data for the document | |
randomBytes := make([]byte, 256) | |
for i := 0; i < len(randomBytes); i++ { | |
randomBytes[i] = byte(i) | |
} | |
// Create a signaling channel for use in the testing | |
signalCh := make(chan bool, 200) | |
// Start a timer | |
sTime := time.Now() | |
if !useGoCbCore { | |
// Run all the goroutines for doing work | |
for i := 0; i < numConcurrent; i++ { | |
go func() { | |
for j := 0; j < numEach; j++ { | |
_, err := bucket.Upsert("frank", randomBytes, 0) | |
if err != nil { | |
fmt.Printf("Error: %v\n", err) | |
} | |
} | |
signalCh <- true | |
}() | |
} | |
// Join everyone! | |
for i := 0; i < numConcurrent; i++ { | |
<-signalCh | |
} | |
} else { | |
// Grab the underlying gocbcore agent | |
agent := bucket.IoRouter() | |
// Handle dispatching a single operation via gocbcore | |
dispatchOne := func(cb func()) { | |
agent.Set([]byte("frank"), randomBytes, 0, 0, func(cas gocbcore.Cas, mutToken gocbcore.MutationToken, err error) { | |
if err != nil { | |
fmt.Printf("Error: %v\n", err) | |
} | |
cb() | |
}) | |
} | |
// Start all the operations | |
for i := 0; i < numConcurrent; i++ { | |
j := 0 | |
var handleOne func() | |
handleOne = func() { | |
j++ | |
if j < numEach { | |
dispatchOne(handleOne) | |
} else { | |
signalCh <- true | |
} | |
} | |
dispatchOne(handleOne) | |
} | |
// Join everyone! | |
for i := 0; i < numConcurrent; i++ { | |
<-signalCh | |
} | |
} | |
// Stop the timer and calculate ops/s | |
eTime := time.Now() | |
dTime := eTime.Sub(sTime) | |
totalOps := numConcurrent * numEach | |
opsPerSec := float64(totalOps) / (float64(dTime) / float64(time.Second)) | |
fmt.Printf("%d concurrent of %d ops (%d total) took %s (%f ops/s)\n", numConcurrent, numEach, totalOps, dTime, opsPerSec) | |
return | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment