Main Content

Test Client Data Integration Against MATLAB

This example shows how to test your RESTful API or Java® client for deployment to MATLAB® Production Server™ using the development version of MATLAB Production Server. MATLAB Compiler SDK™ includes the development version of MATLAB Production Server for testing and debugging application code and Excel® add-ins before deploying them to web applications and enterprise systems.

For testing purposes, you will create and use a MATLAB function called addmatrix that accepts two numeric matrices as inputs and returns their sum as an output. You access the local test server by clicking the Test Client button in the Production Server Compiler app.

Create a MATLAB Function

  1. 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.

    function a = addmatrix(a1, a2)
    a = a1 + a2;
  2. 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

Prepare for Testing

  1. Open the Production Server Compiler app by typing the following at the MATLAB command prompt:

    productionServerCompiler

    Production Server Compiler with Deployable Archive (.ctf) type selected and addmatrix.m in the exported functions section

  2. In the Type section of the toolstrip, select Deployable Archive (.ctf) from the list.

  3. Specify the MATLAB functions to deploy.

    1. In the Exported Functions section of the toolstrip, click the plus button.

    2. Using the file explorer, locate and select the addmatrix.m file.

  4. 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 for MATLAB Function Execution (MATLAB Production Server).

  5. Click the Test Client button. The app will switch to the TEST tab.

    Production Server Compiler with port set to 9910. The server address is http://localhost:9910/matfun.

    1. 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.

    2. 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.

    3. 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.

  6. Click Start.

Test Using RESTful API

This example uses the MATLAB Call Web Services from MATLAB Using HTTP to invoke the RESTful API and make requests to the testing interface. You can use other tools such cURL or JavaScript XHR.

The testing interface does not support asynchronous client requests. The interface processes a POST Asynchronous Request (MATLAB Production Server) like a POST Synchronous Request (MATLAB Production Server). Other asynchronous requests from the RESTful API are not supported.

Test Discovery Service

  1. 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);
    

  2. View the response body.

    response.Body.Data
    ans = 
    
        "{"discoverySchemaVersion":"1.0.0","archives":{"matfun":{"archiveSchemaVersion":"1.1.0",...
    The response body has been snipped to fit the page. A formatted version of the response body can be found by expanding ans.

     ans

    To test using JavaScript XHR you can use the following code:

     JavaScript XHR Code for Testing Discovery Service

Testing Data Exchange

  1. Start a separate session of the MATLAB desktop.

    Note

    You must use a separate MATLAB session to make POST requests. If you make POST requests from the same MATLAB session that is running the testing interface, MATLAB does not respond.

  2. 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)
  3. 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:

     JavaScript XHR Code for Testing Data Exchange

Examine Data

  1. Switch to the Production Server Compiler app.

    MATLAB Execution Requests section where the status of function a=addmatrix(a1,a2) is marked as complete. The inputs a1 and a2 are both 2-by-3 double arrays and the output a is a 2-by-3 double array.

  2. 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.

  3. Click Input to view the arrays passed into MATLAB.

  4. Click Output to view the array returned to the client.

Set Breakpoints

  1. In the testing interface of the Production Server Compiler, click Breakpoints > Break on MATLAB function entry.

  2. In the separate MATLAB session, resend a POST request to the local test server.

  3. 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.

    Function addmatrix.m with a breakpoint on the line a = a1 + a2. The value of a1 is [10 20 30; 40 50 60], and the value of a2 is [100 200 300;400 500 600].

    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.

  4. Note that variables a1 and a2 are displayed in the MATLAB workspace.

  5. In the MATLAB editor, click Continue to complete the debug process.

    The Server Requests section of the app shows that the request completed successfully.

  6. Click Stop to shutdown the local test server.

  7. Click Close Test.

Testing Using Java Client Application

  1. Create a Java file MPSClientExample.java with following client code:

     MPSClientExample.java

  2. 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
    
  3. 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.

Related Topics