I'm sure that my server can work with better performance, but it's just a dream.
Parsing a very long strings is not so obvious task for me. I have a server that handles a lot of clients requests and finds a substring of a string.
Server
package api
import (
"net/http"
"stackexchange/api/controllers"
)
func start() {
mux := http.NewServeMux()
mux.HandleFunc("/messages", Messages)
}
func Messages(res http.ResponseWriter, req *http.Request) {
mc := controllers.NewMessageController()
switch req.Method {
case "POST":
mc.ActionPost(res, req)
}
}
Controller
package controllers
import (
"net/http"
"regexp"
)
type MessageController struct {
}
func NewMessageController() *MessageController {
return &MessageController{}
}
func (mc *MessageController) ActionPost(res http.ResponseWriter, req *http.Request) {
msgBody := req.URL.Query().Get("body")
msgPattern := req.URL.Query().Get("pattern")
isPatternFind := regexp.MatchString(msgPattern, msgBody)
if isPatternFind {
res.Write([]byte("find"))
} else {
res.Write([]byte("not find"))
}
}
Test
package tests
import (
"fmt"
"log"
"net/http"
"net/http/httptest"
"stackexchange/api"
"testing"
)
var longString = `Knowing that millions of people around the world would be
watching in person and on television and expecting great
things from him — at least one more gold medal for America,
if not another world record — during this, his fourth and
surely his last appearance in the World Olympics, and realizing
that his legs could no longer carry him down the runway with
the same blazing speed and confidence in making a huge,
eye-popping leap that they were capable of a few years ago
when he set world records in the 100-meter dash and in the
400-meter relay and won a silver medal in the long jump,
the renowned sprinter and track-and-field personality
Carl Lewis, who had known pressure from fans and media before
but never, even as a professional runner, this kind of pressure,
made only a few appearances in races during the few months before
the Summer Olympics in Atlanta, Georgia, partly because he was
afraid of raising expectations even higher and he did not want
to be distracted by interviews and adoring fans who would follow
him into stores and restaurants demanding autographs and
photo-opportunities, but mostly because he wanted to conserve
his energies and concentrate, like a martial arts expert, on
the job at hand: winning his favorite competition, the long jump,
and bringing home another Gold Medal for the United States,
the most fitting conclusion to his brilliant career in track and field.`
var subString = `home`
func TestPostMessage(t *testing.T) {
url := "http://localhost:8080/messages?"
url = fmt.Sprintf("%sbody=%s", url, longString)
url = fmt.Sprintf("%s&pattern=%s", url, subString)
req, err := http.NewRequest("POST", url, nil)
if err != nil {
log.Fatal(err)
}
res := httptest.NewRecorder()
api.Messages(res, req)
expected := "find"
actual := res.Body.String()
if actual != expected {
log.Fatalf("\nMethod: POST\nActual: %s \nExpected: %s \n", actual, expected)
}
}
1 Answer 1
The call to regexp.MatchString
is the only really expensive part of your code. Do you need full regexp support? If you just want to find substrings, you should use strings.Contains
instead of the regexp package. Otherwise, there's no way around it. It might be worth looking at what each request looks like: you could call strings.Contains
for the simple ones (that don't use any regexp features) and only use regexp.MatchString
when needed.
Some comments on style:
- Your
NewMessageController
method is useless. Just have your callers usenew(controllers.MessageController)
. - Use
t.Fatalf
instead oflog.Fatalf
in your tests. Build strings in a consistent manner:
var ( longString = "your long string" subString = "home" host = "http://localhost:8080" ) func TestPostMessage(t *testing.T) { url := fmt.Sprintf("%s/messages?body=%s&pattern=%s", host, longString, subString) ... }
Explore related questions
See similar questions with these tags.