github.com/mantzas/incata@v0.3.0/examples/example.go (about)

     1  package main
     2  
     3  import (
     4  	"flag"
     5  	"fmt"
     6  	"log"
     7  	"runtime"
     8  	"strconv"
     9  	"sync"
    10  	"time"
    11  
    12  	_ "github.com/denisenkom/go-mssqldb" // import sql driver
    13  	_ "github.com/lib/pq"                // import postgrsql driver
    14  	"github.com/mantzas/incata"
    15  	"github.com/mantzas/incata/marshal"
    16  	"github.com/mantzas/incata/model"
    17  	"github.com/mantzas/incata/reader"
    18  	"github.com/mantzas/incata/storage"
    19  	"github.com/mantzas/incata/writer"
    20  	"github.com/satori/go.uuid"
    21  )
    22  
    23  type payload struct {
    24  	Description string `json:"description"`
    25  }
    26  
    27  func main() {
    28  	eventCount := flag.Int("events", 1, "count of events stored")
    29  	dbTypeInput := flag.String("db", "MSSQL", "db type: MSSQL or PostgreSQL")
    30  	dbName := flag.String("dbName", "", "db name")
    31  	connection := flag.String("conn", "", `MSSQL:      Server={server};Database={db};User Id={user};Password={password};
    32          PostgreSQL: postgres://{user}:{passwd}@{host}/{db}?sslmode=disable`)
    33  	flag.Parse()
    34  
    35  	if *eventCount == 0 || *dbTypeInput == "" || *connection == "" || *dbName == "" {
    36  		flag.Usage()
    37  		return
    38  	}
    39  
    40  	fmt.Printf("Event Count: %d", *eventCount)
    41  	fmt.Println()
    42  	fmt.Printf("Max Procs: %d", runtime.NumCPU())
    43  	fmt.Println()
    44  
    45  	dbType, err := storage.ConvertToDbType(*dbTypeInput)
    46  	if err != nil {
    47  		panic(err.Error())
    48  	}
    49  
    50  	fmt.Println(*connection)
    51  
    52  	storage, err := storage.NewStorage(dbType, *connection, *dbName)
    53  
    54  	if err != nil {
    55  		panic(err)
    56  	}
    57  
    58  	sr := marshal.NewJSONMarshaller()
    59  	wr := writer.NewSQLWriter(storage, sr)
    60  
    61  	incata.SetupAppender(wr)
    62  
    63  	sourceID := uuid.NewV4()
    64  
    65  	fmt.Println("Starting")
    66  	var wg sync.WaitGroup
    67  	wg.Add(*eventCount)
    68  	startTime := time.Now()
    69  
    70  	for i := 1; i <= *eventCount; i++ {
    71  
    72  		go func(index int) {
    73  			defer wg.Done()
    74  			event := model.NewEvent(sourceID, payload{Description: string(index)}, "TestEvent", 1)
    75  			ar, err := incata.NewAppender()
    76  			if err != nil {
    77  				log.Print(err)
    78  			}
    79  			err = ar.Append(*event)
    80  		}(i)
    81  	}
    82  
    83  	wg.Wait()
    84  
    85  	timePassed := time.Since(startTime)
    86  	timePerEvent := timePassed.Nanoseconds() / int64(*eventCount)
    87  	timeDurationPerEvent, _ := time.ParseDuration(strconv.FormatInt(timePerEvent, 10) + "ns")
    88  
    89  	fmt.Printf("Finished appending events in %s. %s per event", timePassed, timeDurationPerEvent)
    90  	fmt.Println()
    91  
    92  	reader := reader.NewSQLReader(storage, sr)
    93  	incata.SetupRetriever(reader)
    94  
    95  	retriever, err := incata.NewRetriever()
    96  	if err != nil {
    97  		panic(err)
    98  	}
    99  
   100  	events, err := retriever.Retrieve(sourceID)
   101  	if err != nil {
   102  		panic(err)
   103  	}
   104  
   105  	fmt.Printf("Read %d events, expected %d", len(events), *eventCount)
   106  	fmt.Println()
   107  
   108  	for index, event := range events {
   109  		if !uuid.Equal(sourceID, event.SourceID) {
   110  			fmt.Printf("Expected %s but was %s on item %d. %s", sourceID, event.SourceID, index, event.Created)
   111  			fmt.Println()
   112  		}
   113  	}
   114  }