├── distribution.png ├── .gitignore ├── examples └── main.go ├── requester ├── noop.go ├── web_requester.go ├── cassandra_requester.go ├── nats_requester.go ├── nats_streaming_requester.go ├── nsq_requester.go ├── kafka_requester.go ├── amqp_requester.go └── redis_requester.go ├── README.md ├── summary.go ├── bench.go └── LICENSE /distribution.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tylertreat/bench/HEAD/distribution.png -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled Object files, Static and Dynamic libs (Shared Objects) 2 | *.o 3 | *.a 4 | *.so 5 | 6 | # Folders 7 | _obj 8 | _test 9 | 10 | # Architecture specific extensions/prefixes 11 | *.[568vq] 12 | [568vq].out 13 | 14 | *.cgo1.go 15 | *.cgo2.c 16 | _cgo_defun.c 17 | _cgo_gotypes.go 18 | _cgo_export.* 19 | 20 | _testmain.go 21 | 22 | *.exe 23 | *.test 24 | *.prof 25 | -------------------------------------------------------------------------------- /examples/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "time" 6 | 7 | "github.com/tylertreat/bench" 8 | "github.com/tylertreat/bench/requester" 9 | ) 10 | 11 | func main() { 12 | r := &requester.NATSRequesterFactory{ 13 | URL: "nats://localhost:4222", 14 | PayloadSize: 500, 15 | Subject: "benchmark", 16 | } 17 | 18 | benchmark := bench.NewBenchmark(r, 10000, 1, 30*time.Second, 0) 19 | summary, err := benchmark.Run() 20 | if err != nil { 21 | panic(err) 22 | } 23 | 24 | fmt.Println(summary) 25 | summary.GenerateLatencyDistribution(nil, "nats.txt") 26 | } 27 | -------------------------------------------------------------------------------- /requester/noop.go: -------------------------------------------------------------------------------- 1 | package requester 2 | 3 | import ( 4 | "sync" 5 | 6 | "github.com/tylertreat/bench" 7 | ) 8 | 9 | type NOOPRequesterFactory struct { 10 | } 11 | 12 | var instance bench.Requester 13 | var once sync.Once 14 | 15 | func (n *NOOPRequesterFactory) GetRequester(num uint64) bench.Requester { 16 | once.Do(func() { 17 | instance = &noopRequester{} 18 | }) 19 | return instance 20 | } 21 | 22 | type noopRequester struct { 23 | } 24 | 25 | func (n *noopRequester) Setup() error { 26 | return nil 27 | } 28 | 29 | func (n *noopRequester) Request() error { 30 | return nil 31 | } 32 | 33 | func (n *noopRequester) Teardown() error { 34 | return nil 35 | } 36 | -------------------------------------------------------------------------------- /requester/web_requester.go: -------------------------------------------------------------------------------- 1 | package requester 2 | 3 | import ( 4 | "net/http" 5 | 6 | "github.com/tylertreat/bench" 7 | ) 8 | 9 | // WebRequesterFactory implements RequesterFactory by creating a Requester 10 | // which makes GET requests to the provided URL. 11 | type WebRequesterFactory struct { 12 | URL string 13 | } 14 | 15 | // GetRequester returns a new Requester, called for each Benchmark connection. 16 | func (w *WebRequesterFactory) GetRequester(uint64) bench.Requester { 17 | return &webRequester{w.URL} 18 | } 19 | 20 | // webRequester implements Requester by making a GET request to the provided 21 | // URL. 22 | type webRequester struct { 23 | url string 24 | } 25 | 26 | // Setup prepares the Requester for benchmarking. 27 | func (w *webRequester) Setup() error { return nil } 28 | 29 | // Request performs a synchronous request to the system under test. 30 | func (w *webRequester) Request() error { 31 | resp, err := http.Get(w.url) 32 | resp.Body.Close() 33 | return err 34 | } 35 | 36 | // Teardown is called upon benchmark completion. 37 | func (w *webRequester) Teardown() error { return nil } 38 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # bench [![GoDoc](https://godoc.org/github.com/tylertreat/bench?status.svg)](https://godoc.org/github.com/tylertreat/bench) 2 | 3 | Bench is a generic latency benchmarking library. It's generic in the sense that it exposes a simple interface (`Requester`) which can be implemented for various systems under test. Several [example Requesters](https://github.com/tylertreat/bench/tree/master/requester) are provided out of the box. 4 | 5 | Bench works by attempting to issue a fixed rate of requests per second and measuring the latency of each request issued synchronously. Latencies are captured using [HDR Histogram](https://github.com/codahale/hdrhistogram), which observes the complete latency distribution and attempts to correct for [Coordinated Omission](https://groups.google.com/forum/#!msg/mechanical-sympathy/icNZJejUHfE/BfDekfBEs_sJ). It provides facilities to generate output which can be [plotted](http://hdrhistogram.github.io/HdrHistogram/plotFiles.html) to produce graphs like the following: 6 | 7 | ![Latency Distribution](distribution.png) 8 | 9 | ## Example Benchmark 10 | 11 | ```go 12 | package main 13 | 14 | import ( 15 | "fmt" 16 | "time" 17 | 18 | "github.com/tylertreat/bench" 19 | "github.com/tylertreat/bench/requester" 20 | ) 21 | 22 | func main() { 23 | r := &requester.RedisPubSubRequesterFactory{ 24 | URL: ":6379", 25 | PayloadSize: 500, 26 | Channel: "benchmark", 27 | } 28 | 29 | benchmark := bench.NewBenchmark(r, 10000, 1, 30*time.Second, 0) 30 | summary, err := benchmark.Run() 31 | if err != nil { 32 | panic(err) 33 | } 34 | 35 | fmt.Println(summary) 36 | summary.GenerateLatencyDistribution(nil, "redis.txt") 37 | } 38 | ``` 39 | -------------------------------------------------------------------------------- /requester/cassandra_requester.go: -------------------------------------------------------------------------------- 1 | package requester 2 | 3 | import ( 4 | "github.com/gocql/gocql" 5 | "github.com/tylertreat/bench" 6 | ) 7 | 8 | // CassandraRequesterFactory implements RequesterFactory by creating a 9 | // Requester which issues queries to Cassandra. Works with Cassandra 2.x.x. 10 | type CassandraRequesterFactory struct { 11 | URLs []string 12 | Keyspace string 13 | Consistency gocql.Consistency 14 | Statement string 15 | Values []interface{} 16 | } 17 | 18 | // GetRequester returns a new Requester, called for each Benchmark connection. 19 | func (c *CassandraRequesterFactory) GetRequester(uint64) bench.Requester { 20 | return &cassandraRequester{ 21 | urls: c.URLs, 22 | keyspace: c.Keyspace, 23 | consistency: c.Consistency, 24 | statement: c.Statement, 25 | values: c.Values, 26 | } 27 | } 28 | 29 | // cassandraRequester implements Requester by issuing a query to Cassandra. 30 | // Works with Cassandra 2.x.x. 31 | type cassandraRequester struct { 32 | urls []string 33 | keyspace string 34 | consistency gocql.Consistency 35 | statement string 36 | values []interface{} 37 | session *gocql.Session 38 | } 39 | 40 | // Setup prepares the Requester for benchmarking. 41 | func (c *cassandraRequester) Setup() error { 42 | cluster := gocql.NewCluster(c.urls...) 43 | cluster.Keyspace = c.keyspace 44 | cluster.Consistency = c.consistency 45 | session, err := cluster.CreateSession() 46 | if err != nil { 47 | return err 48 | } 49 | c.session = session 50 | return nil 51 | } 52 | 53 | // Request performs a synchronous request to the system under test. 54 | func (c *cassandraRequester) Request() error { 55 | return c.session.Query(c.statement, c.values...).Exec() 56 | } 57 | 58 | // Teardown is called upon benchmark completion. 59 | func (c *cassandraRequester) Teardown() error { 60 | c.session.Close() 61 | c.session = nil 62 | return nil 63 | } 64 | -------------------------------------------------------------------------------- /requester/nats_requester.go: -------------------------------------------------------------------------------- 1 | package requester 2 | 3 | import ( 4 | "strconv" 5 | "time" 6 | 7 | "github.com/nats-io/go-nats" 8 | "github.com/tylertreat/bench" 9 | ) 10 | 11 | // NATSRequesterFactory implements RequesterFactory by creating a Requester 12 | // which publishes messages to NATS and waits to receive them. 13 | type NATSRequesterFactory struct { 14 | URL string 15 | PayloadSize int 16 | Subject string 17 | } 18 | 19 | // GetRequester returns a new Requester, called for each Benchmark connection. 20 | func (n *NATSRequesterFactory) GetRequester(num uint64) bench.Requester { 21 | return &natsRequester{ 22 | url: n.URL, 23 | payloadSize: n.PayloadSize, 24 | subject: n.Subject + "-" + strconv.FormatUint(num, 10), 25 | } 26 | } 27 | 28 | // natsRequester implements Requester by publishing a message to NATS and 29 | // waiting to receive it. 30 | type natsRequester struct { 31 | url string 32 | payloadSize int 33 | subject string 34 | conn *nats.Conn 35 | sub *nats.Subscription 36 | msg []byte 37 | } 38 | 39 | // Setup prepares the Requester for benchmarking. 40 | func (n *natsRequester) Setup() error { 41 | conn, err := nats.Connect(n.url) 42 | if err != nil { 43 | return err 44 | } 45 | sub, err := conn.SubscribeSync(n.subject) 46 | if err != nil { 47 | conn.Close() 48 | return err 49 | } 50 | n.conn = conn 51 | n.sub = sub 52 | n.msg = make([]byte, n.payloadSize) 53 | return nil 54 | } 55 | 56 | // Request performs a synchronous request to the system under test. 57 | func (n *natsRequester) Request() error { 58 | if err := n.conn.Publish(n.subject, n.msg); err != nil { 59 | return err 60 | } 61 | _, err := n.sub.NextMsg(30 * time.Second) 62 | return err 63 | } 64 | 65 | // Teardown is called upon benchmark completion. 66 | func (n *natsRequester) Teardown() error { 67 | if err := n.sub.Unsubscribe(); err != nil { 68 | return err 69 | } 70 | n.sub = nil 71 | n.conn.Close() 72 | n.conn = nil 73 | return nil 74 | } 75 | -------------------------------------------------------------------------------- /requester/nats_streaming_requester.go: -------------------------------------------------------------------------------- 1 | package requester 2 | 3 | import ( 4 | "errors" 5 | "fmt" 6 | "math/rand" 7 | "strconv" 8 | "time" 9 | 10 | "github.com/nats-io/go-nats-streaming" 11 | "github.com/tylertreat/bench" 12 | ) 13 | 14 | // NATSStreamingRequesterFactory implements RequesterFactory by creating a 15 | // Requester which publishes messages to NATS Streaming and waits to receive 16 | // them. 17 | type NATSStreamingRequesterFactory struct { 18 | PayloadSize int 19 | Subject string 20 | ClientID string 21 | URL string 22 | } 23 | 24 | // GetRequester returns a new Requester, called for each Benchmark connection. 25 | func (n *NATSStreamingRequesterFactory) GetRequester(num uint64) bench.Requester { 26 | return &natsStreamingRequester{ 27 | url: n.URL, 28 | clientID: n.ClientID, 29 | payloadSize: n.PayloadSize, 30 | subject: n.Subject + "-" + strconv.FormatUint(num, 10), 31 | } 32 | } 33 | 34 | // natsStreamingRequester implements Requester by publishing a message to NATS 35 | // Streaming and waiting to receive it. 36 | type natsStreamingRequester struct { 37 | url string 38 | clientID string 39 | payloadSize int 40 | subject string 41 | conn stan.Conn 42 | sub stan.Subscription 43 | msg []byte 44 | msgChan chan []byte 45 | } 46 | 47 | // Setup prepares the Requester for benchmarking. 48 | func (n *natsStreamingRequester) Setup() error { 49 | conn, err := stan.Connect("test-cluster", fmt.Sprintf("%s-%d", n.clientID, time.Now().UnixNano()), stan.NatsURL(n.url)) 50 | if err != nil { 51 | return err 52 | } 53 | n.msgChan = make(chan []byte) 54 | sub, err := conn.Subscribe(n.subject, func(msg *stan.Msg) { 55 | n.msgChan <- msg.Data 56 | }) 57 | if err != nil { 58 | conn.Close() 59 | return err 60 | } 61 | n.conn = conn 62 | n.sub = sub 63 | n.msg = make([]byte, n.payloadSize) 64 | for i := 0; i < n.payloadSize; i++ { 65 | n.msg[i] = 'A' + uint8(rand.Intn(26)) 66 | } 67 | return nil 68 | } 69 | 70 | // Request performs a synchronous request to the system under test. 71 | func (n *natsStreamingRequester) Request() error { 72 | if _, err := n.conn.PublishAsync(n.subject, n.msg, nil); err != nil { 73 | return err 74 | } 75 | select { 76 | case <-n.msgChan: 77 | return nil 78 | case <-time.After(30 * time.Second): 79 | return errors.New("timeout") 80 | } 81 | } 82 | 83 | // Teardown is called upon benchmark completion. 84 | func (n *natsStreamingRequester) Teardown() error { 85 | if err := n.sub.Unsubscribe(); err != nil { 86 | return err 87 | } 88 | n.sub = nil 89 | n.conn.Close() 90 | n.conn = nil 91 | return nil 92 | } 93 | -------------------------------------------------------------------------------- /requester/nsq_requester.go: -------------------------------------------------------------------------------- 1 | package requester 2 | 3 | import ( 4 | "errors" 5 | "math/rand" 6 | "strconv" 7 | "time" 8 | 9 | "github.com/nsqio/go-nsq" 10 | "github.com/tylertreat/bench" 11 | ) 12 | 13 | // NSQRequesterFactory implements RequesterFactory by creating a Requester 14 | // which publishes messages to NSQ and waits to receive them. 15 | type NSQRequesterFactory struct { 16 | URL string 17 | PayloadSize int 18 | Topic string 19 | } 20 | 21 | // GetRequester returns a new Requester, called for each Benchmark connection. 22 | func (n *NSQRequesterFactory) GetRequester(num uint64) bench.Requester { 23 | return &nsqRequester{ 24 | url: n.URL, 25 | payloadSize: n.PayloadSize, 26 | topic: n.Topic + "-" + strconv.FormatUint(num, 10), 27 | channel: n.Topic + "-" + strconv.FormatUint(num, 10), 28 | } 29 | } 30 | 31 | // nsqRequester implements Requester by publishing a message to NSQ and 32 | // waiting to receive it. 33 | type nsqRequester struct { 34 | url string 35 | payloadSize int 36 | topic string 37 | channel string 38 | producer *nsq.Producer 39 | consumer *nsq.Consumer 40 | msg []byte 41 | msgChan chan []byte 42 | } 43 | 44 | // Setup prepares the Requester for benchmarking. 45 | func (n *nsqRequester) Setup() error { 46 | config := nsq.NewConfig() 47 | producer, err := nsq.NewProducer(n.url, config) 48 | if err != nil { 49 | return err 50 | } 51 | consumer, err := nsq.NewConsumer(n.topic, n.channel, config) 52 | if err != nil { 53 | return err 54 | } 55 | n.msgChan = make(chan []byte) 56 | consumer.AddConcurrentHandlers(nsq.HandlerFunc(func(m *nsq.Message) error { 57 | m.Finish() 58 | n.msgChan <- m.Body 59 | return nil 60 | }), 1) 61 | if err := consumer.ConnectToNSQD(n.url); err != nil { 62 | producer.Stop() 63 | return err 64 | } 65 | n.producer = producer 66 | n.consumer = consumer 67 | n.msg = make([]byte, n.payloadSize) 68 | for i := 0; i < n.payloadSize; i++ { 69 | n.msg[i] = 'A' + uint8(rand.Intn(26)) 70 | } 71 | return nil 72 | } 73 | 74 | // Request performs a synchronous request to the system under test. 75 | func (n *nsqRequester) Request() error { 76 | if err := n.producer.Publish(n.topic, n.msg); err != nil { 77 | return err 78 | } 79 | select { 80 | case <-n.msgChan: 81 | return nil 82 | case <-time.After(30 * time.Second): 83 | return errors.New("timeout") 84 | } 85 | } 86 | 87 | // Teardown is called upon benchmark completion. 88 | func (n *nsqRequester) Teardown() error { 89 | if err := n.consumer.DisconnectFromNSQD(n.url); err != nil { 90 | return err 91 | } 92 | n.producer.Stop() 93 | 94 | n.consumer = nil 95 | n.producer = nil 96 | return nil 97 | } 98 | -------------------------------------------------------------------------------- /requester/kafka_requester.go: -------------------------------------------------------------------------------- 1 | package requester 2 | 3 | import ( 4 | "errors" 5 | "math/rand" 6 | "strconv" 7 | "time" 8 | 9 | "github.com/Shopify/sarama" 10 | "github.com/tylertreat/bench" 11 | ) 12 | 13 | // KafkaRequesterFactory implements RequesterFactory by creating a Requester 14 | // which publishes messages to Kafka and waits to consume them. 15 | type KafkaRequesterFactory struct { 16 | URLs []string 17 | PayloadSize int 18 | Topic string 19 | } 20 | 21 | // GetRequester returns a new Requester, called for each Benchmark connection. 22 | func (k *KafkaRequesterFactory) GetRequester(num uint64) bench.Requester { 23 | return &kafkaRequester{ 24 | urls: k.URLs, 25 | payloadSize: k.PayloadSize, 26 | topic: k.Topic + "-" + strconv.FormatUint(num, 10), 27 | } 28 | } 29 | 30 | // kafkaRequester implements Requester by publishing a message to Kafka and 31 | // waiting to consume it. 32 | type kafkaRequester struct { 33 | urls []string 34 | payloadSize int 35 | topic string 36 | producer sarama.AsyncProducer 37 | consumer sarama.Consumer 38 | partitionConsumer sarama.PartitionConsumer 39 | msg *sarama.ProducerMessage 40 | } 41 | 42 | // Setup prepares the Requester for benchmarking. 43 | func (k *kafkaRequester) Setup() error { 44 | config := sarama.NewConfig() 45 | producer, err := sarama.NewAsyncProducer(k.urls, config) 46 | if err != nil { 47 | return err 48 | } 49 | 50 | consumer, err := sarama.NewConsumer(k.urls, nil) 51 | if err != nil { 52 | producer.Close() 53 | return err 54 | } 55 | partitionConsumer, err := consumer.ConsumePartition(k.topic, 0, sarama.OffsetNewest) 56 | if err != nil { 57 | producer.Close() 58 | consumer.Close() 59 | return err 60 | } 61 | 62 | k.producer = producer 63 | k.consumer = consumer 64 | k.partitionConsumer = partitionConsumer 65 | msg := make([]byte, k.payloadSize) 66 | for i := 0; i < k.payloadSize; i++ { 67 | msg[i] = 'A' + uint8(rand.Intn(26)) 68 | } 69 | k.msg = &sarama.ProducerMessage{ 70 | Topic: k.topic, 71 | Value: sarama.ByteEncoder(msg), 72 | } 73 | 74 | return nil 75 | } 76 | 77 | // Request performs a synchronous request to the system under test. 78 | func (k *kafkaRequester) Request() error { 79 | k.producer.Input() <- k.msg 80 | select { 81 | case <-k.partitionConsumer.Messages(): 82 | return nil 83 | case <-time.After(30 * time.Second): 84 | return errors.New("requester: Request timed out receiving") 85 | } 86 | } 87 | 88 | // Teardown is called upon benchmark completion. 89 | func (k *kafkaRequester) Teardown() error { 90 | if err := k.partitionConsumer.Close(); err != nil { 91 | return err 92 | } 93 | if err := k.consumer.Close(); err != nil { 94 | return err 95 | } 96 | if err := k.producer.Close(); err != nil { 97 | return err 98 | } 99 | k.partitionConsumer = nil 100 | k.consumer = nil 101 | k.producer = nil 102 | return nil 103 | } 104 | -------------------------------------------------------------------------------- /requester/amqp_requester.go: -------------------------------------------------------------------------------- 1 | package requester 2 | 3 | import ( 4 | "errors" 5 | "strconv" 6 | "time" 7 | 8 | "github.com/streadway/amqp" 9 | "github.com/tylertreat/bench" 10 | ) 11 | 12 | // AMQPRequesterFactory implements RequesterFactory by creating a Requester 13 | // which publishes messages to an AMQP exchange and waits to consume them. 14 | type AMQPRequesterFactory struct { 15 | URL string 16 | PayloadSize int 17 | Queue string 18 | Exchange string 19 | } 20 | 21 | // GetRequester returns a new Requester, called for each Benchmark connection. 22 | func (r *AMQPRequesterFactory) GetRequester(num uint64) bench.Requester { 23 | return &amqpRequester{ 24 | url: r.URL, 25 | payloadSize: r.PayloadSize, 26 | queueName: r.Queue + "-" + strconv.FormatUint(num, 10), 27 | exchangeName: r.Exchange + "-" + strconv.FormatUint(num, 10), 28 | } 29 | } 30 | 31 | // amqpRequester implements Requester by publishing a message to an AMQP 32 | // exhcnage and waiting to consume it. 33 | type amqpRequester struct { 34 | url string 35 | payloadSize int 36 | queueName string 37 | exchangeName string 38 | conn *amqp.Connection 39 | queue amqp.Queue 40 | channel *amqp.Channel 41 | inbound <-chan amqp.Delivery 42 | msg amqp.Publishing 43 | } 44 | 45 | // Setup prepares the Requester for benchmarking. 46 | func (r *amqpRequester) Setup() error { 47 | conn, err := amqp.Dial(r.url) 48 | if err != nil { 49 | return err 50 | } 51 | c, err := conn.Channel() 52 | if err != nil { 53 | return err 54 | } 55 | queue, err := c.QueueDeclare( 56 | r.queueName, // name 57 | false, // not durable 58 | false, // delete when unused 59 | true, // exclusive 60 | false, // no wait 61 | nil, // arguments 62 | ) 63 | if err != nil { 64 | return err 65 | } 66 | err = c.ExchangeDeclare( 67 | r.exchangeName, // name 68 | "fanout", // type 69 | false, // not durable 70 | false, // auto-deleted 71 | false, // internal 72 | false, // no wait 73 | nil, // arguments 74 | ) 75 | if err != nil { 76 | return err 77 | } 78 | err = c.QueueBind( 79 | r.queueName, 80 | r.queueName, 81 | r.exchangeName, 82 | false, 83 | nil, 84 | ) 85 | if err != nil { 86 | return err 87 | } 88 | 89 | inbound, err := c.Consume( 90 | r.queueName, // queue 91 | "", // consumer 92 | true, // auto ack 93 | false, // exclusive 94 | true, // no local 95 | false, // no wait 96 | nil, // args 97 | ) 98 | if err != nil { 99 | return err 100 | } 101 | r.conn = conn 102 | r.queue = queue 103 | r.channel = c 104 | r.inbound = inbound 105 | r.msg = amqp.Publishing{ 106 | DeliveryMode: amqp.Transient, 107 | Timestamp: time.Now(), 108 | ContentType: "text/plain", 109 | Body: make([]byte, r.payloadSize), 110 | } 111 | return nil 112 | } 113 | 114 | // Request performs a synchronous request to the system under test. 115 | func (r *amqpRequester) Request() error { 116 | if err := r.channel.Publish( 117 | r.exchangeName, // exchange 118 | "", // routing key 119 | false, // mandatory 120 | false, // immediate 121 | r.msg, 122 | ); err != nil { 123 | return err 124 | } 125 | select { 126 | case <-r.inbound: 127 | case <-time.After(30 * time.Second): 128 | return errors.New("requester: Request timed out receiving") 129 | } 130 | return nil 131 | } 132 | 133 | // Teardown is called upon benchmark completion. 134 | func (r *amqpRequester) Teardown() error { 135 | if err := r.channel.Close(); err != nil { 136 | return err 137 | } 138 | return r.conn.Close() 139 | } 140 | -------------------------------------------------------------------------------- /summary.go: -------------------------------------------------------------------------------- 1 | package bench 2 | 3 | import ( 4 | "fmt" 5 | "time" 6 | 7 | "github.com/codahale/hdrhistogram" 8 | "github.com/tylertreat/hdrhistogram-writer" 9 | ) 10 | 11 | // Summary contains the results of a Benchmark run. 12 | type Summary struct { 13 | Connections uint64 14 | RequestRate uint64 15 | SuccessTotal uint64 16 | ErrorTotal uint64 17 | TimeElapsed time.Duration 18 | SuccessHistogram *hdrhistogram.Histogram 19 | UncorrectedSuccessHistogram *hdrhistogram.Histogram 20 | ErrorHistogram *hdrhistogram.Histogram 21 | UncorrectedErrorHistogram *hdrhistogram.Histogram 22 | Throughput float64 23 | } 24 | 25 | // String returns a stringified version of the Summary. 26 | func (s *Summary) String() string { 27 | return fmt.Sprintf( 28 | "\n{Connections: %d, RequestRate: %d, RequestTotal: %d, SuccessTotal: %d, ErrorTotal: %d, TimeElapsed: %s, Throughput: %.2f/s}", 29 | s.Connections, s.RequestRate, (s.SuccessTotal + s.ErrorTotal), s.SuccessTotal, s.ErrorTotal, s.TimeElapsed, s.Throughput) 30 | } 31 | 32 | // GenerateLatencyDistribution generates a text file containing the specified 33 | // latency distribution in a format plottable by 34 | // http://hdrhistogram.github.io/HdrHistogram/plotFiles.html. Percentiles is a 35 | // list of percentiles to include, e.g. 10.0, 50.0, 99.0, 99.99, etc. If 36 | // percentiles is nil, it defaults to a logarithmic percentile scale. If a 37 | // request rate was specified for the benchmark, this will also generate an 38 | // uncorrected distribution file which does not account for coordinated 39 | // omission. 40 | func (s *Summary) GenerateLatencyDistribution(percentiles histwriter.Percentiles, file string) error { 41 | return generateLatencyDistribution(s.SuccessHistogram, s.UncorrectedSuccessHistogram, s.RequestRate, percentiles, file) 42 | } 43 | 44 | // GenerateErrorLatencyDistribution generates a text file containing the specified 45 | // latency distribution (of requests that resulted in errors) in a format plottable by 46 | // http://hdrhistogram.github.io/HdrHistogram/plotFiles.html. Percentiles is a 47 | // list of percentiles to include, e.g. 10.0, 50.0, 99.0, 99.99, etc. If 48 | // percentiles is nil, it defaults to a logarithmic percentile scale. If a 49 | // request rate was specified for the benchmark, this will also generate an 50 | // uncorrected distribution file which does not account for coordinated 51 | // omission. 52 | func (s *Summary) GenerateErrorLatencyDistribution(percentiles histwriter.Percentiles, file string) error { 53 | return generateLatencyDistribution(s.ErrorHistogram, s.UncorrectedErrorHistogram, s.RequestRate, percentiles, file) 54 | } 55 | 56 | func getOneByPercentile(percentile float64) float64 { 57 | if percentile < 100 { 58 | return 1 / (1 - (percentile / 100)) 59 | } 60 | return float64(10000000) 61 | } 62 | 63 | func generateLatencyDistribution(histogram, unHistogram *hdrhistogram.Histogram, requestRate uint64, percentiles histwriter.Percentiles, file string) error { 64 | scaleFactor := 0.000001 // Scale ns to ms. 65 | if err := histwriter.WriteDistributionFile(histogram, percentiles, scaleFactor, file); err != nil { 66 | return err 67 | } 68 | 69 | // Generate uncorrected distribution. 70 | if requestRate > 0 { 71 | if err := histwriter.WriteDistributionFile(unHistogram, percentiles, scaleFactor, fmt.Sprintf("uncorrected_%s", file)); err != nil { 72 | return err 73 | } 74 | } 75 | 76 | return nil 77 | } 78 | 79 | // merge the other Summary into this one. 80 | func (s *Summary) merge(o *Summary) { 81 | if o.TimeElapsed > s.TimeElapsed { 82 | s.TimeElapsed = o.TimeElapsed 83 | } 84 | s.SuccessHistogram.Merge(o.SuccessHistogram) 85 | s.UncorrectedSuccessHistogram.Merge(o.UncorrectedSuccessHistogram) 86 | s.ErrorHistogram.Merge(o.ErrorHistogram) 87 | s.UncorrectedErrorHistogram.Merge(o.UncorrectedErrorHistogram) 88 | s.SuccessTotal += o.SuccessTotal 89 | s.ErrorTotal += o.ErrorTotal 90 | s.Throughput += o.Throughput 91 | s.RequestRate += o.RequestRate 92 | } 93 | -------------------------------------------------------------------------------- /requester/redis_requester.go: -------------------------------------------------------------------------------- 1 | package requester 2 | 3 | import ( 4 | "strconv" 5 | 6 | "github.com/garyburd/redigo/redis" 7 | "github.com/tylertreat/bench" 8 | ) 9 | 10 | // RedisRequesterFactory implements RequesterFactory by creating a Requester 11 | // which sends the configured command and arguments to Redis and waits for the 12 | // reply. 13 | type RedisRequesterFactory struct { 14 | URL string 15 | Command string 16 | Args []interface{} 17 | } 18 | 19 | // GetRequester returns a new Requester, called for each Benchmark connection. 20 | func (r *RedisRequesterFactory) GetRequester(uint64) bench.Requester { 21 | return &redisRequester{ 22 | url: r.URL, 23 | command: r.Command, 24 | args: r.Args, 25 | } 26 | } 27 | 28 | // redisRequester implements Requester by sending the configured command and 29 | // arguments to Redis and waiting for the reply. 30 | type redisRequester struct { 31 | url string 32 | command string 33 | args []interface{} 34 | conn redis.Conn 35 | } 36 | 37 | // Setup prepares the Requester for benchmarking. 38 | func (r *redisRequester) Setup() error { 39 | conn, err := redis.Dial("tcp", r.url) 40 | if err != nil { 41 | return err 42 | } 43 | r.conn = conn 44 | return nil 45 | } 46 | 47 | // Request performs a synchronous request to the system under test. 48 | func (r *redisRequester) Request() error { 49 | _, err := r.conn.Do(r.command, r.args...) 50 | return err 51 | } 52 | 53 | // Teardown is called upon benchmark completion. 54 | func (r *redisRequester) Teardown() error { 55 | if err := r.conn.Close(); err != nil { 56 | return err 57 | } 58 | r.conn = nil 59 | return nil 60 | } 61 | 62 | // RedisPubSubRequesterFactory implements RequesterFactory by creating a 63 | // Requester which publishes messages to Redis and waits to receive them. 64 | type RedisPubSubRequesterFactory struct { 65 | URL string 66 | PayloadSize int 67 | Channel string 68 | } 69 | 70 | // redisPubSubRequester implements Requester by publishing a message to Redis 71 | // and waiting to receive it. 72 | type redisPubSubRequester struct { 73 | url string 74 | payloadSize int 75 | channel string 76 | publishConn redis.Conn 77 | subscribeConn *redis.PubSubConn 78 | msg string 79 | } 80 | 81 | // GetRequester returns a new Requester, called for each Benchmark connection. 82 | func (r *RedisPubSubRequesterFactory) GetRequester(num uint64) bench.Requester { 83 | return &redisPubSubRequester{ 84 | url: r.URL, 85 | payloadSize: r.PayloadSize, 86 | channel: r.Channel + "-" + strconv.FormatUint(num, 10), 87 | } 88 | } 89 | 90 | // Setup prepares the Requester for benchmarking. 91 | func (r *redisPubSubRequester) Setup() error { 92 | pubConn, err := redis.Dial("tcp", r.url) 93 | if err != nil { 94 | return err 95 | } 96 | subConn, err := redis.Dial("tcp", r.url) 97 | if err != nil { 98 | return err 99 | } 100 | subscribeConn := &redis.PubSubConn{subConn} 101 | if err := subscribeConn.Subscribe(r.channel); err != nil { 102 | subscribeConn.Close() 103 | return err 104 | } 105 | // Receive subscription message. 106 | switch recv := subscribeConn.Receive().(type) { 107 | case error: 108 | return recv 109 | } 110 | r.publishConn = pubConn 111 | r.subscribeConn = subscribeConn 112 | r.msg = string(make([]byte, r.payloadSize)) 113 | return nil 114 | } 115 | 116 | // Request performs a synchronous request to the system under test. 117 | func (r *redisPubSubRequester) Request() error { 118 | if err := r.publishConn.Send("PUBLISH", r.channel, r.msg); err != nil { 119 | return err 120 | } 121 | if err := r.publishConn.Flush(); err != nil { 122 | return err 123 | } 124 | switch recv := r.subscribeConn.Receive().(type) { 125 | case error: 126 | return recv 127 | default: 128 | return nil 129 | } 130 | } 131 | 132 | // Teardown is called upon benchmark completion. 133 | func (r *redisPubSubRequester) Teardown() error { 134 | if err := r.publishConn.Close(); err != nil { 135 | return err 136 | } 137 | r.publishConn = nil 138 | if err := r.subscribeConn.Unsubscribe(r.channel); err != nil { 139 | return err 140 | } 141 | if err := r.subscribeConn.Close(); err != nil { 142 | return err 143 | } 144 | r.subscribeConn = nil 145 | return nil 146 | } 147 | -------------------------------------------------------------------------------- /bench.go: -------------------------------------------------------------------------------- 1 | /* 2 | Package bench provides a generic framework for performing latency benchmarks. 3 | */ 4 | package bench 5 | 6 | import ( 7 | "context" 8 | "math" 9 | "sync" 10 | "time" 11 | 12 | "golang.org/x/time/rate" 13 | 14 | "github.com/codahale/hdrhistogram" 15 | ) 16 | 17 | const ( 18 | maxRecordableLatencyNS = 300000000000 19 | sigFigs = 5 20 | defaultBurst = 1000 21 | ) 22 | 23 | // RequesterFactory creates new Requesters. 24 | type RequesterFactory interface { 25 | // GetRequester returns a new Requester, called for each Benchmark 26 | // connection. 27 | GetRequester(number uint64) Requester 28 | } 29 | 30 | // Requester synchronously issues requests for a particular system under test. 31 | type Requester interface { 32 | // Setup prepares the Requester for benchmarking. 33 | Setup() error 34 | 35 | // Request performs a synchronous request to the system under test. 36 | Request() error 37 | 38 | // Teardown is called upon benchmark completion. 39 | Teardown() error 40 | } 41 | 42 | // Benchmark performs a system benchmark by attempting to issue requests at a 43 | // specified rate and capturing the latency distribution. The request rate is 44 | // divided across the number of configured connections. 45 | type Benchmark struct { 46 | connections uint64 47 | benchmarks []*connectionBenchmark 48 | } 49 | 50 | // NewBenchmark creates a Benchmark which runs a system benchmark using the 51 | // given RequesterFactory. The requestRate argument specifies the number of 52 | // requests per second to issue. This value is divided across the number of 53 | // connections specified, so if requestRate is 50,000 and connections is 10, 54 | // each connection will attempt to issue 5,000 requests per second. A zero 55 | // value disables rate limiting entirely. The duration argument specifies how 56 | // long to run the benchmark. Requests will be issued in bursts with the 57 | // specified burst rate. If burst == 0 then burst will be the lesser of 58 | // (0.1 * requestRate) and 1000 but at least 1. 59 | func NewBenchmark(factory RequesterFactory, requestRate, connections uint64, 60 | duration time.Duration, burst uint64) *Benchmark { 61 | 62 | if connections == 0 { 63 | connections = 1 64 | } 65 | 66 | benchmarks := make([]*connectionBenchmark, connections) 67 | for i := uint64(0); i < connections; i++ { 68 | benchmarks[i] = newConnectionBenchmark( 69 | factory.GetRequester(i), requestRate/connections, duration, burst) 70 | } 71 | 72 | return &Benchmark{connections: connections, benchmarks: benchmarks} 73 | } 74 | 75 | // Run the benchmark and return a summary of the results. An error is returned 76 | // if something went wrong along the way. 77 | func (b *Benchmark) Run() (*Summary, error) { 78 | var ( 79 | start = make(chan struct{}) 80 | results = make(chan *result, b.connections) 81 | wg sync.WaitGroup 82 | ) 83 | 84 | // Prepare connection benchmarks 85 | for _, benchmark := range b.benchmarks { 86 | if err := benchmark.setup(); err != nil { 87 | return nil, err 88 | } 89 | wg.Add(1) 90 | go func(b *connectionBenchmark) { 91 | <-start 92 | results <- b.run() 93 | wg.Done() 94 | }(benchmark) 95 | } 96 | 97 | // Start benchmark 98 | close(start) 99 | 100 | // Wait for completion 101 | wg.Wait() 102 | 103 | // Teardown 104 | for _, benchmark := range b.benchmarks { 105 | if err := benchmark.teardown(); err != nil { 106 | return nil, err 107 | } 108 | } 109 | 110 | // Merge results 111 | result := <-results 112 | if result.err != nil { 113 | return nil, result.err 114 | } 115 | summary := result.summary 116 | for i := uint64(1); i < b.connections; i++ { 117 | result = <-results 118 | if result.err != nil { 119 | return nil, result.err 120 | } 121 | summary.merge(result.summary) 122 | } 123 | summary.Connections = b.connections 124 | 125 | return summary, nil 126 | } 127 | 128 | // result of a single connectionBenchmark run. 129 | type result struct { 130 | err error 131 | summary *Summary 132 | } 133 | 134 | // connectionBenchmark performs a system benchmark by issuing requests at a 135 | // specified rate and capturing the latency distribution. 136 | type connectionBenchmark struct { 137 | requester Requester 138 | requestRate uint64 139 | duration time.Duration 140 | expectedInterval time.Duration 141 | successHistogram *hdrhistogram.Histogram 142 | uncorrectedSuccessHistogram *hdrhistogram.Histogram 143 | errorHistogram *hdrhistogram.Histogram 144 | uncorrectedErrorHistogram *hdrhistogram.Histogram 145 | successTotal uint64 146 | errorTotal uint64 147 | elapsed time.Duration 148 | burst int 149 | } 150 | 151 | // newConnectionBenchmark creates a connectionBenchmark which runs a system 152 | // benchmark using the given Requester. The requestRate argument specifies the 153 | // number of requests per second to issue. A zero value disables rate limiting 154 | // entirely. The duration argument specifies how long to run the benchmark. 155 | func newConnectionBenchmark(requester Requester, requestRate uint64, duration time.Duration, burst uint64) *connectionBenchmark { 156 | var interval time.Duration 157 | if requestRate > 0 { 158 | interval = time.Duration(1000000000 / requestRate) 159 | } 160 | 161 | if burst == 0 { 162 | 163 | // burst is at least 1 - otherwise it's the smaller of DefaultBurst and 10% of requestRate 164 | burst = uint64(math.Max(1, math.Min(float64(requestRate)*0.1, float64(defaultBurst)))) 165 | } 166 | 167 | return &connectionBenchmark{ 168 | requester: requester, 169 | requestRate: requestRate, 170 | duration: duration, 171 | expectedInterval: interval, 172 | successHistogram: hdrhistogram.New(1, maxRecordableLatencyNS, sigFigs), 173 | uncorrectedSuccessHistogram: hdrhistogram.New(1, maxRecordableLatencyNS, sigFigs), 174 | errorHistogram: hdrhistogram.New(1, maxRecordableLatencyNS, sigFigs), 175 | uncorrectedErrorHistogram: hdrhistogram.New(1, maxRecordableLatencyNS, sigFigs), 176 | burst: int(burst), 177 | } 178 | } 179 | 180 | // setup prepares the benchmark for running. 181 | func (c *connectionBenchmark) setup() error { 182 | c.successHistogram.Reset() 183 | c.uncorrectedSuccessHistogram.Reset() 184 | c.errorHistogram.Reset() 185 | c.uncorrectedErrorHistogram.Reset() 186 | c.successTotal = 0 187 | c.errorTotal = 0 188 | return c.requester.Setup() 189 | } 190 | 191 | // teardown cleans up any benchmark resources. 192 | func (c *connectionBenchmark) teardown() error { 193 | return c.requester.Teardown() 194 | } 195 | 196 | // run the benchmark and return the result. Result contains an error if 197 | // something went wrong along the way. 198 | func (c *connectionBenchmark) run() *result { 199 | var err error 200 | if c.requestRate == 0 { 201 | c.elapsed, err = c.runFullThrottle() 202 | } else { 203 | c.elapsed, err = c.runRateLimited() 204 | } 205 | return &result{summary: c.summarize(), err: err} 206 | } 207 | 208 | // runRateLimited runs the benchmark by attempting to issue the configured 209 | // number of requests per second. 210 | func (c *connectionBenchmark) runRateLimited() (time.Duration, error) { 211 | var ( 212 | interval = c.expectedInterval.Nanoseconds() 213 | stop = time.After(c.duration) 214 | start = time.Now() 215 | limit = rate.Every(c.expectedInterval) 216 | limiter = rate.NewLimiter(limit, c.burst) 217 | ctx = context.Background() 218 | ) 219 | for { 220 | select { 221 | case <-stop: 222 | return time.Since(start), nil 223 | default: 224 | } 225 | 226 | limiter.WaitN(ctx, c.burst) 227 | for i := 0; i < c.burst; i++ { 228 | before := time.Now() 229 | err := c.requester.Request() 230 | latency := time.Since(before).Nanoseconds() 231 | if err != nil { 232 | if err := c.errorHistogram.RecordCorrectedValue(latency, interval); err != nil { 233 | return 0, err 234 | } 235 | if err := c.uncorrectedErrorHistogram.RecordValue(latency); err != nil { 236 | return 0, err 237 | } 238 | c.errorTotal++ 239 | } else { 240 | if err := c.successHistogram.RecordCorrectedValue(latency, interval); err != nil { 241 | return 0, err 242 | } 243 | if err := c.uncorrectedSuccessHistogram.RecordValue(latency); err != nil { 244 | return 0, err 245 | } 246 | c.successTotal++ 247 | } 248 | } 249 | } 250 | } 251 | 252 | // runFullThrottle runs the benchmark without a limit on requests per second. 253 | func (c *connectionBenchmark) runFullThrottle() (time.Duration, error) { 254 | var ( 255 | stop = time.After(c.duration) 256 | start = time.Now() 257 | ) 258 | for { 259 | select { 260 | case <-stop: 261 | return time.Since(start), nil 262 | default: 263 | } 264 | 265 | before := time.Now() 266 | err := c.requester.Request() 267 | latency := time.Since(before).Nanoseconds() 268 | if err != nil { 269 | if err := c.errorHistogram.RecordValue(latency); err != nil { 270 | return 0, err 271 | } 272 | c.errorTotal++ 273 | } else { 274 | if err := c.successHistogram.RecordValue(latency); err != nil { 275 | return 0, err 276 | } 277 | c.successTotal++ 278 | } 279 | } 280 | } 281 | 282 | // summarize returns a Summary of the last benchmark run. 283 | func (c *connectionBenchmark) summarize() *Summary { 284 | return &Summary{ 285 | SuccessTotal: c.successTotal, 286 | ErrorTotal: c.errorTotal, 287 | TimeElapsed: c.elapsed, 288 | SuccessHistogram: hdrhistogram.Import(c.successHistogram.Export()), 289 | UncorrectedSuccessHistogram: hdrhistogram.Import(c.uncorrectedSuccessHistogram.Export()), 290 | ErrorHistogram: hdrhistogram.Import(c.errorHistogram.Export()), 291 | UncorrectedErrorHistogram: hdrhistogram.Import(c.uncorrectedErrorHistogram.Export()), 292 | Throughput: float64(c.successTotal+c.errorTotal) / c.elapsed.Seconds(), 293 | RequestRate: c.requestRate, 294 | } 295 | } 296 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | 203 | --------------------------------------------------------------------------------