func TestDefault2(t *testing.T) { Default = dbm.NewMDbs2() AddDefault2("cny:[email protected]:27017/cny*5") if len(Default.Dbs) != 5 { t.Error("error") return } AddDefault2("cny:[email protected]:27017/cny*5;cny:[email protected]:27017/cny;cny:[email protected]:27017/cny*3") if len(Default.Dbs) != 14 { t.Error("error") return } }
func TestDefault(t *testing.T) { dbm.ShowLog = true time.Sleep(time.Second) runtime.GOMAXPROCS(util.CPU()) Default = dbm.NewMDbs2() err := AddDefault("cny:[email protected]:27017/cny", "cny") if err != nil { t.Error(err.Error()) return } err = AddDefault("cny:[email protected]:27017/cny", "cny") if err != nil { t.Error(err.Error()) return } if Db() == nil { t.Error("error") return } Db().C("abc").DropCollection() // err = ChkIdx(C, Indexes) if err != nil { t.Error(err.Error()) return } err = ChkIdx(C, Indexes2) if err != nil { t.Error(err.Error()) return } // time.Sleep(5 * time.Second) wg := &sync.WaitGroup{} rundb := func() { err := C("abc").Insert(bson.M{"a": 1, "b": 2}) if err != nil { t.Error(err.Error()) } wg.Done() } wg.Add(3) for i := 0; i < 3; i++ { go rundb() } wg.Wait() // Default.SelMDb().Close() // Default.SelMDb().Close() // C("abc").Insert(bson.M{"a": 1, "b": 2}) time.Sleep(2 * time.Second) }
func TestDbL(t *testing.T) { runtime.GOMAXPROCS(util.CPU()) Default = dbm.NewMDbs2() err := AddDbL("a1", "cny:[email protected]:27017/cny", "cny") if err != nil { t.Error(err.Error()) return } err = AddDbL("a2", "cny:[email protected]:27017/cny", "cny") if err != nil { t.Error(err.Error()) return } if DbBy("a1") == nil { t.Error("error") return } if DbBy("a2") == nil { t.Error("error") return } // time.Sleep(5 * time.Second) wg := &sync.WaitGroup{} rundb := func() { err = CBy("a1", "abc").Insert(bson.M{"a": 1, "b": 2}) if err != nil { t.Error(err.Error()) } err = CBy("a2", "abc").Insert(bson.M{"a": 1, "b": 2}) if err != nil { t.Error(err.Error()) } wg.Done() } wg.Add(3) for i := 0; i < 3; i++ { go rundb() } wg.Wait() time.Sleep(2 * time.Second) fmt.Println("all done") }
package sql import ( "database/sql" "fmt" "github.com/Centny/dbm" "github.com/Centny/gwf/dbutil" "github.com/Centny/gwf/log" "github.com/Centny/gwf/util" ) var Default = dbm.NewMDbs2() var DbL = map[string]*dbm.MDbs{} func Db() *sql.DB { return Default.Db().(*sql.DB) } func DbBy(key string) *sql.DB { if mdb, ok := DbL[key]; ok { return mdb.Db().(*sql.DB) } else { panic("database is not found by name " + key) } } func AddDefault(driver, url string, idle, max int) error { mdb, err := dbm.NewMDb(NewSQL_H(driver, url, idle, max)) if err == nil { Default.Add(mdb) }
func TestPerformance(t *testing.T) { runtime.GOMAXPROCS(util.CPU()) Default = dbm.NewMDbs2() err := AddDefault("cny:[email protected]:27017/cny", "cny") if err != nil { t.Error(err.Error()) return } fmt.Println("xxxx->") var added = map[int64]bool{} var lck = sync.RWMutex{} C(Sequence).RemoveAll(nil) fmt.Println(Next2("abc", 1)) fmt.Println(Next2("abc", 10)) used, err := tutil.DoPerf(2000, "", func(i int) { err := C("abc").Insert(bson.M{"a": 1, "b": 2}) if err != nil { t.Error(err.Error()) return } _, nv, err := Next2("abc", 1) if err != nil { t.Error(err.Error()) return } _, nv2, err := Next(Sequence, "abc", 1) if err != nil { t.Error(err.Error()) return } lck.Lock() if added[nv] { panic("exit") } if added[nv2] { panic("exit") } added[nv] = true added[nv2] = true lck.Unlock() }) fmt.Println("used->", used, err) fmt.Println(Next2("abc", 0)) // // added = map[int64]bool{} used, err = tutil.DoPerf(20000, "", func(i int) { nv := WaitNext("abc") if err != nil { t.Error(err.Error()) return } nv2 := WaitNext("abc") if err != nil { t.Error(err.Error()) return } lck.Lock() if added[nv] { fmt.Println(nv) fmt.Println(pool) panic("exists") } if added[nv2] { fmt.Println(nv2) panic("exists") } added[nv] = true added[nv2] = true lck.Unlock() }) fmt.Println("used->", used, err) fmt.Println(Next2("abc", 0)) }