Beispiel #1
0
func BenchmarkJSONFormatter(b *testing.B) {

	test := newTestJSONFormatter([]interface{}{
		`findKey    :":  key        ::`,
		`findKey    :}:             : pop  : end`,
		`key        :":  findVal    :      : key`,
		`findVal    :\:: value      ::`,
		`value      :":  string     ::`,
		`value      :[:  array      : push : arr`,
		`value      :{:  findKey    : push : obj`,
		`value      :,:  findKey    :      : val`,
		`value      :}:             : pop  : val+end`,
		`string     :":  findKey    :      : esc`,
		`array      :[:  array      : push : arr`,
		`array      :{:  findKey    : push : obj`,
		`array      :]:             : pop  : end`,
		`array      :,:  arrIntVal  :      : val`,
		`array      :":  arrStrVal  ::`,
		`arrIntVal  :,:  arrIntVal  :      : val`,
		`arrIntVal  :]:             : pop  : val+end`,
		`arrStrVal  :":  arrNextStr :      : esc`,
		`arrNextStr :":  arrStrVal  ::`,
		`arrNextStr :]:             : pop  : end`,
	}, "findKey")

	for i := 0; i < b.N; i++ {
		testString := fmt.Sprintf(`{"a":%d23,"b":"string","c":[%d,2,3],"d":[{"a":%d}],"e":[[%d,2]],"f":[{"a":%d},{"b":2}],"g":[[%d,2],[3,4]]}`, i, i, i, i, i, i)
		msg := core.NewMessage(nil, []byte(testString), 0)
		test.Format(msg)
	}
}
Beispiel #2
0
func testFormatter(formatter core.Formatter) bool {
	message := []byte("\ttest\r\n123 456\n")
	msg := core.NewMessage(nil, message, 0)

	formatter.Format(msg)
	return true
}
Beispiel #3
0
func TestJSONFormatter1(t *testing.T) {
	expect := shared.NewExpect(t)

	testString := `{"a":123,"b":"string","c":[1,2,3],"d":[{"a":1}],"e":[[1,2]],"f":[{"a":1},{"b":2}],"g":[[1,2],[3,4]]}`
	msg := core.NewMessage(nil, []byte(testString), 0)
	test := newTestJSONFormatter([]interface{}{
		`findKey    :":  key        ::`,
		`findKey    :}:             : pop  : end`,
		`key        :":  findVal    :      : key`,
		`findVal    :\:: value      ::`,
		`value      :":  string     ::`,
		`value      :[:  array      : push : arr`,
		`value      :{:  findKey    : push : obj`,
		`value      :,:  findKey    :      : val`,
		`value      :}:             : pop  : val+end`,
		`string     :":  findKey    :      : esc`,
		`array      :[:  array      : push : arr`,
		`array      :{:  findKey    : push : obj`,
		`array      :]:             : pop  : end`,
		`array      :,:  arrIntVal  :      : val`,
		`array      :":  arrStrVal  ::`,
		`arrIntVal  :,:  arrIntVal  :      : val`,
		`arrIntVal  :]:             : pop  : val+end`,
		`arrStrVal  :":  arrNextStr :      : esc`,
		`arrNextStr :":  arrStrVal  ::`,
		`arrNextStr :]:             : pop  : end`,
	}, "findKey")

	result, _ := test.Format(msg)
	expect.Equal(testString, string(result))
}
Beispiel #4
0
func (prod *Proxy) sendMessage(msg core.Message) {
	// If we have not yet connected or the connection dropped: connect.
	for prod.connection == nil {
		conn, err := net.DialTimeout(prod.protocol, prod.address, prod.timeout)

		if err != nil {
			Log.Error.Print("Proxy connection error - ", err)
			<-time.After(time.Second)
		} else {
			conn.(bufferedConn).SetWriteBuffer(prod.bufferSizeKB << 10)
			prod.connection = conn
		}
	}

	// Check if and how to work with the message source
	responder, processResponse := msg.Source.(core.AsyncMessageSource)
	if processResponse {
		if serialResponder, isSerial := msg.Source.(core.SerialMessageSource); isSerial {
			defer serialResponder.ResponseDone()
		}
	}

	// Write data
	prod.connection.SetWriteDeadline(time.Now().Add(prod.timeout))
	if _, err := prod.connection.Write(msg.Data); err != nil {
		Log.Error.Print("Proxy write error: ", err)
		prod.connection.Close()
		prod.connection = nil
		return // ### return, connection closed ###
	}

	// Prepare responder function
	enqueueResponse := shared.BufferReadCallback(nil)
	if processResponse {
		enqueueResponse = func(data []byte, seq uint64) {
			response := core.NewMessage(prod, data, seq)
			responder.EnqueueResponse(response)
		}
	}

	// Read response
	prod.connection.SetReadDeadline(time.Now().Add(prod.timeout))
	if err := prod.reader.ReadAll(prod.connection, enqueueResponse); err != nil {
		Log.Error.Print("Proxy read error: ", err)
		prod.connection.Close()
		prod.connection = nil
		return // ### return, connection closed ###
	}
}
Beispiel #5
0
func (client *proxyClient) sendMessage(data []byte, seq uint64) {
	msg := core.NewMessage(client, data, seq)
	client.proxy.EnqueueMessage(msg)
}