@@ -35,8 +35,10 @@ func main() {
3535 log .Println ("program started" )
3636 defer log .Println ("program finished" )
3737
38- ctx , cancel = context .WithTimeout (ctx , time .Duration (cfg .Time )* time .Second )
39- defer cancel ()
38+ if cfg .Mode != config .RunAllMode {
39+ ctx , cancel = context .WithTimeout (ctx , time .Duration (cfg .Time )* time .Second )
40+ defer cancel ()
41+ }
4042
4143 s , err := NewStorage (ctx , cfg , cfg .ReadRPS + cfg .WriteRPS )
4244 if err != nil {
@@ -127,6 +129,77 @@ func main() {
127129 go w .Metrics (ctx , & wg , metricsRL )
128130
129131 wg .Wait ()
132+ case config .RunAllMode :
133+ err = s .createTable (ctx )
134+ if err != nil {
135+ panic (fmt .Errorf ("create table failed: %w" , err ))
136+ }
137+ log .Println ("create table ok" )
138+
139+ createGen := generator .New (0 )
140+
141+ g := errgroup.Group {}
142+
143+ for i := uint64 (0 ); i < cfg .InitialDataCount ; i ++ {
144+ g .Go (func () (err error ) {
145+ e , err := createGen .Generate ()
146+ if err != nil {
147+ return err
148+ }
149+
150+ _ , err = s .Write (ctx , e )
151+
152+ return err
153+ })
154+ }
155+
156+ if err = g .Wait (); err != nil {
157+ panic (err )
158+ }
159+
160+ log .Println ("entries write ok" )
161+
162+ runCtx , runCancel := context .WithTimeout (ctx , time .Duration (cfg .Time )* time .Second )
163+
164+ runGen := generator .New (cfg .InitialDataCount )
165+
166+ w , err := workers .New (cfg , s , ref , label , jobName )
167+ if err != nil {
168+ runCancel ()
169+ panic (fmt .Errorf ("create workers failed: %w" , err ))
170+ }
171+
172+ wg := sync.WaitGroup {}
173+
174+ readRL := rate .NewLimiter (rate .Limit (cfg .ReadRPS ), 1 )
175+ wg .Add (cfg .ReadRPS )
176+ for i := 0 ; i < cfg .ReadRPS ; i ++ {
177+ go w .Read (runCtx , & wg , readRL )
178+ }
179+
180+ writeRL := rate .NewLimiter (rate .Limit (cfg .WriteRPS ), 1 )
181+ wg .Add (cfg .WriteRPS )
182+ for i := 0 ; i < cfg .WriteRPS ; i ++ {
183+ go w .Write (runCtx , & wg , writeRL , runGen )
184+ }
185+
186+ metricsRL := rate .NewLimiter (rate .Every (time .Duration (cfg .ReportPeriod )* time .Millisecond ), 1 )
187+ wg .Add (1 )
188+ go w .Metrics (runCtx , & wg , metricsRL )
189+
190+ wg .Wait ()
191+ runCancel ()
192+
193+ if err = w .Close (); err != nil {
194+ panic (fmt .Errorf ("workers close failed: %w" , err ))
195+ }
196+ log .Println ("workers close ok" )
197+
198+ err = s .dropTable (ctx )
199+ if err != nil {
200+ panic (fmt .Errorf ("drop table failed: %w" , err ))
201+ }
202+ log .Println ("cleanup table ok" )
130203 default :
131204 panic (fmt .Errorf ("unknown mode: %v" , cfg .Mode ))
132205 }
0 commit comments