| 
									
										
										
										
											2016-12-02 19:16:14 -05:00
										 |  |  | // Copyright 2016 The Go Authors. All rights reserved. | 
					
						
							|  |  |  | // Use of this source code is governed by a BSD-style | 
					
						
							|  |  |  | // license that can be found in the LICENSE file. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | package syncmap_test | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"fmt" | 
					
						
							|  |  |  | 	"reflect" | 
					
						
							|  |  |  | 	"sync/atomic" | 
					
						
							|  |  |  | 	"testing" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	"golang.org/x/sync/syncmap" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | type bench struct { | 
					
						
							|  |  |  | 	setup func(*testing.B, mapInterface) | 
					
						
							|  |  |  | 	perG  func(b *testing.B, pb *testing.PB, i int, m mapInterface) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func benchMap(b *testing.B, bench bench) { | 
					
						
							|  |  |  | 	for _, m := range [...]mapInterface{&DeepCopyMap{}, &RWMutexMap{}, &syncmap.Map{}} { | 
					
						
							|  |  |  | 		b.Run(fmt.Sprintf("%T", m), func(b *testing.B) { | 
					
						
							|  |  |  | 			m = reflect.New(reflect.TypeOf(m).Elem()).Interface().(mapInterface) | 
					
						
							|  |  |  | 			if bench.setup != nil { | 
					
						
							|  |  |  | 				bench.setup(b, m) | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			b.ResetTimer() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			var i int64 | 
					
						
							|  |  |  | 			b.RunParallel(func(pb *testing.PB) { | 
					
						
							|  |  |  | 				id := int(atomic.AddInt64(&i, 1) - 1) | 
					
						
							|  |  |  | 				bench.perG(b, pb, id*b.N, m) | 
					
						
							|  |  |  | 			}) | 
					
						
							|  |  |  | 		}) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func BenchmarkLoadMostlyHits(b *testing.B) { | 
					
						
							|  |  |  | 	const hits, misses = 1023, 1 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	benchMap(b, bench{ | 
					
						
							|  |  |  | 		setup: func(_ *testing.B, m mapInterface) { | 
					
						
							|  |  |  | 			for i := 0; i < hits; i++ { | 
					
						
							|  |  |  | 				m.LoadOrStore(i, i) | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			// Prime the map to get it into a steady state. | 
					
						
							|  |  |  | 			for i := 0; i < hits*2; i++ { | 
					
						
							|  |  |  | 				m.Load(i % hits) | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		perG: func(b *testing.B, pb *testing.PB, i int, m mapInterface) { | 
					
						
							|  |  |  | 			for ; pb.Next(); i++ { | 
					
						
							|  |  |  | 				m.Load(i % (hits + misses)) | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 	}) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func BenchmarkLoadMostlyMisses(b *testing.B) { | 
					
						
							|  |  |  | 	const hits, misses = 1, 1023 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	benchMap(b, bench{ | 
					
						
							|  |  |  | 		setup: func(_ *testing.B, m mapInterface) { | 
					
						
							|  |  |  | 			for i := 0; i < hits; i++ { | 
					
						
							|  |  |  | 				m.LoadOrStore(i, i) | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			// Prime the map to get it into a steady state. | 
					
						
							|  |  |  | 			for i := 0; i < hits*2; i++ { | 
					
						
							|  |  |  | 				m.Load(i % hits) | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		perG: func(b *testing.B, pb *testing.PB, i int, m mapInterface) { | 
					
						
							|  |  |  | 			for ; pb.Next(); i++ { | 
					
						
							|  |  |  | 				m.Load(i % (hits + misses)) | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 	}) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func BenchmarkLoadOrStoreBalanced(b *testing.B) { | 
					
						
							|  |  |  | 	const hits, misses = 128, 128 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	benchMap(b, bench{ | 
					
						
							|  |  |  | 		setup: func(b *testing.B, m mapInterface) { | 
					
						
							|  |  |  | 			if _, ok := m.(*DeepCopyMap); ok { | 
					
						
							|  |  |  | 				b.Skip("DeepCopyMap has quadratic running time.") | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			for i := 0; i < hits; i++ { | 
					
						
							|  |  |  | 				m.LoadOrStore(i, i) | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			// Prime the map to get it into a steady state. | 
					
						
							|  |  |  | 			for i := 0; i < hits*2; i++ { | 
					
						
							|  |  |  | 				m.Load(i % hits) | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		perG: func(b *testing.B, pb *testing.PB, i int, m mapInterface) { | 
					
						
							|  |  |  | 			for ; pb.Next(); i++ { | 
					
						
							|  |  |  | 				j := i % (hits + misses) | 
					
						
							|  |  |  | 				if j < hits { | 
					
						
							|  |  |  | 					if _, ok := m.LoadOrStore(j, i); !ok { | 
					
						
							|  |  |  | 						b.Fatalf("unexpected miss for %v", j) | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 				} else { | 
					
						
							|  |  |  | 					if v, loaded := m.LoadOrStore(i, i); loaded { | 
					
						
							|  |  |  | 						b.Fatalf("failed to store %v: existing value %v", i, v) | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 	}) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func BenchmarkLoadOrStoreUnique(b *testing.B) { | 
					
						
							|  |  |  | 	benchMap(b, bench{ | 
					
						
							|  |  |  | 		setup: func(b *testing.B, m mapInterface) { | 
					
						
							|  |  |  | 			if _, ok := m.(*DeepCopyMap); ok { | 
					
						
							|  |  |  | 				b.Skip("DeepCopyMap has quadratic running time.") | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		perG: func(b *testing.B, pb *testing.PB, i int, m mapInterface) { | 
					
						
							|  |  |  | 			for ; pb.Next(); i++ { | 
					
						
							|  |  |  | 				m.LoadOrStore(i, i) | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 	}) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func BenchmarkLoadOrStoreCollision(b *testing.B) { | 
					
						
							|  |  |  | 	benchMap(b, bench{ | 
					
						
							|  |  |  | 		setup: func(_ *testing.B, m mapInterface) { | 
					
						
							|  |  |  | 			m.LoadOrStore(0, 0) | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		perG: func(b *testing.B, pb *testing.PB, i int, m mapInterface) { | 
					
						
							|  |  |  | 			for ; pb.Next(); i++ { | 
					
						
							|  |  |  | 				m.LoadOrStore(0, 0) | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 	}) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-16 13:52:35 -05:00
										 |  |  | func BenchmarkRange(b *testing.B) { | 
					
						
							|  |  |  | 	const mapSize = 1 << 10 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	benchMap(b, bench{ | 
					
						
							|  |  |  | 		setup: func(_ *testing.B, m mapInterface) { | 
					
						
							|  |  |  | 			for i := 0; i < mapSize; i++ { | 
					
						
							|  |  |  | 				m.Store(i, i) | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		perG: func(b *testing.B, pb *testing.PB, i int, m mapInterface) { | 
					
						
							|  |  |  | 			for ; pb.Next(); i++ { | 
					
						
							|  |  |  | 				m.Range(func(_, _ interface{}) bool { return true }) | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 	}) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-02 19:16:14 -05:00
										 |  |  | // BenchmarkAdversarialAlloc tests performance when we store a new value | 
					
						
							| 
									
										
										
										
											2017-02-16 13:52:35 -05:00
										 |  |  | // immediately whenever the map is promoted to clean and otherwise load a | 
					
						
							|  |  |  | // unique, missing key. | 
					
						
							| 
									
										
										
										
											2016-12-02 19:16:14 -05:00
										 |  |  | // | 
					
						
							|  |  |  | // This forces the Load calls to always acquire the map's mutex. | 
					
						
							|  |  |  | func BenchmarkAdversarialAlloc(b *testing.B) { | 
					
						
							|  |  |  | 	benchMap(b, bench{ | 
					
						
							|  |  |  | 		perG: func(b *testing.B, pb *testing.PB, i int, m mapInterface) { | 
					
						
							|  |  |  | 			var stores, loadsSinceStore int64 | 
					
						
							|  |  |  | 			for ; pb.Next(); i++ { | 
					
						
							|  |  |  | 				m.Load(i) | 
					
						
							|  |  |  | 				if loadsSinceStore++; loadsSinceStore > stores { | 
					
						
							|  |  |  | 					m.LoadOrStore(i, stores) | 
					
						
							|  |  |  | 					loadsSinceStore = 0 | 
					
						
							|  |  |  | 					stores++ | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 	}) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-16 13:52:35 -05:00
										 |  |  | // BenchmarkAdversarialDelete tests performance when we periodically delete | 
					
						
							|  |  |  | // one key and add a different one in a large map. | 
					
						
							| 
									
										
										
										
											2016-12-02 19:16:14 -05:00
										 |  |  | // | 
					
						
							|  |  |  | // This forces the Load calls to always acquire the map's mutex and periodically | 
					
						
							|  |  |  | // makes a full copy of the map despite changing only one entry. | 
					
						
							|  |  |  | func BenchmarkAdversarialDelete(b *testing.B) { | 
					
						
							|  |  |  | 	const mapSize = 1 << 10 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	benchMap(b, bench{ | 
					
						
							|  |  |  | 		setup: func(_ *testing.B, m mapInterface) { | 
					
						
							|  |  |  | 			for i := 0; i < mapSize; i++ { | 
					
						
							|  |  |  | 				m.Store(i, i) | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		perG: func(b *testing.B, pb *testing.PB, i int, m mapInterface) { | 
					
						
							|  |  |  | 			for ; pb.Next(); i++ { | 
					
						
							|  |  |  | 				m.Load(i) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				if i%mapSize == 0 { | 
					
						
							|  |  |  | 					m.Range(func(k, _ interface{}) bool { | 
					
						
							| 
									
										
										
										
											2017-05-09 16:06:53 -04:00
										 |  |  | 						m.Delete(k) | 
					
						
							| 
									
										
										
										
											2016-12-02 19:16:14 -05:00
										 |  |  | 						return false | 
					
						
							|  |  |  | 					}) | 
					
						
							| 
									
										
										
										
											2017-02-16 13:52:35 -05:00
										 |  |  | 					m.Store(i, i) | 
					
						
							| 
									
										
										
										
											2016-12-02 19:16:14 -05:00
										 |  |  | 				} | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 	}) | 
					
						
							|  |  |  | } |