|
1 |
| -// package main |
2 |
| - |
3 |
| -// import ( |
4 |
| -// "fmt" |
5 |
| -// "message-queue/queue" |
6 |
| -// "sync" |
7 |
| -// "time" |
8 |
| -// ) |
9 |
| - |
10 |
| -// func main() { |
11 |
| -// // instance := queue.GetDBInstance("db1") |
12 |
| -// // instance.RPop() |
13 |
| -// // instance.LPush("a") |
14 |
| -// // instance.RPush(0) |
15 |
| -// // instance.LPop() |
16 |
| -// // instance.RPush(1) |
17 |
| -// // instance.RPop() |
18 |
| -// // instance.RPop() |
19 |
| -// // instance.RPush(2) |
20 |
| -// // instance.LPush(3) |
21 |
| -// // instance.DisplayQueue() |
22 |
| - |
23 |
| -// benchmark(1000000) |
24 |
| -// } |
25 |
| - |
26 |
| -// func benchmark(ops int) { |
27 |
| -// benchmarkLPush(ops) |
28 |
| -// benchmarkRPush(ops) |
29 |
| -// benchmarkLPop(ops) |
30 |
| -// benchmarkRPop(ops) |
31 |
| - |
32 |
| -// // benchmarkPushAndPop(ops) |
33 |
| -// } |
34 |
| - |
35 |
| -// func benchmarkLPush(ops int) { |
36 |
| -// start := time.Now().UnixNano() |
37 |
| -// instance := queue.GetDBInstance("db0") |
38 |
| -// for i := 0; i < ops; i++ { |
39 |
| -// instance.LPush(i) |
40 |
| -// } |
41 |
| -// end := time.Now().UnixNano() |
42 |
| -// cost := float64(end-start) / 1e6 |
43 |
| - |
44 |
| -// fmt.Printf("------------------\n<benchmark>\n%d次[LPush]性能测试\n------------------\n-->操作次数: %d 次\n-->耗时: %f 毫秒\n-->队列长度: %d\n", |
45 |
| -// ops, ops, cost, instance.GetSize()) |
46 |
| -// } |
47 |
| - |
48 |
| -// func benchmarkRPush(ops int) { |
49 |
| -// start := time.Now().UnixNano() |
50 |
| -// instance := queue.GetDBInstance("db1") |
51 |
| -// for i := 0; i < ops; i++ { |
52 |
| -// instance.RPush(i) |
53 |
| -// } |
54 |
| -// end := time.Now().UnixNano() |
55 |
| -// cost := float64(end-start) / 1e6 |
56 |
| - |
57 |
| -// fmt.Printf("------------------\n<benchmark>\n%d次[RPush]性能测试\n------------------\n-->操作次数: %d 次\n-->耗时: %f 毫秒\n-->队列长度: %d\n", |
58 |
| -// ops, ops, cost, instance.GetSize()) |
59 |
| -// } |
60 |
| - |
61 |
| -// func benchmarkLPop(ops int) { |
62 |
| -// instance := queue.GetDBInstance("db2") |
63 |
| -// for i := 0; i < ops; i++ { |
64 |
| -// instance.LPush(i) |
65 |
| -// } |
66 |
| -// start := time.Now().UnixNano() |
67 |
| -// for i := 0; i < ops; i++ { |
68 |
| -// instance.LPop() |
69 |
| -// } |
70 |
| -// end := time.Now().UnixNano() |
71 |
| -// cost := float64(end-start) / 1e6 |
72 |
| - |
73 |
| -// fmt.Printf("------------------\n<benchmark>\n%d次[LPop]性能测试\n------------------\n-->操作次数: %d 次\n-->耗时: %f 毫秒\n-->队列长度: %d\n", |
74 |
| -// ops, ops, cost, instance.GetSize()) |
75 |
| -// } |
76 |
| - |
77 |
| -// func benchmarkRPop(ops int) { |
78 |
| -// instance := queue.GetDBInstance("db3") |
79 |
| -// for i := 0; i < ops; i++ { |
80 |
| -// instance.LPush(i) |
81 |
| -// } |
82 |
| -// start := time.Now().UnixNano() |
83 |
| -// for i := 0; i < ops; i++ { |
84 |
| -// instance.RPop() |
85 |
| -// } |
86 |
| -// end := time.Now().UnixNano() |
87 |
| -// cost := float64(end-start) / 1e6 |
88 |
| - |
89 |
| -// fmt.Printf("------------------\n<benchmark>\n%d次[RPop]性能测试\n------------------\n-->操作次数: %d 次\n-->耗时: %f 毫秒\n-->队列长度: %d\n", |
90 |
| -// ops, ops, cost, instance.GetSize()) |
91 |
| -// } |
92 |
| - |
93 |
| -// func benchmarkPushAndPop(ops int) { |
94 |
| -// var wg sync.WaitGroup |
95 |
| -// start := time.Now().UnixNano() |
96 |
| -// instance := queue.GetDBInstance("db4") |
97 |
| -// wg.Add(2) |
98 |
| -// go func() { |
99 |
| -// for i := 0; i < ops; i++ { |
100 |
| -// instance.RPush(i) |
101 |
| -// } |
102 |
| -// wg.Done() |
103 |
| -// }() |
104 |
| - |
105 |
| -// go func(instance *queue.Instance) { |
106 |
| -// var times int |
107 |
| -// for { |
108 |
| -// if times == ops { |
109 |
| -// break |
110 |
| -// } |
111 |
| -// if instance.GetSize() == 0 { |
112 |
| -// continue |
113 |
| -// } |
114 |
| -// _, e := instance.RPop() |
115 |
| -// if e == nil { |
116 |
| -// times++ |
117 |
| -// } |
118 |
| -// } |
119 |
| -// wg.Done() |
120 |
| -// }(instance) |
121 |
| - |
122 |
| -// wg.Wait() |
123 |
| - |
124 |
| -// end := time.Now().UnixNano() |
125 |
| -// cost := float64(end-start) / 1e6 |
126 |
| -// fmt.Printf("------------------\n<benchmark>\n%d次同时[RPush|RPop]性能测试\n------------------\n-->操作次数: %d 次\n-->耗时: %f 毫秒\n-->队列长度: %d\n", |
127 |
| -// ops, ops*2, cost, instance.GetSize()) |
128 |
| - |
129 |
| -// instance.FlushDB() |
130 |
| - |
131 |
| -// // instance.LPush("x") |
132 |
| -// } |
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "sync" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/keepchen/message-queue/queue" |
| 9 | +) |
| 10 | + |
| 11 | +func main() { |
| 12 | + // instance := queue.GetDBInstance("db1") |
| 13 | + // instance.RPop() |
| 14 | + // instance.LPush("a") |
| 15 | + // instance.RPush(0) |
| 16 | + // instance.LPop() |
| 17 | + // instance.RPush(1) |
| 18 | + // instance.RPop() |
| 19 | + // instance.RPop() |
| 20 | + // instance.RPush(2) |
| 21 | + // instance.LPush(3) |
| 22 | + // instance.DisplayQueue() |
| 23 | + |
| 24 | + benchmark(1000000) |
| 25 | +} |
| 26 | + |
| 27 | +func benchmark(ops int) { |
| 28 | + benchmarkLPush(ops) |
| 29 | + benchmarkRPush(ops) |
| 30 | + benchmarkLPop(ops) |
| 31 | + benchmarkRPop(ops) |
| 32 | + |
| 33 | + // benchmarkPushAndPop(ops) |
| 34 | +} |
| 35 | + |
| 36 | +func benchmarkLPush(ops int) { |
| 37 | + start := time.Now().UnixNano() |
| 38 | + instance := queue.GetDBInstance("db0") |
| 39 | + for i := 0; i < ops; i++ { |
| 40 | + instance.LPush(i) |
| 41 | + } |
| 42 | + end := time.Now().UnixNano() |
| 43 | + cost := float64(end-start) / 1e6 |
| 44 | + |
| 45 | + fmt.Printf("------------------\n<benchmark>\n%d次[LPush]性能测试\n------------------\n-->操作次数: %d 次\n-->耗时: %f 毫秒\n-->队列长度: %d\n", |
| 46 | + ops, ops, cost, instance.GetSize()) |
| 47 | +} |
| 48 | + |
| 49 | +func benchmarkRPush(ops int) { |
| 50 | + start := time.Now().UnixNano() |
| 51 | + instance := queue.GetDBInstance("db1") |
| 52 | + for i := 0; i < ops; i++ { |
| 53 | + instance.RPush(i) |
| 54 | + } |
| 55 | + end := time.Now().UnixNano() |
| 56 | + cost := float64(end-start) / 1e6 |
| 57 | + |
| 58 | + fmt.Printf("------------------\n<benchmark>\n%d次[RPush]性能测试\n------------------\n-->操作次数: %d 次\n-->耗时: %f 毫秒\n-->队列长度: %d\n", |
| 59 | + ops, ops, cost, instance.GetSize()) |
| 60 | +} |
| 61 | + |
| 62 | +func benchmarkLPop(ops int) { |
| 63 | + instance := queue.GetDBInstance("db2") |
| 64 | + for i := 0; i < ops; i++ { |
| 65 | + instance.LPush(i) |
| 66 | + } |
| 67 | + start := time.Now().UnixNano() |
| 68 | + for i := 0; i < ops; i++ { |
| 69 | + instance.LPop() |
| 70 | + } |
| 71 | + end := time.Now().UnixNano() |
| 72 | + cost := float64(end-start) / 1e6 |
| 73 | + |
| 74 | + fmt.Printf("------------------\n<benchmark>\n%d次[LPop]性能测试\n------------------\n-->操作次数: %d 次\n-->耗时: %f 毫秒\n-->队列长度: %d\n", |
| 75 | + ops, ops, cost, instance.GetSize()) |
| 76 | +} |
| 77 | + |
| 78 | +func benchmarkRPop(ops int) { |
| 79 | + instance := queue.GetDBInstance("db3") |
| 80 | + for i := 0; i < ops; i++ { |
| 81 | + instance.LPush(i) |
| 82 | + } |
| 83 | + start := time.Now().UnixNano() |
| 84 | + for i := 0; i < ops; i++ { |
| 85 | + instance.RPop() |
| 86 | + } |
| 87 | + end := time.Now().UnixNano() |
| 88 | + cost := float64(end-start) / 1e6 |
| 89 | + |
| 90 | + fmt.Printf("------------------\n<benchmark>\n%d次[RPop]性能测试\n------------------\n-->操作次数: %d 次\n-->耗时: %f 毫秒\n-->队列长度: %d\n", |
| 91 | + ops, ops, cost, instance.GetSize()) |
| 92 | +} |
| 93 | + |
| 94 | +func benchmarkPushAndPop(ops int) { |
| 95 | + var wg sync.WaitGroup |
| 96 | + start := time.Now().UnixNano() |
| 97 | + instance := queue.GetDBInstance("db4") |
| 98 | + wg.Add(2) |
| 99 | + go func() { |
| 100 | + for i := 0; i < ops; i++ { |
| 101 | + instance.RPush(i) |
| 102 | + } |
| 103 | + wg.Done() |
| 104 | + }() |
| 105 | + |
| 106 | + go func(instance *queue.Instance) { |
| 107 | + var times int |
| 108 | + for { |
| 109 | + if times == ops { |
| 110 | + break |
| 111 | + } |
| 112 | + if instance.GetSize() == 0 { |
| 113 | + continue |
| 114 | + } |
| 115 | + _, e := instance.RPop() |
| 116 | + if e == nil { |
| 117 | + times++ |
| 118 | + } |
| 119 | + } |
| 120 | + wg.Done() |
| 121 | + }(instance) |
| 122 | + |
| 123 | + wg.Wait() |
| 124 | + |
| 125 | + end := time.Now().UnixNano() |
| 126 | + cost := float64(end-start) / 1e6 |
| 127 | + fmt.Printf("------------------\n<benchmark>\n%d次同时[RPush|RPop]性能测试\n------------------\n-->操作次数: %d 次\n-->耗时: %f 毫秒\n-->队列长度: %d\n", |
| 128 | + ops, ops*2, cost, instance.GetSize()) |
| 129 | + |
| 130 | + instance.FlushDB() |
| 131 | + |
| 132 | + // instance.LPush("x") |
| 133 | +} |
0 commit comments