| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package main |
| 18 |
|
| 19 |
import ( |
| 20 |
"flag" |
| 21 |
"fmt" |
| 22 |
"log" |
| 23 |
"math/rand" |
| 24 |
"net" |
| 25 |
"net/http" |
| 26 |
"net/rpc" |
| 27 |
"os" |
| 28 |
"os/signal" |
| 29 |
"strconv" |
| 30 |
"strings" |
| 31 |
"sync" |
| 32 |
"time" |
| 33 |
|
| 34 |
"github.com/minio/dsync" |
| 35 |
) |
| 36 |
|
| 37 |
const rpcPath = "/dsync" |
| 38 |
|
| 39 |
var servers = []string{ |
| 40 |
"10.x0.y0.z0:12345", |
| 41 |
"10.x1.y1.z1:12346", |
| 42 |
"10.x2.y2.z2:12347", |
| 43 |
"10.x3.y3.z3:12348", |
| 44 |
"10.x4.y4.z4:12349", |
| 45 |
"10.x5.y5.z5:12350", |
| 46 |
"10.x6.y6.z6:12351", |
| 47 |
"10.x7.y7.z7:12352"} |
| 48 |
|
| 49 |
var ( |
| 50 |
portFlag = flag.Int("p", 0, "Port for server to listen on") |
| 51 |
resources []string |
| 52 |
) |
| 53 |
|
| 54 |
func lockLoop(w *sync.WaitGroup, timeStart *time.Time, runs int, done *bool, nr int, ch chan<- float64) { |
| 55 |
defer w.Done() |
| 56 |
dm := dsync.NewDRWMutex(fmt.Sprintf("chaos-%d-%d", *portFlag, nr)) |
| 57 |
|
| 58 |
delayMax := float64(0.0) |
| 59 |
timeLast := time.Now() |
| 60 |
var run int |
| 61 |
for run = 1; !*done && run <= runs; run++ { |
| 62 |
dm.Lock() |
| 63 |
|
| 64 |
if run == 1 { |
| 65 |
*timeStart = time.Now() |
| 66 |
timeLast = time.Now() |
| 67 |
} |
| 68 |
|
| 69 |
duration := time.Since(timeLast) |
| 70 |
if delayMax < duration.Seconds() || run%100 == 0 { |
| 71 |
if delayMax < duration.Seconds() { |
| 72 |
delayMax = duration.Seconds() |
| 73 |
} |
| 74 |
fmt.Print(".") |
| 75 |
} |
| 76 |
timeLast = time.Now() |
| 77 |
dm.Unlock() |
| 78 |
} |
| 79 |
|
| 80 |
ch <- delayMax |
| 81 |
} |
| 82 |
|
| 83 |
func startRPCServer(port int) { |
| 84 |
server := rpc.NewServer() |
| 85 |
server.RegisterName("Dsync", &lockServer{ |
| 86 |
mutex: sync.Mutex{}, |
| 87 |
lockMap: make(map[string]int64), |
| 88 |
}) |
| 89 |
|
| 90 |
server.HandleHTTP(resources[port-12345], fmt.Sprintf("%s-debug", resources[port-12345])) |
| 91 |
l, e := net.Listen("tcp", ":"+strconv.Itoa(port)) |
| 92 |
if e != nil { |
| 93 |
log.Fatal("listen error:", e) |
| 94 |
} |
| 95 |
go http.Serve(l, nil) |
| 96 |
} |
| 97 |
|
| 98 |
func main() { |
| 99 |
rand.Seed(time.Now().UTC().UnixNano()) |
| 100 |
|
| 101 |
flag.Parse() |
| 102 |
|
| 103 |
if *portFlag == 0 { |
| 104 |
log.Fatalf("No port number specified") |
| 105 |
} |
| 106 |
|
| 107 |
resources = make([]string, 0, len(servers)) |
| 108 |
for i := range servers { |
| 109 |
resources = append(resources, rpcPath+"-"+strconv.Itoa(i)) |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
var clnts []dsync.NetLocker |
| 114 |
for i := 0; i < len(servers); i++ { |
| 115 |
clnts = append(clnts, newClient(servers[i], resources[i])) |
| 116 |
} |
| 117 |
|
| 118 |
if err := dsync.Init(clnts, getSelfNode(clnts, *portFlag)); err != nil { |
| 119 |
log.Fatalf("set nodes failed with %v", err) |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
startRPCServer(*portFlag) |
| 124 |
|
| 125 |
timeStart := time.Now() |
| 126 |
|
| 127 |
done := false |
| 128 |
|
| 129 |
|
| 130 |
c := make(chan os.Signal, 1) |
| 131 |
signal.Notify(c, os.Interrupt) |
| 132 |
go func() { |
| 133 |
for sig := range c { |
| 134 |
fmt.Println("Ctrl-C intercepted", sig) |
| 135 |
done = true |
| 136 |
} |
| 137 |
}() |
| 138 |
|
| 139 |
runs := 40000 |
| 140 |
parallel := 5 |
| 141 |
wait := sync.WaitGroup{} |
| 142 |
wait.Add(parallel) |
| 143 |
|
| 144 |
|
| 145 |
ch := make(chan float64, parallel) |
| 146 |
|
| 147 |
fmt.Println("Test starting...") |
| 148 |
|
| 149 |
for i := 0; i < parallel; i++ { |
| 150 |
go lockLoop(&wait, &timeStart, runs, &done, i, ch) |
| 151 |
} |
| 152 |
totalRuns := runs * parallel |
| 153 |
|
| 154 |
wait.Wait() |
| 155 |
close(ch) |
| 156 |
|
| 157 |
delayMax := float64(0.0) |
| 158 |
for c := range ch { |
| 159 |
if delayMax < c { |
| 160 |
delayMax = c |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
fmt.Println("") |
| 165 |
fmt.Printf(" Locks/sec: %7.0f\n", 1.0/(time.Since(timeStart).Seconds()/float64(totalRuns))) |
| 166 |
fmt.Printf(" Msgs/sec: %7.0f\n", float64(len(servers))*2.0*1.0/(time.Since(timeStart).Seconds()/float64(totalRuns))) |
| 167 |
fmt.Printf(" Worst case delay: %5.3f s\n", delayMax) |
| 168 |
|
| 169 |
if !done { |
| 170 |
|
| 171 |
fmt.Println("Waiting for test to close...") |
| 172 |
time.Sleep(10000 * time.Millisecond) |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
func getSelfNode(rpcClnts []dsync.NetLocker, port int) int { |
| 177 |
|
| 178 |
index := -1 |
| 179 |
for i, c := range rpcClnts { |
| 180 |
p, _ := strconv.Atoi(strings.Split(c.ServerAddr(), ":")[1]) |
| 181 |
if port == p { |
| 182 |
if index == -1 { |
| 183 |
index = i |
| 184 |
} else { |
| 185 |
panic("More than one port found") |
| 186 |
} |
| 187 |
} |
| 188 |
} |
| 189 |
return index |
| 190 |
} |