SlideShare a Scribd company logo
Scaling FastAGI Applications with Go 
Lefteris Zafiris NTA Ltd 
Astricon 2014 Las Vegas
Who we are 
VoIP engineer at NTA ltd 
Voice and text toolset 
- Flite Asterisk App 
- eSpeak Asterisk App 
- googleTTS 
- speech recognition - Google speech API 
- Text translation 
github.com/zaf
Who we are 
NTA Ltd - nta.co.uk 
- UK based VoIP provider since 2001 
- Hosted PBX services 
- SIP 
- FOSS 
- Kamailio / Asterisk 
- Perl
Perl stack 
- FastAGI server 
- Net::Server 
- fork / prefork 
- Memory 
- Capacity - calls / server
Enter Go 
2007 Google 
Rob Pike, Ken Thompson, Robert Griesemer 
- Statically typed 
- Garbage-collected 
- Natively compiled 
- Concurrency 
- C family 
- Pragmatic, minimalistic
Enter Go 
Do not communicate by sharing memory. 
Share memory by communicating. 
- Goroutines 
lightweight threads 
cheap 
multiplexed in OS threads 
- Channels 
communication 
synchronization
Enter Go 
/* 
A simple example in go 
*/ 
package main 
import "fmt" 
func main() { 
var msg string 
msg = "Hello Astricon" 
// Retroactively say hello to all Astricons! 
for i := 0; i <= 10; i++ { 
fmt.Println(msg, 2004+i) 
} 
}
Go toolset 
- go command 
test 
fmt 
build 
install 
- Debugging 
gdb 
- Package management 
go get
AGI Package 
- AGI and FastAGI support 
- Simple familiar interface 
- BSD Licence 
- Install: 
go get github.com/zaf/agi
AGI Package 
- Session structure 
AGI environment variables 
Env map[string]string 
- Reply structure 
Numeric result of the AGI command 
Res int 
Additional returned data 
Dat string
AGI Package 
- AGI commands as methods of the Session struct 
func (a *Session) Answer() (Reply, error) 
func (a *Session) SendText(text string) (Reply, 
error) 
func (a *Session) Hangup(channel ...string) 
(Reply, error) 
func (a *Session) SayDigits(digit int, 
escape string) (Reply, error)
AGI Package usage 
- Create a new session: 
myAgi := agi.New() 
- Initialize, read and parse AGI environment: 
myAgi.Init(nil) 
- Execute AGI commands: 
myAgi.StreamFile("hello-world", "#")
AGI example 
package main 
import ( 
"log" 
"github.com/zaf/agi" 
) 
func main() { 
// Create a new AGI session and Parse the AGI environment. 
myAgi := agi.New() 
err := myAgi.Init(nil) 
if err != nil { 
log.Fatalf("Error Parsing AGI environment: %vn", err) 
} 
// Print a message on the asterisk console using Verbose. 
_, err := myAgi.Verbose("Hello World") 
if err != nil { 
log.Fatalf("AGI reply error: %vn", err) 
} 
}
FastAGI Example 
func main() { 
// Create a tcp listener on port 4573 and start a new goroutine for each connection. 
ln, err := net.Listen("tcp", ":4573") 
if err != nil { 
log.Fatal(err) 
} 
defer ln.Close() 
for { 
conn, err := ln.Accept() 
if err != nil { 
log.Println(err) 
continue 
} 
go connHandle(conn) 
} 
}
FastAGI Example 
func connHandle(c net.Conn) { 
defer c.Close() 
myAgi := agi.New() 
// Create I/O buffers 
rw := bufio.NewReadWriter(bufio.NewReader(c), bufio.NewWriter(c)) 
err := myAgi.Init(rw) 
if err != nil { 
log.Println(err) 
return 
} 
rep, err := myAgi.StreamFile("hello", "1234567890#*") 
if err != nil { 
log.Println(err) 
return 
} 
// Check AGI command return value 
if rep.Res == -1 { 
log.Println("Failed to playback file") 
} 
}
Error Handling 
func connHandle(c net.Conn) { 
defer func() { 
c.Close() 
if err := recover(); err != nil { 
log.Println("Session terminated:", err) 
} 
}() 
myAgi := agi.New() 
rw := bufio.NewReadWriter(bufio.NewReader(c), bufio.NewWriter(c)) 
err := myAgi.Init(rw) 
checkErr(err) 
_, err := myAgi.Verbose("Hello World") 
checkErr(err) 
} 
//Check for AGI Protocol errors or hangups 
func checkErr(e error) { 
if e != nil { 
panic(e) 
} 
}
AGI Debug 
- Error testing 
- Performance testing 
- Security testing 
Eliminate the use of Asterisk 
Custom AGI Payloads
Agistress 
A client that can connect to servers using the Asterisk Gateway 
Interface 
- user defined payloads 
- user controlled session parameters 
- fast - parallel session spawning 
- performance measuring 
- written in Go 
go get github.com/zaf/agistress
Agistress 
Run once and display full debug output of the AGI session: 
agistress -host 127.0.0.1 -single 
Stress test the AGI server by spawning 10 sessions 10 times per 
second and display performance details: 
agistress -host 127.0.0.1 -sess 10 -runs 10 
Run once using custom AGI payload from config file: 
agistress -host 127.0.0.1 -single -conf payload.conf
Agistress 
Config file: 
- JSON format 
- Environment variables 
- AGI Command replies 
- Reply delay
Agistress 
Config file sample: 
{ 
"AgiPayload": [ 
{"Msg": "200 result=0", "Delay": 10}, 
{"Msg": "200 result=0", "Delay": 10}, 
{"Msg": "200 result=1 endpos=1234", "Delay": 3000}, 
{"Msg": "HANGUP"} 
] 
}
Agistress 
Sample output: 
Running FastAGI bench against: 192.168.1.10:4573 
Press Enter to stop. 
A new run each: 100ms 
Sessions per run: 10 
Reply delay: 50ms 
FastAGI Sessions 
Active: 20 
Completed: 520 
Duration: 206121479 ns (last 10000 sessions average) 
Failed: 0
Measuring 
Comparison between FastAGI implementations: 
- Perl: 
Asterisk::AGI 
Net::Server::PreFork 
- Go: 
agi package 
net package
Measuring 
Simple sound file Playback application: 
- Start session and parse AGI Env 
- Parse FastAGI request URI 
agi://10.11.12.13/myagi?file=echo-test 
- Check channel status 
- Answer channel 
- Playback given file 
Perl: 73 loc (http://goo.gl/hdsxuw) 
Go: 90 loc (http://goo.gl/uxezg7)
Benchmark 
Stress test using agistress: 
- 1 session/sec 
agistress -delay=0 
- 25 sessions/sec 
agistress -runs=5 -sess=5 -delay=0 
- 50 sessions/sec 
agistress -runs=10 -sess=5 -delay=0 
- 100 sessions/sec 
agistress -runs=10 -sess=10 -delay=0 
- 200 and 400 sessions/sec
Scaling FastAGI Applications with Go
Benchmark 
Memory usage: 
Many active sessions 
Custom AGI Payload, 5 sec playback duration 
- 12 active sessions: 
agistress -sess=2 -conf=sample.conf 
- 24 active sessions: 
agistress -sess=4 -conf=sample.conf 
- 48, 96 and 192 sessions
Scaling FastAGI Applications with Go
Results 
- Up to 3 times less CPU usage 
- Up to 2.5 times shorter runtime 
- Enormous memory savings
Thank You!

More Related Content

What's hot (20)

PDF
The Linux Block Layer - Built for Fast Storage
Kernel TLV
 
PDF
Secret Management with Hashicorp’s Vault
AWS Germany
 
PDF
Introduction to Kafka Streams
Guozhang Wang
 
PDF
Scale Kubernetes to support 50000 services
LinuxCon ContainerCon CloudOpen China
 
PDF
A brief introduction to version control systems
Tim Staley
 
PDF
Introduction and Deep Dive Into Containerd
Kohei Tokunaga
 
PPT
Jenkins Overview
Ahmed M. Gomaa
 
PDF
Kubernetes GitOps featuring GitHub, Kustomize and ArgoCD
Sunnyvale
 
PDF
Credential store using HashiCorp Vault
Mayank Patel
 
PDF
Ansible
Raul Leite
 
PDF
Ansible
Rahul Bajaj
 
PPTX
Best practices for ansible
George Shuklin
 
ODP
Expanding Asterisk with Kamailio
Fred Posner
 
PPTX
Hands on ansible
sumit23kumar
 
PDF
Cilium - BPF & XDP for containers
Docker, Inc.
 
ODP
Introduction to Ansible
Knoldus Inc.
 
TXT
Interview questions
xavier john
 
PDF
Docker Architecture (v1.3)
rajdeep
 
PDF
LCU13: An Introduction to ARM Trusted Firmware
Linaro
 
PPTX
Secret Management with Hashicorp Vault and Consul on Kubernetes
An Nguyen
 
The Linux Block Layer - Built for Fast Storage
Kernel TLV
 
Secret Management with Hashicorp’s Vault
AWS Germany
 
Introduction to Kafka Streams
Guozhang Wang
 
Scale Kubernetes to support 50000 services
LinuxCon ContainerCon CloudOpen China
 
A brief introduction to version control systems
Tim Staley
 
Introduction and Deep Dive Into Containerd
Kohei Tokunaga
 
Jenkins Overview
Ahmed M. Gomaa
 
Kubernetes GitOps featuring GitHub, Kustomize and ArgoCD
Sunnyvale
 
Credential store using HashiCorp Vault
Mayank Patel
 
Ansible
Raul Leite
 
Ansible
Rahul Bajaj
 
Best practices for ansible
George Shuklin
 
Expanding Asterisk with Kamailio
Fred Posner
 
Hands on ansible
sumit23kumar
 
Cilium - BPF & XDP for containers
Docker, Inc.
 
Introduction to Ansible
Knoldus Inc.
 
Interview questions
xavier john
 
Docker Architecture (v1.3)
rajdeep
 
LCU13: An Introduction to ARM Trusted Firmware
Linaro
 
Secret Management with Hashicorp Vault and Consul on Kubernetes
An Nguyen
 

Viewers also liked (20)

PPT
Lecture 16 Part 2
giskende
 
PDF
A Rational approach to application migration and modernization
IBM Rational software
 
PDF
Test Automation and Service Virtualization Services Offerings from Rational L...
IBM Rational software
 
PPT
Lecture 16 Part 1
giskende
 
PDF
vFutebol - Mídia Kit
Virtues Media & Applications
 
PPT
The red river_rebellions1869_1870[1]
Nicolemarie4
 
PDF
Optymalizacja procesu zarządzania komunikacją marki na platformach społecznoś...
NapoleonCat.com
 
DOC
Mitch pt3
mitchello44
 
PPT
Lecture 19
giskende
 
PPT
Portfolio research
David_Hickman
 
PDF
First5
Maria Lodenborg
 
PDF
Radar
Abi DanielDina
 
PPT
Oι φαναριώτες στην ελληνικη επανασταση. Εργασία στο μάθημα της ιστορίας. Γ΄Γυ...
Εύα Ζαρκογιάννη
 
PDF
Primaria 4-junio-sesion
Ruben Dario
 
PDF
Becker School District
Digium
 
PPTX
ΕΡΓΑΣΙΑ ΜΑΘΗΤΩΝ ΣΤΟ ΜΑΘΗΜΑ ΤΗΣ ΙΣΤΟΡΙΑΣ -ΥΠΕΥΘΥΝΗ ΚΑΘΗΓΗΤΡΙΑ ΖΑΡΚΟΓΙΑΝΝΗ ΕΥΑ
Εύα Ζαρκογιάννη
 
PPTX
Łódź - doskonałe miasto dla marketing miejsc
NapoleonCat.com
 
PPTX
Logging En Monitoring Presentatie Met Penetratie Testen 0.5
Ferdinand_u
 
DOCX
Newspaper Ideas
mitchello44
 
PDF
Napoleon. Raport aktywności branż na Facebooku - kwiecień 2012
NapoleonCat.com
 
Lecture 16 Part 2
giskende
 
A Rational approach to application migration and modernization
IBM Rational software
 
Test Automation and Service Virtualization Services Offerings from Rational L...
IBM Rational software
 
Lecture 16 Part 1
giskende
 
vFutebol - Mídia Kit
Virtues Media & Applications
 
The red river_rebellions1869_1870[1]
Nicolemarie4
 
Optymalizacja procesu zarządzania komunikacją marki na platformach społecznoś...
NapoleonCat.com
 
Mitch pt3
mitchello44
 
Lecture 19
giskende
 
Portfolio research
David_Hickman
 
Oι φαναριώτες στην ελληνικη επανασταση. Εργασία στο μάθημα της ιστορίας. Γ΄Γυ...
Εύα Ζαρκογιάννη
 
Primaria 4-junio-sesion
Ruben Dario
 
Becker School District
Digium
 
ΕΡΓΑΣΙΑ ΜΑΘΗΤΩΝ ΣΤΟ ΜΑΘΗΜΑ ΤΗΣ ΙΣΤΟΡΙΑΣ -ΥΠΕΥΘΥΝΗ ΚΑΘΗΓΗΤΡΙΑ ΖΑΡΚΟΓΙΑΝΝΗ ΕΥΑ
Εύα Ζαρκογιάννη
 
Łódź - doskonałe miasto dla marketing miejsc
NapoleonCat.com
 
Logging En Monitoring Presentatie Met Penetratie Testen 0.5
Ferdinand_u
 
Newspaper Ideas
mitchello44
 
Napoleon. Raport aktywności branż na Facebooku - kwiecień 2012
NapoleonCat.com
 
Ad

Similar to Scaling FastAGI Applications with Go (20)

PDF
An Introduction to Go
Cloudflare
 
PDF
Microservices in GO - Massimiliano Dessì - Codemotion Rome 2017
Codemotion
 
PDF
神に近づくx/net/context (Finding God with x/net/context)
guregu
 
PDF
ARI and AGI, a powerful combination
Jöran Vinzens
 
PDF
Networking and Go: An Epic Journey
Sneha Inguva
 
PDF
Goroutines and Channels in practice
Guilherme Garnier
 
PDF
Microservices in Go_Dessi_Massimiliano_Codemotion_2017_Rome
Massimiliano Dessì
 
PDF
Building Web APIs that Scale
Salesforce Developers
 
PDF
On the Edge Systems Administration with Golang
Chris McEniry
 
PDF
OSCON: System software goes weird
Docker, Inc.
 
PPTX
Go Concurrency Patterns
ElifTech
 
PPTX
Go from a PHP Perspective
Barry Jones
 
PDF
Go for Rubyists
tchandy
 
ODP
Phpconf 2013 - Agile Telephony Applications with PAMI and PAGI
Marcelo Gornstein
 
PDF
Taming Cloud APIs with Swift
Tim Burks
 
PDF
LCA2014 - Introduction to Go
dreamwidth
 
PDF
Ruby voip
Dieter Pisarewski
 
PDF
Asynchronous Architectures for Implementing Scalable Cloud Services - Evan Co...
Twilio Inc
 
PDF
Meetup talk: Readying your Go Webservice
Ralph Ligtenberg
 
PDF
Using Asterisk in a SIP softswitch
Monica McArthur
 
An Introduction to Go
Cloudflare
 
Microservices in GO - Massimiliano Dessì - Codemotion Rome 2017
Codemotion
 
神に近づくx/net/context (Finding God with x/net/context)
guregu
 
ARI and AGI, a powerful combination
Jöran Vinzens
 
Networking and Go: An Epic Journey
Sneha Inguva
 
Goroutines and Channels in practice
Guilherme Garnier
 
Microservices in Go_Dessi_Massimiliano_Codemotion_2017_Rome
Massimiliano Dessì
 
Building Web APIs that Scale
Salesforce Developers
 
On the Edge Systems Administration with Golang
Chris McEniry
 
OSCON: System software goes weird
Docker, Inc.
 
Go Concurrency Patterns
ElifTech
 
Go from a PHP Perspective
Barry Jones
 
Go for Rubyists
tchandy
 
Phpconf 2013 - Agile Telephony Applications with PAMI and PAGI
Marcelo Gornstein
 
Taming Cloud APIs with Swift
Tim Burks
 
LCA2014 - Introduction to Go
dreamwidth
 
Asynchronous Architectures for Implementing Scalable Cloud Services - Evan Co...
Twilio Inc
 
Meetup talk: Readying your Go Webservice
Ralph Ligtenberg
 
Using Asterisk in a SIP softswitch
Monica McArthur
 
Ad

More from Digium (20)

PPTX
AstriCon 2017 Recap
Digium
 
PDF
MegaFreight - South Africa’s largest independent freight forwarder
Digium
 
PPTX
Danny Windham, Digium CEO, Keynote address - ITEXPO East 2015, Miamii
Digium
 
PPTX
AstriCon 2014 keynote: Russell Bryant
Digium
 
PDF
Distribution, redundancy and high availability using OpenSIPS
Digium
 
PDF
Getting the best out of WebRTC
Digium
 
PPTX
Automatic Configuration Management for Kamailio and Asterisk in the era of Pu...
Digium
 
PPTX
Making your Asterisk System Secure
Digium
 
PDF
WebRTC: The Big Debate, Shut Up and Build Something
Digium
 
PPT
Connecting Non-SIP IP Camera to Your PBX
Digium
 
PDF
The Past and Future of VoIP
Digium
 
PDF
Developing an ivr payment system with asterisk (astricon 2014 las vegas nevada)
Digium
 
PDF
More than a phone system. A better way to communicate.
Digium
 
PDF
Real Success Stories from IT Heroes
Digium
 
PDF
Smart Deductions for Small Business
Digium
 
PPTX
How to Build Your Brand with UC
Digium
 
PDF
6 Ways a New Phone System can make your Life Easier
Digium
 
PDF
Security Strategies for UC
Digium
 
PDF
Switchvox - The Best Value in Unified Communications
Digium
 
PDF
Five Essential Benefits Driving UC Adoption by SMBs
Digium
 
AstriCon 2017 Recap
Digium
 
MegaFreight - South Africa’s largest independent freight forwarder
Digium
 
Danny Windham, Digium CEO, Keynote address - ITEXPO East 2015, Miamii
Digium
 
AstriCon 2014 keynote: Russell Bryant
Digium
 
Distribution, redundancy and high availability using OpenSIPS
Digium
 
Getting the best out of WebRTC
Digium
 
Automatic Configuration Management for Kamailio and Asterisk in the era of Pu...
Digium
 
Making your Asterisk System Secure
Digium
 
WebRTC: The Big Debate, Shut Up and Build Something
Digium
 
Connecting Non-SIP IP Camera to Your PBX
Digium
 
The Past and Future of VoIP
Digium
 
Developing an ivr payment system with asterisk (astricon 2014 las vegas nevada)
Digium
 
More than a phone system. A better way to communicate.
Digium
 
Real Success Stories from IT Heroes
Digium
 
Smart Deductions for Small Business
Digium
 
How to Build Your Brand with UC
Digium
 
6 Ways a New Phone System can make your Life Easier
Digium
 
Security Strategies for UC
Digium
 
Switchvox - The Best Value in Unified Communications
Digium
 
Five Essential Benefits Driving UC Adoption by SMBs
Digium
 

Recently uploaded (20)

PDF
CIFDAQ Market Wrap for the week of 4th July 2025
CIFDAQ
 
PDF
HCIP-Data Center Facility Deployment V2.0 Training Material (Without Remarks ...
mcastillo49
 
PDF
NewMind AI - Journal 100 Insights After The 100th Issue
NewMind AI
 
PPTX
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 
PDF
CIFDAQ Weekly Market Wrap for 11th July 2025
CIFDAQ
 
PDF
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
PDF
Transcript: New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
PDF
Fl Studio 24.2.2 Build 4597 Crack for Windows Free Download 2025
faizk77g
 
PPTX
Building Search Using OpenSearch: Limitations and Workarounds
Sease
 
PDF
July Patch Tuesday
Ivanti
 
PDF
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
PDF
Presentation - Vibe Coding The Future of Tech
yanuarsinggih1
 
PDF
Smart Trailers 2025 Update with History and Overview
Paul Menig
 
PDF
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
PDF
Mastering Financial Management in Direct Selling
Epixel MLM Software
 
PPTX
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
PDF
IoT-Powered Industrial Transformation – Smart Manufacturing to Connected Heal...
Rejig Digital
 
PDF
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
PDF
DevBcn - Building 10x Organizations Using Modern Productivity Metrics
Justin Reock
 
PDF
How Startups Are Growing Faster with App Developers in Australia.pdf
India App Developer
 
CIFDAQ Market Wrap for the week of 4th July 2025
CIFDAQ
 
HCIP-Data Center Facility Deployment V2.0 Training Material (Without Remarks ...
mcastillo49
 
NewMind AI - Journal 100 Insights After The 100th Issue
NewMind AI
 
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 
CIFDAQ Weekly Market Wrap for 11th July 2025
CIFDAQ
 
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
Transcript: New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
Fl Studio 24.2.2 Build 4597 Crack for Windows Free Download 2025
faizk77g
 
Building Search Using OpenSearch: Limitations and Workarounds
Sease
 
July Patch Tuesday
Ivanti
 
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
Presentation - Vibe Coding The Future of Tech
yanuarsinggih1
 
Smart Trailers 2025 Update with History and Overview
Paul Menig
 
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
Mastering Financial Management in Direct Selling
Epixel MLM Software
 
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
IoT-Powered Industrial Transformation – Smart Manufacturing to Connected Heal...
Rejig Digital
 
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
DevBcn - Building 10x Organizations Using Modern Productivity Metrics
Justin Reock
 
How Startups Are Growing Faster with App Developers in Australia.pdf
India App Developer
 

Scaling FastAGI Applications with Go

  • 1. Scaling FastAGI Applications with Go Lefteris Zafiris NTA Ltd Astricon 2014 Las Vegas
  • 2. Who we are VoIP engineer at NTA ltd Voice and text toolset - Flite Asterisk App - eSpeak Asterisk App - googleTTS - speech recognition - Google speech API - Text translation github.com/zaf
  • 3. Who we are NTA Ltd - nta.co.uk - UK based VoIP provider since 2001 - Hosted PBX services - SIP - FOSS - Kamailio / Asterisk - Perl
  • 4. Perl stack - FastAGI server - Net::Server - fork / prefork - Memory - Capacity - calls / server
  • 5. Enter Go 2007 Google Rob Pike, Ken Thompson, Robert Griesemer - Statically typed - Garbage-collected - Natively compiled - Concurrency - C family - Pragmatic, minimalistic
  • 6. Enter Go Do not communicate by sharing memory. Share memory by communicating. - Goroutines lightweight threads cheap multiplexed in OS threads - Channels communication synchronization
  • 7. Enter Go /* A simple example in go */ package main import "fmt" func main() { var msg string msg = "Hello Astricon" // Retroactively say hello to all Astricons! for i := 0; i <= 10; i++ { fmt.Println(msg, 2004+i) } }
  • 8. Go toolset - go command test fmt build install - Debugging gdb - Package management go get
  • 9. AGI Package - AGI and FastAGI support - Simple familiar interface - BSD Licence - Install: go get github.com/zaf/agi
  • 10. AGI Package - Session structure AGI environment variables Env map[string]string - Reply structure Numeric result of the AGI command Res int Additional returned data Dat string
  • 11. AGI Package - AGI commands as methods of the Session struct func (a *Session) Answer() (Reply, error) func (a *Session) SendText(text string) (Reply, error) func (a *Session) Hangup(channel ...string) (Reply, error) func (a *Session) SayDigits(digit int, escape string) (Reply, error)
  • 12. AGI Package usage - Create a new session: myAgi := agi.New() - Initialize, read and parse AGI environment: myAgi.Init(nil) - Execute AGI commands: myAgi.StreamFile("hello-world", "#")
  • 13. AGI example package main import ( "log" "github.com/zaf/agi" ) func main() { // Create a new AGI session and Parse the AGI environment. myAgi := agi.New() err := myAgi.Init(nil) if err != nil { log.Fatalf("Error Parsing AGI environment: %vn", err) } // Print a message on the asterisk console using Verbose. _, err := myAgi.Verbose("Hello World") if err != nil { log.Fatalf("AGI reply error: %vn", err) } }
  • 14. FastAGI Example func main() { // Create a tcp listener on port 4573 and start a new goroutine for each connection. ln, err := net.Listen("tcp", ":4573") if err != nil { log.Fatal(err) } defer ln.Close() for { conn, err := ln.Accept() if err != nil { log.Println(err) continue } go connHandle(conn) } }
  • 15. FastAGI Example func connHandle(c net.Conn) { defer c.Close() myAgi := agi.New() // Create I/O buffers rw := bufio.NewReadWriter(bufio.NewReader(c), bufio.NewWriter(c)) err := myAgi.Init(rw) if err != nil { log.Println(err) return } rep, err := myAgi.StreamFile("hello", "1234567890#*") if err != nil { log.Println(err) return } // Check AGI command return value if rep.Res == -1 { log.Println("Failed to playback file") } }
  • 16. Error Handling func connHandle(c net.Conn) { defer func() { c.Close() if err := recover(); err != nil { log.Println("Session terminated:", err) } }() myAgi := agi.New() rw := bufio.NewReadWriter(bufio.NewReader(c), bufio.NewWriter(c)) err := myAgi.Init(rw) checkErr(err) _, err := myAgi.Verbose("Hello World") checkErr(err) } //Check for AGI Protocol errors or hangups func checkErr(e error) { if e != nil { panic(e) } }
  • 17. AGI Debug - Error testing - Performance testing - Security testing Eliminate the use of Asterisk Custom AGI Payloads
  • 18. Agistress A client that can connect to servers using the Asterisk Gateway Interface - user defined payloads - user controlled session parameters - fast - parallel session spawning - performance measuring - written in Go go get github.com/zaf/agistress
  • 19. Agistress Run once and display full debug output of the AGI session: agistress -host 127.0.0.1 -single Stress test the AGI server by spawning 10 sessions 10 times per second and display performance details: agistress -host 127.0.0.1 -sess 10 -runs 10 Run once using custom AGI payload from config file: agistress -host 127.0.0.1 -single -conf payload.conf
  • 20. Agistress Config file: - JSON format - Environment variables - AGI Command replies - Reply delay
  • 21. Agistress Config file sample: { "AgiPayload": [ {"Msg": "200 result=0", "Delay": 10}, {"Msg": "200 result=0", "Delay": 10}, {"Msg": "200 result=1 endpos=1234", "Delay": 3000}, {"Msg": "HANGUP"} ] }
  • 22. Agistress Sample output: Running FastAGI bench against: 192.168.1.10:4573 Press Enter to stop. A new run each: 100ms Sessions per run: 10 Reply delay: 50ms FastAGI Sessions Active: 20 Completed: 520 Duration: 206121479 ns (last 10000 sessions average) Failed: 0
  • 23. Measuring Comparison between FastAGI implementations: - Perl: Asterisk::AGI Net::Server::PreFork - Go: agi package net package
  • 24. Measuring Simple sound file Playback application: - Start session and parse AGI Env - Parse FastAGI request URI agi://10.11.12.13/myagi?file=echo-test - Check channel status - Answer channel - Playback given file Perl: 73 loc (http://goo.gl/hdsxuw) Go: 90 loc (http://goo.gl/uxezg7)
  • 25. Benchmark Stress test using agistress: - 1 session/sec agistress -delay=0 - 25 sessions/sec agistress -runs=5 -sess=5 -delay=0 - 50 sessions/sec agistress -runs=10 -sess=5 -delay=0 - 100 sessions/sec agistress -runs=10 -sess=10 -delay=0 - 200 and 400 sessions/sec
  • 27. Benchmark Memory usage: Many active sessions Custom AGI Payload, 5 sec playback duration - 12 active sessions: agistress -sess=2 -conf=sample.conf - 24 active sessions: agistress -sess=4 -conf=sample.conf - 48, 96 and 192 sessions
  • 29. Results - Up to 3 times less CPU usage - Up to 2.5 times shorter runtime - Enormous memory savings