This repository has been archived by the owner on Dec 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from grafana/amqp-init
Init AMQP k6 plugin
- Loading branch information
Showing
21 changed files
with
1,292 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
vendor |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,119 @@ | ||
package amqp | ||
|
||
import ( | ||
amqpDriver "github.com/streadway/amqp" | ||
"go.k6.io/k6/js/modules" | ||
) | ||
|
||
const version = "v0.0.1" | ||
|
||
type Amqp struct { | ||
Version string | ||
Version string | ||
Connection *amqpDriver.Connection | ||
Queue *Queue | ||
Exchange *Exchange | ||
} | ||
|
||
type AmqpOptions struct { | ||
ConnectionUrl string | ||
} | ||
|
||
type PublishOptions struct { | ||
QueueName string | ||
Body string | ||
Exchange string | ||
Mandatory bool | ||
Immediate bool | ||
} | ||
|
||
type ConsumeOptions struct { | ||
Consumer string | ||
AutoAck bool | ||
Exclusive bool | ||
NoLocal bool | ||
NoWait bool | ||
Args amqpDriver.Table | ||
} | ||
|
||
type ListenerType func(string) error | ||
|
||
type ListenOptions struct { | ||
Listener ListenerType | ||
QueueName string | ||
Consumer string | ||
AutoAck bool | ||
Exclusive bool | ||
NoLocal bool | ||
NoWait bool | ||
Args amqpDriver.Table | ||
} | ||
|
||
func (amqp *Amqp) Start(options AmqpOptions) error { | ||
conn, err := amqpDriver.Dial(options.ConnectionUrl) | ||
amqp.Connection = conn | ||
amqp.Queue.Connection = conn | ||
amqp.Exchange.Connection = conn | ||
return err | ||
} | ||
|
||
func (amqp *Amqp) Publish(options PublishOptions) error { | ||
ch, err := amqp.Connection.Channel() | ||
if err != nil { | ||
return err | ||
} | ||
defer ch.Close() | ||
|
||
return ch.Publish( | ||
options.Exchange, | ||
options.QueueName, | ||
options.Mandatory, | ||
options.Immediate, | ||
amqpDriver.Publishing{ | ||
ContentType: "text/plain", | ||
Body: []byte(options.Body), | ||
}, | ||
) | ||
} | ||
|
||
func (amqp *Amqp) Listen(options ListenOptions) error { | ||
ch, err := amqp.Connection.Channel() | ||
if err != nil { | ||
return err | ||
} | ||
defer ch.Close() | ||
|
||
msgs, err := ch.Consume( | ||
options.QueueName, | ||
options.Consumer, | ||
options.AutoAck, | ||
options.Exclusive, | ||
options.NoLocal, | ||
options.NoWait, | ||
options.Args, | ||
) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
go func() { | ||
for d := range msgs { | ||
options.Listener(string(d.Body)) | ||
} | ||
}() | ||
return nil | ||
} | ||
|
||
func init() { | ||
modules.Register("k6/x/amqp", &Amqp{ | ||
Version: version, | ||
}) | ||
|
||
queue := Queue{} | ||
exchange := Exchange{} | ||
generalAmqp := Amqp{ | ||
Version: version, | ||
Queue: &queue, | ||
Exchange: &exchange, | ||
} | ||
|
||
modules.Register("k6/x/amqp", &generalAmqp) | ||
modules.Register("k6/x/amqp/queue", &queue) | ||
modules.Register("k6/x/amqp/exchange", &exchange) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
#!/usr/bin/env sh | ||
|
||
xk6 build --with github.com/lxkuz/xk6-amqp=. | ||
xk6 build --with github.com/grafana/xk6-amqp=. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
package amqp | ||
|
||
import ( | ||
amqpDriver "github.com/streadway/amqp" | ||
) | ||
|
||
type Channels struct { | ||
Version string | ||
Connection *amqpDriver.Connection | ||
} | ||
|
||
type ChannelOptions struct { | ||
ConnectionUrl string | ||
} | ||
|
||
type ChannelDeclareOptions struct { | ||
Name string | ||
Kind string | ||
Durable bool | ||
AutoDelete bool | ||
Internal bool | ||
NoWait bool | ||
Args amqpDriver.Table | ||
} | ||
|
||
type ChannelBindOptions struct { | ||
DestinationChannelName string | ||
SourceChannelName string | ||
RoutingKey string | ||
NoWait bool | ||
Args amqpDriver.Table | ||
} | ||
|
||
type ChannelUnindOptions struct { | ||
DestinationChannelName string | ||
SourceChannelName string | ||
RoutingKey string | ||
NoWait bool | ||
Args amqpDriver.Table | ||
} | ||
|
||
func (Channels *Channels) Declare(options ChannelDeclareOptions) error { | ||
ch, err := Channels.Connection.Channel() | ||
if err != nil { | ||
return err | ||
} | ||
defer ch.Close() | ||
return ch.ChannelDeclare( | ||
options.Name, | ||
options.Kind, | ||
options.Durable, | ||
options.AutoDelete, | ||
options.Internal, | ||
options.NoWait, | ||
options.Args, | ||
) | ||
} | ||
|
||
func (Channels *Channels) Delete(name string) error { | ||
ch, err := Channels.Connection.Channel() | ||
if err != nil { | ||
return err | ||
} | ||
defer ch.Close() | ||
return ch.ChannelDelete( | ||
name, | ||
false, // ifUnused | ||
false, // noWait | ||
) | ||
} | ||
|
||
func (Channels *Channels) Bind(options ChannelBindOptions) error { | ||
ch, err := Channels.Connection.Channel() | ||
if err != nil { | ||
return err | ||
} | ||
defer ch.Close() | ||
return ch.ChannelBind( | ||
options.DestinationChannelName, | ||
options.RoutingKey, | ||
options.SourceChannelName, | ||
options.NoWait, | ||
options.Args, | ||
) | ||
} | ||
|
||
func (Channels *Channels) Unbind(options ChannelUnindOptions) error { | ||
ch, err := Channels.Connection.Channel() | ||
if err != nil { | ||
return err | ||
} | ||
defer ch.Close() | ||
return ch.ChannelUnbind( | ||
options.DestinationChannelName, | ||
options.RoutingKey, | ||
options.SourceChannelName, | ||
options.NoWait, | ||
options.Args, | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import Amqp from 'k6/x/amqp'; | ||
import Exchange from 'k6/x/amqp/exchange'; | ||
|
||
export default function () { | ||
const url = "amqp://guest:guest@localhost:5672/" | ||
Amqp.start({ | ||
connection_url: url | ||
}) | ||
|
||
const sourceExchangeName = 'K6 exchange' | ||
const destinationExchangeName = 'destination K6 exchange' | ||
|
||
Exchange.bind({ | ||
destination_exchange_name: destinationExchangeName, | ||
routing_key: '', | ||
source_exchange_name: sourceExchangeName, | ||
no_wait: false, | ||
args: null | ||
}) | ||
|
||
console.log(destinationExchangeName + " exchange binded to " + sourceExchangeName + ' exchange') | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import Amqp from 'k6/x/amqp'; | ||
import Queue from 'k6/x/amqp/queue'; | ||
|
||
export default function () { | ||
const url = "amqp://guest:guest@localhost:5672/" | ||
Amqp.start({ | ||
connection_url: url | ||
}) | ||
|
||
const queueName = 'K6 queue' | ||
const exchangeName = 'K6 exchange' | ||
|
||
Queue.bind({ | ||
queue_name: queueName, | ||
routing_key: '', | ||
exchange_name: exchangeName, | ||
no_wait: false, | ||
args: null | ||
}) | ||
|
||
console.log(queueName + " queue binded to " + exchangeName) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import Amqp from 'k6/x/amqp'; | ||
import Exchange from 'k6/x/amqp/exchange'; | ||
|
||
export default function () { | ||
const url = "amqp://guest:guest@localhost:5672/" | ||
Amqp.start({ | ||
connection_url: url | ||
}) | ||
|
||
console.log("Connection opened: " + url) | ||
|
||
const exchangeName = 'K6 exchange' | ||
|
||
Exchange.declare({ | ||
name: exchangeName, | ||
kind: 'direct', | ||
durable: false, | ||
auto_delete: false, | ||
internal: false, | ||
no_wait: false, | ||
args: null | ||
}) | ||
|
||
console.log(exchangeName + " exchange is ready") | ||
} |
Oops, something went wrong.