· 6 years ago · Jul 23, 2019, 06:20 PM
1package main
2
3import (
4 "flag"
5 "fmt"
6 "github.com/gocql/gocql"
7 "log"
8 "strings"
9 "time"
10 "io/ioutil"
11)
12
13func main() {
14 start:=time.Now();
15
16 var query string;
17 dataCount := flag.Int("d", 100, "define data count per parition")
18 var hosts string
19 flag.StringVar(&hosts, "hosts", "localhost", "hosts to connect ( in format host1,host2,host3 )")
20
21 var replicaFactorCnt int
22 flag.IntVar(&replicaFactorCnt, "rf", 1, "Keyspace Replica Factor (1 will be simpleTopology , else will be NetworkTopology)")
23 var dc1 string
24 flag.StringVar(&dc1, "dc1","scb", "Regoin 1 name")
25 var dc2 string
26 flag.StringVar(&dc2, "dc2","bna", "Region 2 name")
27
28 var clWrite string;
29 flag.StringVar(&clWrite, "write-cl","One","Write Consistency Level ( Any One Two Three Quorum All LocalQuorum EachQuorum LocalOne)");
30
31 var clRead string;
32 flag.StringVar(&clRead, "read-cl","One","Read Consistency Level ( Any One Two Three Quorum All LocalQuorum EachQuorum LocalOne)");
33
34 var username string;
35 flag.StringVar(&username, "u","","Username");
36
37 var password string;
38 flag.StringVar(&password, "p","","Password");
39
40 var skip int
41 flag.IntVar(&skip, "skip", 0, "just start for generater id")
42
43
44
45 var action string;
46 flag.StringVar(&action, "action","all","Action: create , run , check , all");
47
48 flag.Parse()
49
50 // connect to the cluster
51
52 // cluster := gocql.NewCluster("172.16.3.89", "172.16.3.90", "172.16.3.91") //replace PublicIP with the IP addresses used by your cluster.
53 hostsSpace := strings.Replace(hosts, ",", " ", -1)
54 cluster := gocql.NewCluster(strings.Fields(hostsSpace)...) //replace PublicIP with the IP addresses used by your cluster.
55
56 var consistency gocql.Consistency;
57 consistency=gocql.ParseConsistency(clWrite)
58 cluster.Consistency = consistency
59 cluster.ProtoVersion = 4
60 cluster.ConnectTimeout = time.Second * 100
61 cluster.Timeout = time.Second * 100
62
63 if username !=""{
64 cluster.Authenticator = gocql.PasswordAuthenticator{Username: username , Password: password} //replace the username and password fields with their real settings.
65 }
66 session, err := cluster.CreateSession()
67 if err != nil {
68 log.Println(err)
69 return
70 }
71 defer session.Close()
72
73 if(action=="create" ||action== "all"){
74 // create keyspaces
75 //err = session.Query("CREATE KEYSPACE IF NOT EXISTS test_json WITH REPLICATION = { 'class' : 'NetworkTopologyStrategy', 'datacenter1' : 1};").Exec()
76 query="drop table IF EXISTS test_json.test_table";
77 log.Println(query);
78 err = session.Query(query).Exec()
79 if err != nil {
80 log.Println(err)
81 return
82 }
83
84 query="drop KEYSPACE IF EXISTS test_json";
85 log.Println(query);
86 err = session.Query(query).Exec()
87 if err != nil {
88 log.Println(err)
89 return
90 }
91 var classRF = "'class' : 'SimpleStrategy' ,'replication_factor':1"
92 if replicaFactorCnt != 1 {
93 var dcRFCnt int;
94 dcRFCnt=replicaFactorCnt/2;
95 classRF = fmt.Sprintf("'class' : 'NetworkTopologyStrategy' ,'%s':%d,'%s':%d", dc1,dcRFCnt,dc2,dcRFCnt)
96 }
97 query=fmt.Sprintf("CREATE KEYSPACE test_json WITH REPLICATION = { %s };", classRF);
98 log.Println(query)
99 err = session.Query(query).Exec()
100
101 //'class' : 'NetworkTopologyStrategy'
102 if err != nil {
103 log.Println(err)
104 return
105 }
106
107 // create table
108 err = session.Query("Create table IF NOT EXISTS test_json.test_table(orderid int primary key ,data jsonb ) ").Exec()
109 if err != nil {
110 log.Println(err)
111 return
112 }
113 }
114
115 if(action=="insert" ||action== "all"){
116 // var jsonString string;
117 jsonString,e:=ioutil.ReadFile("data.json");
118 if(e != nil){
119 log.Println(e)
120 return
121 }
122 key:= start.Unix() ;
123 log.Printf("running id: %d",key)
124 for j := 0+skip ; j < *dataCount +skip ; j++ {
125 query := fmt.Sprintf(" insert into test_json.test_table (orderid,data) values(%d,'%s' );", j,jsonString)
126 // log.Printf(query)
127 err = session.Query(query).Exec()
128 if err != nil {
129 log.Println(err)
130 return
131 }
132 }
133
134 }
135 if(action=="query" ||action== "all"){
136 consistency=gocql.ParseConsistency(clRead)
137 var allparCnt int
138 var total int
139
140 log.Println ("With Json condition");
141 startJSON:=time.Now()
142 for j := 0; j < *dataCount; j++ {
143 // query := fmt.Sprintf("select count(*) from test_json.test_table where orderid=%d and data->orderStatusNotification->Customer->Account->0->>accountId='254812620'",j)
144 query := fmt.Sprintf("select count(*) from test_json.test_table where orderid=%d and data->'orderStatusNotification'->'Customer'->>'customerId'='2241026';",j)
145 output := session.Query(query).Iter()
146 output.Scan(&total)
147 //log.Println (fmt.Sprintf(" Row %d\n", total))
148 allparCnt += total
149 }
150 elapsedJSON := time.Since(startJSON)
151 log.Println (fmt.Sprintf("All Row %d\n", allparCnt))
152 log.Printf("with json took %s", elapsedJSON)
153
154
155 allparCnt=0;
156 log.Println ("Without Json condition");
157
158 elapsedNoJSONStart:=time.Now()
159 for j := 0; j < *dataCount; j++ {
160 // query := fmt.Sprintf("select count(*) from test_json.test_table where orderid=%d and data->orderStatusNotification->Customer->Account->0->>accountId='254812620'",j)
161 query := fmt.Sprintf("select count(*) from test_json.test_table where orderid=%d",j)
162 output := session.Query(query).Iter()
163 output.Scan(&total)
164 //log.Println (fmt.Sprintf(" Row %d\n", total))
165 allparCnt += total
166 }
167 elapsedNoJSON := time.Since(elapsedNoJSONStart)
168 log.Println (fmt.Sprintf("All Row %d\n", allparCnt))
169
170 log.Printf("without json took %s", elapsedNoJSON)
171
172
173 }
174 // Return average sleep time for James
175 elapsed := time.Since(start)
176 log.Printf("took %s", elapsed)
177
178}