cancel
Showing results for 
Search instead for 
Did you mean: 

How to Profiling R Procedures with Rprof

Former Member
0 Kudos

Hello experts,

I want to profile R procedures with "Rprof". According to the example in SAP HANA R Integration Guide, I executed the following code:

DROP TABLE "Rprof";

CREATE COLUMN TABLE "Rprof"("function" VARCHAR(5000), "total.time" DOUBLE, "total.pct" DOUBLE, "self.time" DOUBLE, "self.pct" DOUBLE);

DROP TYPE DUMMY_2;

CREATE TYPE DUMMY_2 AS TABLE(

VAL DOUBLE

);

DROP PROCEDURE USE_RPROF;

CREATE PROCEDURE USE_RPROF(OUT result "Rprof", OUT result2 DUMMY_2)

LANGUAGE RLANG AS

BEGIN

tmp <- tempfile()

Rprof(tmp)

addition <- function(a,b,c){

d <- a * b + c - a;

};

result2 <- addition(5,7,5);

Rprof(NULL)

profile <- as.data.frame(summaryRprof(tmp)$by.total)

unlink(tmp)

result <- cbind("function"=rownames(profile), profile)

END;

CALL USE_RPROF(?,?);

SELECT * FROM "Rprof";

Then I receive the Error:

Could not execute 'CALL USE_RPROF(?,?)' in 408 ms 404 µs .

SAP DBTech JDBC: [2048]: column store error: search table error:  [34084] Receive error: get result error.;The response message type mismatch

If I delete the function "addition()" in these code, instead of the simple code:

BEGIN

tmp <- tempfile()

Rprof(tmp)

a <- 5;

b <- 6;

a+b;

Rprof(NULL)

profile <- as.data.frame(summaryRprof(tmp)$by.total)

unlink(tmp)

result <- cbind("function"=rownames(profile), profile)

END;

CALL USE_RPROF(?);

SELECT * FROM "Rprof";

Then it could be successfully executed, but with no any record caught in the consequent table. I wonder where is the problem?

Accepted Solutions (1)

Accepted Solutions (1)

lbreddemann
Active Contributor
0 Kudos

Please check the result of SummaryRprof in your R environment.As Rprof is a sample based profiler, it might be that the very simple code you used for the example did not actually make it into any samples.

The R documentation contains the details on R and RProf. Please check these for details.

As for the type mismatch: result2 in your example is a numeric scalar, while the output type DUMMY_2 is a table type.

With cbind() you can turn your result2 into a table typed value.

Former Member
0 Kudos

I corrected the codes as below and it can work in HANA, but still with no record in Rprof.

BEGIN

tmp <- tempfile()

Rprof(tmp)

addition <- function(a,b,c){

d <- a * b + c - a;

return(d);

};

e <- addition(5,7,5);

result2 <- data.frame(VAL = c(e));

Rprof(NULL)

profile <- as.data.frame(summaryRprof(tmp)$by.total)

unlink(tmp)

result <- cbind("function"=rownames(profile), profile)

In the R environment I test step by step until "profile <- as.data.frame(summaryRprof(tmp)$by.total)" and there are records in profile.

but if in HANA using the code, still nothing.

lbreddemann
Active Contributor
0 Kudos

The data in your R screenshot shows that all subfunctions take the full amount of time of the single sample (default sample interval is 0.02 as documented in R).

When I tested this locally on my R Studio, I was only able to get values into the summaryRprof once the profiled code actually ran longer than 1 sample.

Try and dig into that a bit more. The data transfer from R via RServe to SAP HANA works. So, there's just no data in the data frame.

This has nothing to do with SAP HANA but all with R and how Rprof works.

Former Member
0 Kudos

Sorry, I still didn't quite clear about the problem. Is the reason why no records in summaryRprof that no data in HANA, all the data just worked between R and Rserve?

The actual situation is that I have created a table in HANA, which stored thousands rows of data. I used some more complexly function to calculate them, but still nothing be recorded in summaryRprof.

So what means you that " there's just no data in the data frame"? How could I make the Rprof work?

Many Thanks !

lbreddemann
Active Contributor
0 Kudos

Nope, doesn't seem to be a HANA problem at all.

Try and see if you can get reasonable profiling information for your R code in R Studio. By 'reasonable' I mean something more than just a single sample).

Why don't you use the example from the RProf docu?

Rprof(tmp <- tempfile())

example(glm)

Rprof()

summaryRprof(tmp)

unlink(tmp)


The GLM demo does enough work to produce some nice profiling output:


> summaryRprof(tmp)

$by.self

                self.time self.pct total.time total.pct

"print.default"      0.06      100       0.06       100

$by.total

                    total.time total.pct self.time self.pct

"print.default"           0.06    100.00      0.06      100

"example"                 0.06    100.00      0.00        0

"print"                   0.06    100.00      0.00        0

"source"                  0.06    100.00      0.00        0

"print.summary.glm"       0.04     66.67      0.00        0

"eval"                    0.02     33.33      0.00        0

"print.data.frame"        0.02     33.33      0.00        0

"printCoefmat"            0.02     33.33      0.00        0

"withVisible"             0.02     33.33      0.00        0

$sample.interval

[1] 0.02

$sampling.time

[1] 0.06

lbreddemann
Active Contributor
0 Kudos

Just tried this on my test system again and it works well:

DROP PROCEDURE USE_RPROF;

CREATE PROCEDURE USE_RPROF(OUT result "Rprof")

LANGUAGE RLANG AS

BEGIN

   tmp <- tempfile()

   Rprof(tmp)

  

   example(glm)

   

   Rprof(NULL)

   profile <- as.data.frame(summaryRprof(tmp)$by.total)

   unlink(tmp)

   result <- cbind("function"=rownames(profile), profile)

END;

CALL USE_RPROF(?);

function              total.timetotal.pctself.timeself.pct
"example"              0.06      100.0    0.0      0.0    
"lazyLoadDBfetch"      0.04      66.67    0.04    66.67  
"source"              0.04      66.67    0.0      0.0    
"match"                0.02      33.33    0.02    33.33  
".Rd_format_title"    0.02      33.33    0.0      0.0    
"anova.glm"            0.02      33.33    0.0      0.0    
"anova"                0.02      33.33    0.0      0.0    
"dimnames<-.data.frame"0.02      33.33    0.0      0.0    
"dimnames<-"          0.02      33.33    0.0      0.0    
"eval"                0.02      33.33    0.0      0.0    
"format.default"      0.02      33.33    0.0      0.0    
"format"              0.02      33.33    0.0      0.0    
"gsub"                0.02      33.33    0.0      0.0    
"print.summary.glm"    0.02      33.33    0.0      0.0    
"print"                0.02      33.33    0.0      0.0    
"printCoefmat"        0.02      33.33    0.0      0.0    
"tools::Rd2ex"        0.02      33.33    0.0      0.0    
"withVisible"          0.02      33.33    0.0      0.0    

Be aware that this information does not automatically gets stored in the table. "Rprof. This table is merely used as a data structure definition here.

Former Member
0 Kudos

Thank you very much!

I try the example and now it worked as well.

Answers (0)