|
| 1 | +library(iotools) |
1 | 2 |
|
2 | | -cat(" - Generating large file ...") |
3 | | -fn <- tempfile("test", fileext=".csv") |
| 3 | +counts <- c(0L, 0L) |
4 | 4 |
|
5 | | -on_exit(function() unlink(fn)) |
| 5 | +group <- function(grp) { |
| 6 | + if (any(counts > 0)) { |
| 7 | + cat("\n Summary:", counts[2], "OK,", counts[1],"FAILED\n") |
| 8 | + if (counts[1] > 0) stop("*** At least one test failed") |
| 9 | + } |
| 10 | + counts <<- c(0L, 0L) |
| 11 | + cat("\n===", grp, "===\n\n") |
| 12 | +} |
| 13 | + |
| 14 | +.timing <- numeric() |
| 15 | + |
| 16 | +test <- function(txt, val, res) { |
| 17 | + cat(txt, "...") |
| 18 | + ok <- if (missing(res)) isTRUE(val) else isTRUE(all.equal(val, res)) |
| 19 | + tim <- if (length(.timing)) paste0(" (",paste(as.integer(.timing * 1000),collapse='/'),")") else "" |
| 20 | + ## \x1b[1m and \x1b[m for ANSI if we care... |
| 21 | + cat(if (ok) " OK" else " FAILED", tim, "\n", sep='') |
| 22 | + if (!ok) { |
| 23 | + if (missing(res)) { |
| 24 | + cat("*** expected TRUE, got:") |
| 25 | + str(val) |
| 26 | + cat("*** call: ", deparse(substitute(val)), "\n\n") |
| 27 | + } else { |
| 28 | + cat("*** expected: ") |
| 29 | + str(val) |
| 30 | + cat("*** got: ") |
| 31 | + str(res) |
| 32 | + cat("***all.equal: ") |
| 33 | + print(all.equal(val, res)) |
| 34 | + cat("*** call: ", deparse(substitute(res)), "\n\n") |
| 35 | + } |
| 36 | + } |
| 37 | + #if (length(.timing)) print(.timing) |
| 38 | + .timing <<- numeric() |
| 39 | + counts[ok + 1] <<- counts[ok + 1] + 1L |
| 40 | +} |
| 41 | + |
| 42 | +set.seed(123) |
| 43 | + |
| 44 | +cat(" - Generating large file ... ") |
| 45 | +# fn <- tempfile("test", fileext=".csv") |
| 46 | +if (file.exists("test.csv")) unlink("test.csv") |
| 47 | +fn <- "test.csv" |
| 48 | + |
| 49 | +.max.ch <- 4e6 ## want more than one key |
6 | 50 |
|
7 | | -for (i in LETTERS) { |
8 | | - n = abs(rnorm(1) + 10000) + 1L |
| 51 | +res <- sapply(LETTERS, function(i) { |
| 52 | + n = abs(rnorm(1,100) + 60000) + 1L |
9 | 53 | d=data.frame(A=i, B=rnorm(n), C=runif(n)) |
10 | 54 | write.table(d, fn, TRUE, FALSE, "\t", row.names=FALSE, col.names=FALSE) |
11 | | -} |
| 55 | + c(B=sum(d$B), C=sum(d$C)) |
| 56 | +}) |
| 57 | + |
| 58 | +cat(sprintf("%.1fMb\n", file.info(fn)$size / (1024^2))) |
| 59 | + |
| 60 | +test("chunk.apply", as.integer(file.info(fn)$size), { |
| 61 | + f <- file(fn, "rb") |
| 62 | + cr <- chunk.reader(f) |
| 63 | + .timing <<- system.time( |
| 64 | + s <- chunk.apply(cr, length, CH.MERGE=sum, CH.MAX.SIZE=.max.ch) |
| 65 | + ) |
| 66 | + rm(cr) |
| 67 | + close(f) |
| 68 | + gc() |
| 69 | + s |
| 70 | +}) |
| 71 | + |
| 72 | +test("parallel chunk.apply", as.integer(file.info(fn)$size), { |
| 73 | + f <- file(fn, "rb") |
| 74 | + cr <- chunk.reader(f) |
| 75 | + .timing <<- system.time( |
| 76 | + s <- chunk.apply(cr, length, CH.MERGE=sum, CH.MAX.SIZE=.max.ch, CH.PARALLEL=4) |
| 77 | + ) |
| 78 | + rm(cr) |
| 79 | + close(f) |
| 80 | + gc() |
| 81 | + s |
| 82 | +}) |
| 83 | + |
| 84 | +test("chunk.apply with keys + ctapply", res[1,], { |
| 85 | + f <- file(fn, "rb") |
| 86 | + cr <- chunk.reader(f, sep="\t") |
| 87 | + .timing <<- system.time( |
| 88 | + s <- chunk.apply(cr, function(r) { |
| 89 | + d <- dstrsplit(r, list(A="",B=1,C=1), "\t") |
| 90 | + ctapply(d$B, d$A, sum) |
| 91 | + }, CH.MERGE=c, CH.MAX.SIZE=.max.ch) |
| 92 | + ) |
| 93 | + rm(cr) |
| 94 | + close(f) |
| 95 | + gc() |
| 96 | + s |
| 97 | +}) |
| 98 | + |
| 99 | +test("chunk.apply with keys + matrix ctapply", res, { |
| 100 | + f <- file(fn, "rb") |
| 101 | + cr <- chunk.reader(f, sep="\t") |
| 102 | + .timing <<- system.time( |
| 103 | + s <- chunk.apply(cr, function(r) { |
| 104 | + d <- dstrsplit(r, list(A="",B=1,C=1), "\t") |
| 105 | + m <- ctapply(as.matrix(d[,c("B","C")]), d$A, colSums, MERGE=rbind) |
| 106 | + }, CH.MERGE=rbind, CH.MAX.SIZE=.max.ch) |
| 107 | + ) |
| 108 | + rm(cr) |
| 109 | + close(f) |
| 110 | + gc() |
| 111 | + colnames(s) <- c("B","C") |
| 112 | + t(s) |
| 113 | +}) |
12 | 114 |
|
13 | | -f <- file(fn, "rb") |
14 | | -cr <- chunk.reader(f) |
15 | | -s <- chunk.apply(cr, length, CH.MERGE=sum, CH.MAX.SIZE=1e6) |
16 | | -rm(cr) |
17 | | -close(f) |
18 | | -gc() |
| 115 | +test("chunk.tapply with matrix", res, { |
| 116 | + f <- file(fn, "rb") |
| 117 | + cr <- chunk.reader(f, sep="\t") |
| 118 | + .timing <<- system.time( |
| 119 | + s <- chunk.tapply(cr, function(r) { |
| 120 | + d <- dstrsplit(r, list(A="",B=1,C=1), "\t") |
| 121 | + m <- ctapply(as.matrix(d[,c("B","C")]), d$A, colSums, MERGE=rbind) |
| 122 | + }, CH.MERGE=rbind, CH.MAX.SIZE=.max.ch) |
| 123 | + ) |
| 124 | + rm(cr) |
| 125 | + close(f) |
| 126 | + gc() |
| 127 | + colnames(s) <- c("B","C") |
| 128 | + t(s) |
| 129 | +}) |
19 | 130 |
|
20 | | -expect_equal(s, file.info(fn)$size) |
21 | 131 |
|
22 | | -f <- file(fn, "rb") |
23 | | -cr <- chunk.reader(f) |
24 | | -s <- chunk.apply(cr, length, CH.MERGE=sum, CH.MAX.SIZE=1e6, CH.PARALLEL=4) |
25 | | -rm(cr) |
26 | | -close(f) |
27 | | -gc() |
28 | 132 |
|
29 | | -expect_equal(s, file.info(fn)$size) |
| 133 | +test("chunk.apply with keys + slow matrix ctapply, parallel", res, { |
| 134 | + f <- file(fn, "rb") |
| 135 | + cr <- chunk.reader(f, sep="\t") |
| 136 | + .timing <<- system.time( |
| 137 | + s <- chunk.apply(cr, function(r) { |
| 138 | + d <- dstrsplit(r, list(A="",B=1,C=1), "\t") |
| 139 | + m <- ctapply(as.matrix(d[,c("B","C")]), d$A, function(m) apply(m, 2, sum), MERGE=rbind) |
| 140 | + }, CH.MERGE=rbind, CH.MAX.SIZE=.max.ch, CH.PARALLEL=4) |
| 141 | + ) |
| 142 | + rm(cr) |
| 143 | + close(f) |
| 144 | + gc() |
| 145 | + colnames(s) <- c("B","C") |
| 146 | + t(s) |
| 147 | +}) |
30 | 148 |
|
| 149 | +unlink(fn) |
0 commit comments