Documentation

Test Client Data Integration Against MATLAB

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

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.

     addmatrix.m

  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

  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 (MATLAB Production Server).

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

    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 HTTP Interface (MATLAB) to invoke the RESTful API and make requests to the testing interface. You can use other tools such cURL or JavaScript XHR.

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. This is because you cannot send a POST request from the same MATLAB session that is running the testing interface.

  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.

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

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