~eilt-team/+junk/bacula-intial-setup

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
/**
 * Copyright (C) 2009-2011 the original author or authors.
 * See the notice.md file distributed with this work for additional
 * information regarding copyright ownership.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.github.stomp.benchmark

import java.util.concurrent.atomic._
import java.util.concurrent.TimeUnit._
import scala.collection.mutable.ListBuffer
import java.util.concurrent.ConcurrentLinkedQueue
import java.security.KeyStore
import java.io.FileInputStream
import javax.net.ssl._
import org.fusesource.hawtdispatch.transport.{SslTransport, TcpTransport}

object Scenario {
  val MESSAGE_ID:Array[Byte] = "message-id"
  val NEWLINE = '\n'.toByte
  val NANOS_PER_SECOND = NANOSECONDS.convert(1, SECONDS)
  val NANOS_PER_MS = NANOSECONDS.convert(1, MILLISECONDS)
  
  implicit def toBytes(value: String):Array[Byte] = value.getBytes("UTF-8")

  def o[T](value:T):Option[T] = value match {
    case null => None
    case x => Some(x)
  }

  def percentiles(percentiles:Array[Double], values:Array[Long]) = {
    if (values.length > 0) {
      java.util.Arrays.sort(values)
      percentiles.map { p =>
        val pos = p * (values.length + 1);
        if (pos < 1) {
          values(0)
        } else if (pos >= values.length) {
          values(values.length - 1);
        } else {
           val lower = values((pos - 1).toInt);
           val upper = values(pos.toInt);
           lower + ((pos - pos.floor) * (upper - lower)).toLong;
        }
      }
    } else {
      percentiles.map(p => 0L)
    }
  }
}

trait Scenario {
  import Scenario._

  var login: Option[String] = None
  var passcode: Option[String] = None
  var request_response = false

  private var _producer_sleep: { def apply(): Int; def init(time: Long) } = new { def apply() = 0; def init(time: Long) {}  }
  def producer_sleep = _producer_sleep()
  def producer_sleep_= (new_value: Int) = _producer_sleep = new { def apply() = new_value; def init(time: Long) {}  }
  def producer_sleep_= (new_func: { def apply(): Int; def init(time: Long) }) = _producer_sleep = new_func

  private var _consumer_sleep: { def apply(): Int; def init(time: Long) } = new { def apply() = 0; def init(time: Long) {}  }
  def consumer_sleep = _consumer_sleep()
  def consumer_sleep_= (new_value: Int) = _consumer_sleep = new { def apply() = new_value; def init(time: Long) {}  }
  def consumer_sleep_= (new_func: { def apply(): Int; def init(time: Long) }) = _consumer_sleep = new_func

  var producers = 1
  var producers_per_sample = 0

  var consumers = 1
  var consumers_per_sample = 0
  var sample_interval = 1000
  var protocol = "tcp"
  var host = "127.0.0.1"
  var port = 61613
  var buffer_size = 32*1024
  
  private var _message_size: { def apply(): Int; def init(time: Long) } = new { def apply() = 1024; def init(time: Long) {}  }
  def message_size = _message_size()
  def message_size_= (new_value: Int) = _message_size = new { def apply() = new_value; def init(time: Long) {}  }
  def message_size_= (new_func: { def apply(): Int; def init(time: Long) }) = _message_size = new_func

  var persistent = false
  var persistent_header = "persistent:true"
  var sync_send = false
  var headers = Array[Array[String]]()
  var subscribe_headers = Array[Array[String]]()
  var ack = "auto"
  var selector:String = null
  var durable = false
  var consumer_prefix = "consumer-"

  var consumer_sleep_modulo = 1
  var producer_sleep_modulo = 1
  private var _messages_per_connection: { def apply(): Int; def init(time: Long) } = new { def apply() = -1; def init(time: Long) {}  }
  def messages_per_connection = _messages_per_connection()
  def messages_per_connection_= (new_value: Int) = _messages_per_connection = new { def apply() = new_value; def init(time: Long) {}  }
  def messages_per_connection_= (new_func: { def apply(): Int; def init(time: Long) }) = _messages_per_connection = new_func

  var display_errors = false

  var destination_type = "queue"
  private var _destination_name: () => String = () => ""
  def destination_name = _destination_name()
  def destination_name_=(new_name: String) = _destination_name = () => new_name
  def destination_name_=(new_func: () => String) = _destination_name = new_func

  private var _response_destination_name: () => String = () => "response"
  def response_destination_name = _response_destination_name()
  def response_destination_name_=(new_name: String) = _response_destination_name = () => new_name
  def response_destination_name_=(new_func: () => String) = _response_destination_name = new_func

  var destination_count = 1

  val producer_counter = new AtomicLong()
  val consumer_counter = new AtomicLong()
  val request_times = new ConcurrentLinkedQueue[Long]()

  val error_counter = new AtomicLong()
  val done = new AtomicBoolean()

  var queue_prefix = "/queue/"
  var topic_prefix = "/topic/"
  var name = "custom"

  var drain_timeout = 2000L

  var key_store_file:Option[String] = None
  var key_store_password:Option[String] = None
  var key_password:Option[String] = None

  var key_store:KeyStore = _
  var trust_managers:Array[TrustManager] = _
  var key_managers:Array[KeyManager] = _

  def ssl_context:SSLContext = {
    Option(SslTransport.protocol(protocol)).map { protocol =>
      val rc = SSLContext.getInstance(protocol)
      rc.init(get_key_managers, get_trust_managers, null)
      rc
    }.getOrElse(null)
  }

  def get_key_store = {
    if( key_store==null && key_store_file.isDefined ) {
      key_store = {
        val store = KeyStore.getInstance("JKS")
        store.load(new FileInputStream(key_store_file.get), key_store_password.getOrElse("").toCharArray())
        store
      }
    }
    key_store
  }

  def get_trust_managers = {
    val store = get_key_store
    if( trust_managers==null && store!=null ) {
      val factory = TrustManagerFactory.getInstance("SunX509")
      factory.init(store)
      trust_managers = factory.getTrustManagers
    }
    trust_managers
  }

  def get_key_managers = {
    val store = get_key_store
    if( key_managers==null && store!=null) {
      val factory = KeyManagerFactory.getInstance("SunX509")
      factory.init(store, key_password.getOrElse("").toCharArray())
      key_managers = factory.getKeyManagers
    }
    key_managers
  }


  def run() = {
    print(toString)
    println("--------------------------------------")
    println("     Running: Press ENTER to stop")
    println("--------------------------------------")
    println("")

    with_load {

      // start a sampling client...
      val sample_thread = new Thread() {
        override def run() = {
          
          def print_rate(name: String, periodCount:Long, totalCount:Long, nanos: Long) = {
            val rate_per_second: java.lang.Float = ((1.0f * periodCount / nanos) * NANOS_PER_SECOND)
            println("%s total: %,d, rate: %,.3f per second".format(name, totalCount, rate_per_second))
          }
          def print_percentil(name: String, value:Long, max:Long) = {
            println("%sth percentile response time: %,.3f ms, max: %,.3f ms".format(name, (1.0f * value / NANOS_PER_MS), (1.0f * max / NANOS_PER_MS)))
          }

          try {
            var start = System.nanoTime
            var total_producer_count = 0L
            var total_consumer_count = 0L
            var total_error_count = 0L
            var max_p90 = 0L
            var max_p99 = 0L
            var max_p999 = 0L

            collection_start
            while( !done.get ) {
              Thread.sleep(sample_interval)
              val end = System.nanoTime
              collection_sample
              val samples = collection_end
              samples.get("p_custom").foreach { case (_, count)::Nil =>
                total_producer_count += count
                print_rate("Producer", count, total_producer_count, end - start)
                case _ =>
              }
              samples.get("c_custom").foreach { case (_, count)::Nil =>
                total_consumer_count += count
                print_rate("Consumer", count, total_consumer_count, end - start)
                case _ =>
              }
              samples.get("p90_custom").foreach { case (_, value)::Nil =>
                max_p90 = max_p90.max(value)
                print_percentil("90", value, max_p90)
                case _ =>
              }
              samples.get("p99_custom").foreach { case (_, value)::Nil =>
                max_p99 = max_p99.max(value)
                print_percentil("99", value, max_p99)
                case _ =>
              }
              samples.get("p999_custom").foreach { case (_, value)::Nil =>
                max_p999 = max_p999.max(value)
                print_percentil("99.9", value, max_p999)
                case _ =>
              }
              samples.get("e_custom").foreach { case (_, count)::Nil =>
                if( count!= 0 ) {
                  total_error_count += count
                  print_rate("Error", count, total_error_count, end - start)
                }
                case _ =>
              }
              start = end
            }
          } catch {
            case e:InterruptedException =>
          }
        }
      }
      sample_thread.start()

      System.in.read()
      done.set(true)

      sample_thread.interrupt
      sample_thread.join
    }

  }

  override def toString() = {
    "--------------------------------------\n"+
    "Scenario Settings\n"+
    "--------------------------------------\n"+
    "  host                  = "+host+"\n"+
    "  port                  = "+port+"\n"+
    "  destination_type      = "+destination_type+"\n"+
    "  queue_prefix          = "+queue_prefix+"\n"+
    "  topic_prefix          = "+topic_prefix+"\n"+
    "  destination_count     = "+destination_count+"\n" +
    "  destination_name      = "+destination_name+"\n" +
    "  sample_interval (ms)  = "+sample_interval+"\n" +
    "  \n"+
    "  --- Producer Properties ---\n"+
    "  producers             = "+producers+"\n"+
    "  message_size          = "+message_size+"\n"+
    "  persistent            = "+persistent+"\n"+
    "  sync_send             = "+sync_send+"\n"+
    "  producer_sleep (ms)   = "+producer_sleep+"\n"+
    "  headers               = "+headers.map( _.mkString(", ") ).mkString("(", "), (", ")")+"\n"+
    "  \n"+
    "  --- Consumer Properties ---\n"+
    "  consumers             = "+consumers+"\n"+
    "  consumer_sleep (ms)   = "+consumer_sleep+"\n"+
    "  ack                   = "+ack+"\n"+
    "  selector              = "+selector+"\n"+
    "  durable               = "+durable+"\n"+
    "  consumer_prefix       = "+consumer_prefix+"\n"+
    "  subscribe_headers     = "+subscribe_headers.map( _.mkString(", ") ).mkString("(", "), (", ")")+"\n"+
    ""

  }
  
  def settings(): List[(String, String)] = {
    var s: List[(String, String)] = Nil
    
    s :+= ("host", host)
    s :+= ("port", port.toString)
    s :+= ("destination_type", destination_type)
    s :+= ("queue_prefix", queue_prefix)
    s :+= ("topic_prefix", topic_prefix)
    s :+= ("destination_count", destination_count.toString)
    s :+= ("destination_name", destination_name)
    s :+= ("sample_interval", sample_interval.toString)
    s :+= ("producers", producers.toString)
    s :+= ("message_size", message_size.toString)
    s :+= ("persistent", persistent.toString)
    s :+= ("sync_send", sync_send.toString)
    s :+= ("producer_sleep", producer_sleep.toString)
    s :+= ("headers", headers.map( _.mkString(", ") ).mkString("(", "), (", ")"))
    s :+= ("subscribe_headers", subscribe_headers.map( _.mkString(", ") ).mkString("(", "), (", ")"))
    s :+= ("consumers", consumers.toString)
    s :+= ("consumer_sleep", consumer_sleep.toString)
    s :+= ("ack", ack)
    s :+= ("selector", selector)
    s :+= ("durable", durable.toString)
    s :+= ("consumer_prefix", consumer_prefix)
    
    s
  }

  protected def destination(i:Int) = destination_type match {
    case "queue" => queue_prefix+destination_name+"-"+(i%destination_count)
    case "topic" => topic_prefix+destination_name+"-"+(i%destination_count)
    case "raw_queue" => destination_name
    case "raw_topic" => destination_name
    case _ => throw new Exception("Unsuported destination type: "+destination_type)
  }

  protected def response_destination(i:Int) = queue_prefix+response_destination_name+"-"+i

  protected def headers_for(i:Int) = {
    if ( headers.isEmpty ) {
      Array[String]()
    } else {
      headers(i%headers.size)
    }
  }

  protected def subscribe_headers_for(i:Int) = {
    if ( subscribe_headers.isEmpty ) {
      Array[String]()
    } else {
      subscribe_headers(i%subscribe_headers.size)
    }
  }

  var producer_samples:Option[ListBuffer[(Long,Long)]] = None
  var consumer_samples:Option[ListBuffer[(Long,Long)]] = None
  var error_samples = ListBuffer[(Long,Long)]()

  var request_time_p90_samples = ListBuffer[(Long,Long)]()
  var request_time_p99_samples = ListBuffer[(Long,Long)]()
  var request_time_p999_samples = ListBuffer[(Long,Long)]()

  def collection_start: Unit = {
    producer_counter.set(0)
    consumer_counter.set(0)
    error_counter.set(0)
    request_times.clear()

    producer_samples = if (producers > 0 || producers_per_sample>0 ) {
      Some(ListBuffer[(Long,Long)]())
    } else {
      None
    }
    consumer_samples = if (consumers > 0 || consumers_per_sample>0 ) {
      Some(ListBuffer[(Long,Long)]())
    } else {
      None
    }
    request_time_p90_samples = ListBuffer[(Long,Long)]()
    request_time_p99_samples = ListBuffer[(Long,Long)]()
    request_time_p999_samples = ListBuffer[(Long,Long)]()
  }

  def collection_end: Map[String, scala.List[(Long,Long)]] = {
    var rc = Map[String, List[(Long,Long)]]()
    producer_samples.foreach{ samples =>
      rc += "p_"+name -> samples.toList
      samples.clear
    }
    consumer_samples.foreach{ samples =>
      rc += "c_"+name -> samples.toList
      samples.clear
    }
    rc += "e_"+name -> error_samples.toList
    error_samples.clear
    if(request_response) {
      rc += "p90_"+name -> request_time_p90_samples.toList
      request_time_p90_samples.clear
      rc += "p99_"+name -> request_time_p99_samples.toList
      request_time_p99_samples.clear
      rc += "p999_"+name -> request_time_p999_samples.toList
      request_time_p999_samples.clear
    }

    rc
  }

  trait Client {
    def start():Unit
    def shutdown():Unit
  }

  var producer_clients = List[Client]()
  var consumer_clients = List[Client]()

  def with_load[T](func: =>T ):T = {
    done.set(false)

    val now = System.currentTimeMillis()
    _producer_sleep.init(now)
    _consumer_sleep.init(now)
    _message_size.init(now)
    _messages_per_connection.init(now)

    for (i <- 0 until producers) {
      val client = createProducer(i)
      producer_clients ::= client
      client.start()
    }

    for (i <- 0 until consumers) {
      val client = createConsumer(i)
      consumer_clients ::= client
      client.start()
    }

    try {
      func
    } finally {
      done.set(true)
      // wait for the threads to finish..
      for( client <- consumer_clients ) {
        client.shutdown
      }
      consumer_clients = List()
      for( client <- producer_clients ) {
        client.shutdown
      }
      producer_clients = List()
    }
  }

  def drain = {
    done.set(false)
    if( destination_type=="queue" || destination_type=="raw_queue" || durable==true ) {
      print("draining")
      consumer_counter.set(0)
      var consumer_clients = List[Client]()
      for (i <- 0 until destination_count) {
        val client = createConsumer(i)
        consumer_clients ::= client
        client.start()
      }

      // Keep sleeping until we stop draining messages.
      var drained = 0L
      try {
        Thread.sleep(drain_timeout);
        def done() = {
          val c = consumer_counter.getAndSet(0)
          drained += c
          c == 0
        }
        while( !done ) {
          print(".")
          Thread.sleep(drain_timeout);
        }
      } finally {
        done.set(true)
        for( client <- consumer_clients ) {
          client.shutdown
        }
        println(". (drained %d)".format(drained))
      }
    }
  }


  def collection_sample: Unit = {

    val now = System.currentTimeMillis()
    producer_samples.foreach(_.append((now, producer_counter.getAndSet(0))))
    consumer_samples.foreach(_.append((now, consumer_counter.getAndSet(0))))

    if( request_response ) {

      var count = producer_samples.get.last._2.toInt
      val times = new Array[Long](count)
      while(count > 0 ) {
        count -= 1
        times(count) = request_times.poll()
      }
      val p = percentiles(Array(0.9, 0.99, 0.999), times)
      request_time_p90_samples.append((now, p(0)))
      request_time_p99_samples.append((now, p(1)))
      request_time_p999_samples.append((now, p(2)))
    }

    error_samples.append((now, error_counter.getAndSet(0)))

    // we might need to increment number the producers..
    for (i <- 0 until producers_per_sample) {
      val client = createProducer(producer_clients.length)
      producer_clients ::= client
      client.start()
    }

    // we might need to increment number the consumers..
    for (i <- 0 until consumers_per_sample) {
      val client = createConsumer(consumer_clients.length)
      consumer_clients ::= client
      client.start()
    }

  }

  def createProducer(i:Int):Client
  def createConsumer(i:Int):Client

  protected def ignore_failure(func: =>Unit):Unit = try {
    func
  } catch { case _ =>
  }

}