I am a newbie of GCP Pub/Sub, and I try to test the benchmarh of publish message.
we have two scenario below:
both scenario will publish 6 messages.
Scenario 1: create pubsubClient, then publish 6 message to the pubsub. After that kill the client.
Scenario 2: for each message, create new pubcubclient, after message published close the client.
the benchmark code can be found below:
func BenchmarkPublish(b *testing.B) {
os.Setenv("PUBSUB_EMULATOR_HOST", "localhost:8085")
ctx := context.Background()
client, err := pubsub.NewClient(ctx, ProjectID)
if err != nil {
log.Fatalln("can't create client: ", err)
}
defer client.Close()
topic := client.Topic(TopicID)
b.ResetTimer()
for i := 0; i < b.N; i++ {
for _, fruit := range fruits {
err := Publish(os.Stdout, ctx, TopicID, topic, fruit)
if err != nil {
log.Fatalln("can't publish message: ", err)
}
}
}
}
func BenchmarkPublishAndKillConnectionEachTime(b *testing.B) {
os.Setenv("PUBSUB_EMULATOR_HOST", "localhost:8085")
ctx := context.Background()
b.ResetTimer()
for i := 0; i < b.N; i++ {
for _, fruit := range fruits {
client, err := pubsub.NewClient(ctx, ProjectID)
if err != nil {
log.Fatalln("can't create client: ", err)
}
topic := client.Topic(TopicID)
err = Publish(os.Stdout, ctx, TopicID, topic, fruit)
if err != nil {
log.Fatalln("can't publish message: ", err)
}
client.Close()
}
}
}
This is Publish function:
func Publish(w io.Writer, ctx context.Context, topicID string, t *pubsub.Topic, msg string) error {
// projectID := "my-project-id"
// topicID := "my-topic"
// msg := "Hello World"
result := t.Publish(ctx, &pubsub.Message{
Data: []byte(msg),
})
id, err := result.Get(ctx)
if err != nil {
return fmt.Errorf("pubsub: result.Get: %w", err)
}
fmt.Fprintf(w, "Published a message; msg ID: %vn", id)
return nil
}
In my mind, the BenchmarkPublish
should run faster than BenchmarkPublishAndKillConnectionEachTime
. but Actually BenchmarkPublish
is slower one.
this is benchmark:
My question is Why the benchmark of killing connection each time is better than using single connection? In my view, If we do create and close client, it should consume a bit of compute resources. So it should slower than maintaining a PubsubClient until all publish operations finished.
jun yan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.