1/* 2 * Copyright (C) 2025 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16import { 17 fileIO, FILE_CONTENT, prepareFile, prepare200MFile, nextFileName, isIntNum, describe, it, expect, 18} from '../Common'; 19import { Level } from '@ohos/hypium'; 20 21const RAND_READ_BASELINE = 3.5; 22const RAND_WRITE_BASELINE = 0.5; 23const SEQUENCE_READ_BASELINE = 15; 24const SEQUENCE_WRITE_BASELINE = 5; 25 26export default function FileIOFilerw_test() { 27 describe('FileIO_fs_filerw_test', function () { 28 29 async function IOfunc(file, bf, total, rand, read, promise) { 30 let startTime = 0; 31 try { 32 let array = new ArrayBuffer(bf); 33 let count = total / bf; 34 startTime = new Date().getTime(); 35 for (let i = 0; i < count; i++) { 36 let offset = i * bf; 37 if (rand) { 38 offset = Math.floor(Math.random() * (total - bf)); 39 } 40 if (promise) { 41 if (read) { 42 fileIO.read(file.fd, array, { length: bf, offset: offset }).then((readLen) => { 43 }).catch((e) => { 44 console.log('fileIO_test_read_promise has failed for ' + e.message + ', code: ' + e.code); 45 return -1; 46 }); 47 } else { 48 fileIO.write(file.fd, array, { length: bf, offset: offset, encoding: 'utf-8' }).then((writeLen) => { 49 }).catch((e) => { 50 console.log('fileIO_test_write_promise has failed for ' + e.message + ', code: ' + e.code); 51 return -1; 52 }); 53 } 54 } else { 55 if (read) { 56 fileIO.read(file.fd, array, { length: bf, offset: offset }, (err, readLen) => { 57 if (err) { 58 console.log('fileIO_test_read_callback has failed for ' + err.message + ', code: ' + err.code); 59 return -1; 60 } 61 }); 62 } else { 63 fileIO.write(file.fd, array, { length: bf, offset: offset, encoding: 'utf-8' }, (err, writeLen) => { 64 if (err) { 65 console.log('fileIO_test_write_callback has failed for ' + err.message + ', code: ' + err.code); 66 return -1; 67 } 68 }); 69 } 70 } 71 } 72 fileIO.fsyncSync(file.fd); 73 } catch (e) { 74 console.log('FileIO_fs_filerw_test has failed for ' + e.message + ', code: ' + e.code); 75 } finally { 76 let endTime = new Date().getTime(); 77 console.log("FileIO_fs_filerw_test finally" + JSON.stringify(endTime)) 78 var interval = endTime - startTime; 79 var average = total * 1000 / interval; 80 var averagerate = average / 0x100000; 81 return averagerate; 82 } 83 } 84 85 /** 86 * @tc.number SUB_DF_FILEIO_FILERW_READ_RANDOM_PROMISE_0000 87 * @tc.name fileIO_test_read_random_promise_000 88 * @tc.desc Test the read() interface. promise. 89 * Open the file, file read() in the random way. 90 * @tc.size MEDIUM 91 * @tc.type Function 92 * @tc.level Level 3 93 * @tc.require 94 */ 95 it('fileIO_test_read_random_promise_000', Level.LEVEL0, async function (done) { 96 let fpath = await nextFileName('fileIO_test_read_random_promise_000'); 97 expect(prepare200MFile(fpath)).assertTrue; 98 99 try { 100 let file = fileIO.openSync(fpath, fileIO.OpenMode.READ_ONLY); 101 expect(isIntNum(file.fd)).assertTrue(); 102 let averagerate = await IOfunc(file, 0x1400, 0x1400000, true, true, true); 103 console.log("RAND_READ Rate is" + averagerate + "M/s"); 104 expect(averagerate > RAND_READ_BASELINE).assertTrue(); 105 fileIO.closeSync(file); 106 fileIO.unlinkSync(fpath); 107 done(); 108 } catch (e) { 109 console.log('fileIO_test_read_random_promise_000 has failed for ' + e.message + ', code: ' + e.code); 110 expect(false).assertTrue(); 111 } 112 }); 113 114 /** 115 * @tc.number SUB_DF_FILEIO_FILERW_WRITE_RANDOM_PROMISE_0000 116 * @tc.name fileIO_test_write_random_promise_000 117 * @tc.desc Test the write() interface. promise. 118 * Open the file, file write() in the random way. 119 * @tc.size MEDIUM 120 * @tc.type Function 121 * @tc.level Level 3 122 * @tc.require 123 */ 124 it('fileIO_test_write_random_promise_000', Level.LEVEL0, async function (done) { 125 let fpath = await nextFileName('fileIO_test_write_random_promise_000'); 126 expect(prepare200MFile(fpath)).assertTrue; 127 128 try { 129 let file = fileIO.openSync(fpath, fileIO.OpenMode.READ_WRITE); 130 expect(isIntNum(file.fd)).assertTrue(); 131 let averagerate = await IOfunc(file, 0x1400, 0x1400000, true, false, true); 132 console.log("RAND_WRITE Rate is" + averagerate + "M/s"); 133 expect(averagerate > RAND_WRITE_BASELINE).assertTrue(); 134 fileIO.closeSync(file); 135 fileIO.unlinkSync(fpath); 136 done(); 137 } catch (e) { 138 console.log('fileIO_test_write_random_promise_000 has failed for ' + e.message + ', code: ' + e.code); 139 expect(false).assertTrue(); 140 } 141 }); 142 143 /** 144 * @tc.number SUB_DF_FILEIO_FILERW_READ_SEQUENCE_PROMISE_0000 145 * @tc.name fileIO_test_read_sequence_promise_000 146 * @tc.desc Test the read() interface. promise. 147 * Open the file, file read() in the sequence way. 148 * @tc.size MEDIUM 149 * @tc.type Function 150 * @tc.level Level 3 151 * @tc.require 152 */ 153 it('fileIO_test_read_sequence_promise_000', Level.LEVEL0, async function (done) { 154 let fpath = await nextFileName('fileIO_test_read_sequence_promise_000'); 155 expect(prepare200MFile(fpath)).assertTrue; 156 157 try { 158 let file = fileIO.openSync(fpath, fileIO.OpenMode.READ_ONLY); 159 expect(isIntNum(file.fd)).assertTrue(); 160 let averagerate = await IOfunc(file, 0x1400, 0x1400000, false, true, true); 161 console.log("SEQUENCE_READ Rate is" + averagerate + "M/s"); 162 expect(averagerate > SEQUENCE_READ_BASELINE).assertTrue(); 163 fileIO.closeSync(file); 164 fileIO.unlinkSync(fpath); 165 done(); 166 } catch (e) { 167 console.log('fileIO_test_read_sequence_promise_000 has failed for ' + e.message + ', code: ' + e.code); 168 expect(false).assertTrue(); 169 } 170 }); 171 172 /** 173 * @tc.number SUB_DF_FILEIO_FILERW_WRITE_SEQUENCE_PROMISE_0000 174 * @tc.name fileIO_test_write_sequence_promise_000 175 * @tc.desc Test the write() interface. promise. 176 * Open the file, file write() in the sequence way. 177 * @tc.size MEDIUM 178 * @tc.type Function 179 * @tc.level Level 3 180 * @tc.require 181 */ 182 it('fileIO_test_write_sequence_promise_000', Level.LEVEL0, async function (done) { 183 let fpath = await nextFileName('fileIO_test_write_sequence_promise_000'); 184 expect(prepare200MFile(fpath)).assertTrue; 185 186 try { 187 let file = fileIO.openSync(fpath, fileIO.OpenMode.READ_WRITE); 188 expect(isIntNum(file.fd)).assertTrue(); 189 let averagerate = await IOfunc(file, 0x1400, 0x1400000, false, false, true); 190 console.log("SEQUENCE_WRITE Rate is" + averagerate + "M/s"); 191 expect(averagerate > SEQUENCE_WRITE_BASELINE).assertTrue(); 192 fileIO.closeSync(file); 193 fileIO.unlinkSync(fpath); 194 done(); 195 } catch (e) { 196 console.log('fileIO_test_write_sequence_promise_000 has failed for ' + e.message + ', code: ' + e.code); 197 expect(false).assertTrue(); 198 } 199 }); 200 201 /** 202 * @tc.number SUB_DF_FILEIO_FILERW_READ_RANDOM_CALLBACK_0000 203 * @tc.name fileIO_test_read_random_callback_000 204 * @tc.desc Test the read() interface. callback. 205 * Open the file, file read() in the random way. 206 * @tc.size MEDIUM 207 * @tc.type Function 208 * @tc.level Level 3 209 * @tc.require 210 */ 211 it('fileIO_test_read_random_callback_000', Level.LEVEL0, async function (done) { 212 let fpath = await nextFileName('fileIO_test_read_random_callback_000'); 213 expect(prepare200MFile(fpath)).assertTrue; 214 215 try { 216 let file = fileIO.openSync(fpath, fileIO.OpenMode.READ_ONLY); 217 expect(isIntNum(file.fd)).assertTrue(); 218 let averagerate = await IOfunc(file, 0x1400, 0x1400000, true, true, false); 219 console.log("RAND_READ Rate is" + averagerate + "M/s"); 220 expect(averagerate > RAND_READ_BASELINE).assertTrue(); 221 fileIO.closeSync(file); 222 fileIO.unlinkSync(fpath); 223 done(); 224 } catch (e) { 225 console.log('fileIO_test_read_random_callback_000 has failed for ' + e.message + ', code: ' + e.code); 226 expect(false).assertTrue(); 227 } 228 }); 229 230 /** 231 * @tc.number SUB_DF_FILEIO_FILERW_WRITE_RANDOM_CALLBACK_0000 232 * @tc.name fileIO_test_write_random_callback_000 233 * @tc.desc Test the write() interface. callback. 234 * Open the file, file write() in the random way. 235 * @tc.size MEDIUM 236 * @tc.type Function 237 * @tc.level Level 3 238 * @tc.require 239 */ 240 it('fileIO_test_write_random_callback_000', Level.LEVEL0, async function (done) { 241 let fpath = await nextFileName('fileIO_test_write_random_callback_000'); 242 expect(prepare200MFile(fpath)).assertTrue; 243 244 try { 245 let file = fileIO.openSync(fpath, fileIO.OpenMode.READ_WRITE); 246 expect(isIntNum(file.fd)).assertTrue(); 247 let averagerate = await IOfunc(file, 0x1400, 0x1400000, true, false, false); 248 console.log("RAND_WRITE Rate is" + averagerate + "M/s"); 249 expect(averagerate > RAND_WRITE_BASELINE).assertTrue(); 250 fileIO.closeSync(file); 251 fileIO.unlinkSync(fpath); 252 done(); 253 } catch (e) { 254 console.log('fileIO_test_write_random_callback_000 has failed for ' + e.message + ', code: ' + e.code); 255 expect(false).assertTrue(); 256 } 257 }); 258 259 /** 260 * @tc.number SUB_DF_FILEIO_FILERW_READ_SEQUENCE_CALLBACK_0000 261 * @tc.name fileIO_test_read_sequence_callback_000 262 * @tc.desc Test the read() interface. callback. 263 * Open the file, file read() in the sequence way. 264 * @tc.size MEDIUM 265 * @tc.type Function 266 * @tc.level Level 3 267 * @tc.require 268 */ 269 it('fileIO_test_read_sequence_callback_000', Level.LEVEL0, async function (done) { 270 let fpath = await nextFileName('fileIO_test_read_sequence_callback_000'); 271 expect(prepare200MFile(fpath)).assertTrue; 272 273 try { 274 let file = fileIO.openSync(fpath, fileIO.OpenMode.READ_ONLY); 275 expect(isIntNum(file.fd)).assertTrue(); 276 let averagerate = await IOfunc(file, 0x1400, 0x1400000, false, true, false); 277 console.log("SEQUENCE_READ Rate is" + averagerate + "M/s"); 278 expect(averagerate > SEQUENCE_READ_BASELINE).assertTrue(); 279 fileIO.closeSync(file); 280 fileIO.unlinkSync(fpath); 281 done(); 282 } catch (e) { 283 console.log('fileIO_test_read_sequence_callback_000 has failed for ' + e.message + ', code: ' + e.code); 284 expect(false).assertTrue(); 285 } 286 }); 287 288 /** 289 * @tc.number SUB_DF_FILEIO_FILERW_WRITE_SEQUENCE_CALLBACK_0000 290 * @tc.name fileIO_test_write_sequence_callback_000 291 * @tc.desc Test the write() interface. callback. 292 * Open the file, file write() in the sequence way. 293 * @tc.size MEDIUM 294 * @tc.type Function 295 * @tc.level Level 3 296 * @tc.require 297 */ 298 it('fileIO_test_write_sequence_callback_000', Level.LEVEL0, async function (done) { 299 let fpath = await nextFileName('fileIO_test_write_sequence_callback_000'); 300 expect(prepare200MFile(fpath)).assertTrue; 301 302 try { 303 let file = fileIO.openSync(fpath, fileIO.OpenMode.READ_WRITE); 304 expect(isIntNum(file.fd)).assertTrue(); 305 let averagerate = await IOfunc(file, 0x1400, 0x1400000, false, false, false); 306 console.log("SEQUENCE_WRITE Rate is" + averagerate + "M/s"); 307 expect(averagerate > SEQUENCE_WRITE_BASELINE).assertTrue(); 308 fileIO.closeSync(file); 309 fileIO.unlinkSync(fpath); 310 done(); 311 } catch (e) { 312 console.log('fileIO_test_write_sequence_callback_000 has failed for ' + e.message + ', code: ' + e.code); 313 expect(false).assertTrue(); 314 } 315 }); 316 }); 317}