Skip to content

Instantly share code, notes, and snippets.

@emilien-puget
Created May 14, 2020 12:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save emilien-puget/6f9d2f840d4dbe0e613da8c634bf2cc2 to your computer and use it in GitHub Desktop.
Save emilien-puget/6f9d2f840d4dbe0e613da8c634bf2cc2 to your computer and use it in GitHub Desktop.
package main
import (
"context"
"fmt"
"os"
"time"
"github.com/rs/xid"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
var emailingIndexes = map[string][]mongo.IndexModel{
"A": {
{
Keys: bson.D{
bson.E{Key: "index_1", Value: -1},
},
Options: options.Index().SetBackground(true),
},
},
"B": {
{
Keys: bson.D{
bson.E{Key: "index_1", Value: -1},
},
Options: options.Index().SetBackground(true),
},
},
"C": {
{
Keys: bson.D{
bson.E{Key: "index_1", Value: 1},
},
Options: options.Index().SetBackground(true),
},
{
Keys: bson.D{
bson.E{Key: "index_2", Value: 1},
},
Options: options.Index().
SetUnique(true).SetBackground(true),
},
{
Keys: bson.D{
bson.E{Key: "index_3", Value: 1},
},
Options: options.Index().
SetUnique(true).SetBackground(true),
},
{
Keys: bson.D{
bson.E{Key: "index_4", Value: 1},
bson.E{Key: "index_5", Value: 1},
},
Options: options.Index().
SetUnique(true).SetBackground(true),
},
},
"D": {
{
Keys: bson.D{
bson.E{Key: "index_1", Value: 1},
},
Options: options.Index().SetBackground(true),
},
},
"E": {
{
Keys: bson.D{
bson.E{Key: "index_1", Value: 1},
},
Options: options.Index().SetBackground(true),
},
},
"F": {
{
Keys: bson.D{
bson.E{Key: "index_1", Value: 1},
bson.E{Key: "index_2", Value: 1},
},
Options: options.Index().SetBackground(true),
},
},
"G": {
{
Keys: bson.D{
bson.E{Key: "index_1", Value: 1},
},
Options: options.Index().SetBackground(true),
},
{
Keys: bson.D{
bson.E{Key: "index_2", Value: 1},
},
Options: options.Index().
SetUnique(true).SetBackground(true),
},
},
"H": {
{
Keys: bson.D{
bson.E{Key: "index_1", Value: 1},
bson.E{Key: "index_2", Value: 1},
bson.E{Key: "index_3", Value: 1},
bson.E{Key: "index_4", Value: 1},
},
Options: options.Index().SetBackground(true),
},
},
"I": {
{
Keys: bson.D{
bson.E{Key: "index_1", Value: 1},
bson.E{Key: "index_2", Value: 1},
bson.E{Key: "index_3", Value: 1},
},
Options: options.Index().SetBackground(true),
},
},
"J": {
{
Keys: bson.D{
bson.E{Key: "index_1", Value: 1},
bson.E{Key: "index_2", Value: 1},
},
Options: options.Index().
SetUnique(true).SetBackground(true),
},
},
"K": {
{
Keys: bson.D{
bson.E{Key: "index_1", Value: 1},
bson.E{Key: "index_2", Value: "2dsphere"},
},
Options: options.Index().SetBackground(true),
},
},
"L": {
{
Keys: bson.D{
bson.E{Key: "index_1", Value: 1},
bson.E{Key: "index_2", Value: 1},
bson.E{Key: "index_3", Value: 1},
bson.E{Key: "index_4", Value: 1},
bson.E{Key: "index_5", Value: 1},
},
Options: options.Index().
SetUnique(true).SetBackground(true),
},
},
"M": {
{
Keys: bson.D{
bson.E{Key: "index_1", Value: 1},
bson.E{Key: "index_2", Value: 1},
},
Options: options.Index().
SetUnique(true).SetBackground(true),
},
},
"N": {
{
Keys: bson.D{
bson.E{Key: "index_1", Value: 1},
},
Options: options.Index().SetBackground(true),
},
},
"O": {
{
Keys: bson.D{
bson.E{Key: "index_1", Value: 1},
},
Options: options.Index().SetBackground(true),
},
},
"P": {
{
Keys: bson.D{
bson.E{Key: "index_1", Value: 1},
},
Options: options.Index().SetBackground(true),
},
},
"Q": {
{
Keys: bson.D{
bson.E{Key: "index_1", Value: 1},
},
Options: options.Index().SetBackground(true),
},
},
"R": {
{
Keys: bson.D{
bson.E{Key: "index_1", Value: 1},
},
Options: options.Index().SetBackground(true),
},
},
"S": {
{
Keys: bson.D{
bson.E{Key: "index_1", Value: 1},
},
Options: options.Index().SetBackground(true),
},
},
"T": {
{
Keys: bson.D{
bson.E{Key: "index_1", Value: 1},
},
Options: options.Index().SetBackground(true),
},
},
"V": {
{
Keys: bson.D{
bson.E{Key: "index_1", Value: 1},
bson.E{Key: "index_2", Value: 1},
bson.E{Key: "index_3", Value: 1},
},
Options: options.Index().SetBackground(true),
},
},
"X": {
{
Keys: bson.D{
bson.E{Key: "index_1", Value: 1},
bson.E{Key: "index_2", Value: 1},
},
Options: options.Index().SetBackground(true),
},
{
Keys: bson.D{
bson.E{Key: "index_3", Value: 1},
},
Options: options.Index().SetBackground(true),
},
},
"Y": {
{
Keys: bson.D{
bson.E{Key: "index_1", Value: 1},
bson.E{Key: "index_2", Value: 1},
},
Options: options.Index().SetBackground(true),
},
},
"Z": {
{
Keys: bson.D{
bson.E{Key: "index_1", Value: 1},
},
Options: options.Index().SetBackground(true),
},
{
Keys: bson.D{
bson.E{Key: "index_2", Value: 1},
bson.E{Key: "index_3", Value: 1},
},
Options: options.Index().SetBackground(true),
},
},
"AA": {
{
Keys: bson.D{
bson.E{Key: "index_1", Value: 1},
},
Options: options.Index().
SetUnique(true).SetBackground(true),
},
{
Keys: bson.D{
bson.E{Key: "index_2", Value: 1},
},
Options: options.Index().
SetPartialFilterExpression(bson.M{
"index_2": bson.M{
"$exists": true,
},
}).SetBackground(true),
},
},
"AB": {
{
Keys: bson.D{
bson.E{Key: "index_1", Value: 1},
},
Options: options.Index().SetBackground(true),
},
{
Keys: bson.D{
bson.E{Key: "index_2", Value: 1},
},
Options: options.Index().SetBackground(true),
},
},
"AC": {
{
Keys: bson.D{
bson.E{Key: "index_1", Value: 1},
bson.E{Key: "index_2", Value: 1},
},
Options: options.Index().SetBackground(true),
},
{
Keys: bson.D{
bson.E{Key: "index_3", Value: 1},
},
Options: options.Index().SetBackground(true),
},
{
Keys: bson.D{
bson.E{Key: "index_4", Value: 1},
},
Options: options.Index().SetBackground(true),
},
{
Keys: bson.D{
bson.E{Key: "index_5", Value: 1},
},
Options: options.Index().SetBackground(true),
},
{
Keys: bson.D{
bson.E{Key: "index_6", Value: 1},
},
Options: options.Index().SetBackground(true),
},
{
Keys: bson.D{
bson.E{Key: "index_7", Value: 1},
},
Options: options.Index().SetBackground(true),
},
{
Keys: bson.D{
bson.E{Key: "index_8", Value: 1},
},
Options: options.Index().SetBackground(true),
},
},
"AD": {
{
Keys: bson.D{
bson.E{Key: "index_1", Value: 1},
},
Options: options.Index().
SetUnique(true).SetBackground(true),
},
},
"AE": {
{
Keys: bson.D{
bson.E{Key: "index_1", Value: 1},
},
Options: options.Index().
SetUnique(true).SetBackground(true),
},
},
"AF": {
{
Keys: bson.D{
bson.E{Key: "index_1", Value: 1},
},
Options: options.Index().SetBackground(true),
},
},
"AG": {
{
Keys: bson.D{
bson.E{Key: "index_1", Value: 1},
},
Options: options.Index().SetBackground(true),
},
{
Keys: bson.D{
bson.E{Key: "index_2", Value: 1},
bson.E{Key: "index_3", Value: 1},
},
Options: options.Index().SetBackground(true),
},
{
Keys: bson.D{
bson.E{Key: "index_4", Value: 1},
},
Options: options.Index().
SetUnique(true).SetBackground(true),
},
{
Keys: bson.D{
bson.E{Key: "index_5", Value: 1},
},
Options: options.Index().SetBackground(true),
},
{
Keys: bson.D{
bson.E{Key: "index_6", Value: -1},
},
Options: options.Index().SetBackground(true),
},
{
Keys: bson.D{
bson.E{Key: "index_7", Value: 1},
},
Options: options.Index().SetBackground(true),
},
{
Keys: bson.D{
bson.E{Key: "index_8", Value: 1},
},
Options: options.Index().SetBackground(true),
},
{
Keys: bson.D{
bson.E{Key: "index_9", Value: 1},
},
Options: options.Index().SetBackground(true),
},
{
Keys: bson.D{
bson.E{Key: "index_10", Value: 1},
},
Options: options.Index().SetBackground(true),
},
},
}
func main() {
ctx := context.Background()
client, err := mongo.Connect(ctx)
if err != nil {
os.Exit(1)
}
fmt.Println("create")
for i := 0; i < 6000; i++ {
err := createDb(ctx, i, client)
if err != nil {
fmt.Print(err)
os.Exit(1)
}
}
}
func deleteDb(ctx context.Context, client *mongo.Client, databaseName string, count int) error {
start := time.Now()
database := client.Database(databaseName)
err := database.Drop(ctx)
if err != nil {
return err
}
fmt.Printf("%d,%d\n", count, int64(time.Now().Sub(start)/time.Millisecond))
return nil
}
func createDb(ctx context.Context, i int, client *mongo.Client) error {
start := time.Now()
u := xid.New()
database := client.Database(u.String())
for collName, idxs := range emailingIndexes {
c := database.Collection(collName)
err := createIndexesCollection(ctx, c, idxs)
if err != nil {
return err
}
}
fmt.Printf("%d,%d\n", i, int64(time.Now().Sub(start)/time.Millisecond))
return nil
}
func createIndexesCollection(ctx context.Context, c *mongo.Collection, idxs []mongo.IndexModel) (err error) {
for i := 0; i < 5; i++ {
_, err = c.Indexes().CreateMany(ctx, idxs)
if err == nil {
return nil
}
}
return err
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment