This example shows you how to test your RESTful API or Java® client for deployment against MATLAB®
Production Server™ using the testing interface in the Production Server Compiler
app. For testing purposes, you will create and use MATLAB function called addmatrix
that accepts two numeric
matrices as inputs and returns their sum as an output.
The testing interface can be accessed by clicking the Test Client button in the Production Server Compiler app. The Production Server Compiler app is part of MATLAB Compiler SDK™.
Write a MATLAB function called addmatrix
that accepts two
numeric matrices as inputs and returns their sum as an output. Save this
file as addmatrix.m
.
Test the function at the MATLAB command prompt.
a = [10 20 30; 40 50 60]; b = [100 200 300; 400 500 600]; c = addmatrix(a,b)
c = 110 220 330 440 550 660
Open the Production Server Compiler app by typing the following at the MATLAB command prompt:
productionServerCompiler
In the Type section of the toolstrip, select Deployable Archive (.ctf) from the list.
Specify the MATLAB functions to deploy.
In the Exported Functions section of the toolstrip, click the plus button.
Using the file explorer, locate and select the
addmatrix.m
file.
In the section titled Include MATLAB function signature file, click the Create File button. This will create an editable JSON file that contains the function signatures of the functions included in the archive. By editing this file you can specify argument types and/or sizes of inputs and outputs, and also provide help information for each of the inputs. For more information, see MATLAB Function Signatures in JSON (MATLAB Production Server).
If you have an existing JSON file with function signatures, click the Add Existing File button to add that file instead of the Create File button.
By including this information in your archive, you can use the discovery service functionality on the server.
Note
Only the MATLAB Production Server RESTful API supports the discovery service. For more information, see RESTful API (MATLAB Production Server).
Click the Test Client button. The app will switch to the TEST tab.
Check the value of the Port field.
It must be:
an available port
the same port number the client is using
For this example, the client will use port 9910.
Check the box to Enable CORS. This option needs to be enabled if you are using a client that uses JavaScript®. By enabling CORS the server will accept requests from different domains.
Check the box to Enable Discovery. This option needs to be enabled to use the discovery service. The discovery service returns information about deployed MATLAB functions as a JSON object.
Click Start.
This example uses the MATLAB HTTP Interface to invoke the RESTful API and make requests to the testing interface. You can use other tools such cURL or JavaScript XHR.
Import the MATLAB HTTP Interface packages, setup the request, and send the request to the testing interface.
% Import MATLAB HTTP Interface packages import matlab.net.* import matlab.net.http.* import matlab.net.http.fields.* % Setup request requestUri = URI('http://localhost:9910/api/discovery'); options = matlab.net.http.HTTPOptions('ConnectTimeout',20,... 'ConvertResponse',false); request = RequestMessage; request.Header = HeaderField('Content-Type','application/json'); request.Method = 'GET'; % Send request response = request.send(requestUri, options);
View the response body.
response.Body.Data
ans = "{"discoverySchemaVersion":"1.0.0","archives":{"matfun":{"archiveSchemaVersion":"1.1.0",...
ans
.To test using JavaScript XHR you can use the following code:
Start a separate session of the MATLAB desktop. This is because you cannot send a POST request from the same MATLAB session that is running the testing interface.
Import the MATLAB HTTP Interface packages, setup the request, and send the request to the testing interface.
% Import HTTP interface packages import matlab.net.* import matlab.net.http.* import matlab.net.http.fields.* % Setup message body body = MessageBody; a = [10 20 30; 40 50 60]; b = [100 200 300;400 500 600]; payload = mps.json.encoderequest({a,b}); body.Payload = payload; % Setup request requestUri = URI('http://localhost:9910/matfun/addmatrix'); options = matlab.net.http.HTTPOptions('ConnectTimeout',20,... 'ConvertResponse',false); request = RequestMessage; request.Header = HeaderField('Content-Type','application/json'); request.Method = 'POST'; request.Body = body; % Send request response = request.send(requestUri, options)
View the response body.
response.Body.Data
ans = "{"lhs":[[[110,220,330],[440,550,660]]]}"
To test using JavaScript XHR you can use the following code:
Examine Data
Switch to the Production Server Compiler app.
In the testing interface, under MATLAB Execution Requests, click the completed message in the app to see the values exchanged between the client and MATLAB.
Click Input to view the arrays passed into MATLAB.
Click Output to view the array returned to the client.
Set Breakpoints
In the testing interface of the Production Server Compiler, click Breakpoints > Break on MATLAB function entry.
In the separate MATLAB session, resend a POST request to the server.
When the MATLAB editor opens, note that a breakpoint is set at the first line in the function and that processing has paused at the breakpoint.
You now can use all of the MATLAB debugging tools to step through your function.
Note
You can create a timeout error in the client if you take a long time stepping through the MATLAB function.
Note that variables a1
and
a2
are displayed in the MATLAB workspace.
In the MATLAB editor, click Continue to complete the debug process.
The Server Requests section of the app shows that the request completed successfully.
Click Stop to shutdown the test server.
Click Close Test.
Create a Java file MPSClientExample.java
with following
client code:
At the system command prompt, compile the Java client code using the javac
command.
javac -classpath "matlabroot
\toolbox\compiler_sdk\mps_clients\java\mps_client.jar" MPSClientExample.java
At the system command prompt, run the Java client.
java -classpath .;"matlabroot\toolbox\compiler_sdk\mps_clients\java\mps_client.jar" MPSClientExample
Note
You cannot run the Java client from the MATLAB command prompt.
The application returns the following at the console:
110.0 220.0 330.0 440.0 550.0 660.0
You can debug the data exchanged between the client and MATLAB using the same steps listed under Test Using RESTful API.