Guest User

Untitled

a guest
Sep 11th, 2019
108
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package main
  2.  
  3. import (
  4. "bytes"
  5. "flag"
  6. "fmt"
  7. "io"
  8. "io/ioutil"
  9. "log"
  10. "net/smtp"
  11. "os"
  12. "os/signal"
  13. "sync/atomic"
  14. "syscall"
  15. "time"
  16. )
  17.  
  18. func main() {
  19. addr := flag.String("addr", "127.0.0.1:1125", "Address of server to stress-test")
  20. msgFile := flag.String("msg", "msg.eml", "Message sample to use")
  21. conns := flag.Int("conns", 100, "Number of concurrent connections to open")
  22. limit := flag.Int("limit", 0, "Stop after sending N messages, default is no limit")
  23. flag.Parse()
  24.  
  25. f, err := ioutil.ReadFile(*msgFile)
  26. if err != nil {
  27. log.Fatalln(err)
  28. }
  29.  
  30. ch := make(chan os.Signal)
  31. signal.Notify(ch, syscall.SIGINT)
  32. stop := false
  33.  
  34. N := uint64(0)
  35. start := time.Now()
  36. for i := 0; i < *conns; i++ {
  37. go func() {
  38. cl, err := smtp.Dial(*addr)
  39. if err != nil {
  40. log.Fatalln(err)
  41. }
  42. defer cl.Close()
  43. if err := cl.Hello("localhost"); err != nil {
  44. log.Fatalln(err)
  45. }
  46.  
  47. for !stop || (atomic.LoadUint64(&N) > uint64(*limit) && *limit != 0) {
  48. if err := cl.Mail("foxcpp@foxcpp.dev"); err != nil {
  49. log.Fatalln(err)
  50. }
  51. if err := cl.Rcpt("foxcpp@foxcpp.dev"); err != nil {
  52. log.Fatalln(err)
  53. }
  54. wr, err := cl.Data()
  55. if err != nil {
  56. log.Fatalln(err)
  57. }
  58. if _, err := io.Copy(wr, bytes.NewReader(f)); err != nil {
  59. log.Fatalln(err)
  60. }
  61. wr.Close()
  62. atomic.AddUint64(&N, 1)
  63. }
  64. }()
  65. }
  66.  
  67. <-ch
  68. stop = true
  69.  
  70. end := time.Now()
  71.  
  72. fmt.Println("time elapsed:", end.Sub(start))
  73. fmt.Println("messages sent:", N)
  74. }
RAW Paste Data