Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Slow performance on writing multiple objects at a time #2989

Closed
andrei-zgirvaci opened this issue Jun 19, 2020 · 9 comments
Closed

Slow performance on writing multiple objects at a time #2989

andrei-zgirvaci opened this issue Jun 19, 2020 · 9 comments

Comments

@andrei-zgirvaci
Copy link

andrei-zgirvaci commented Jun 19, 2020

j                                                                 Hello, I am using Realm in a React Native project and I am experiencing some performance issues. Maybe this is normal, but it's too slow for me. Or maybe my implementation is wrong...

Goals

I want to write 600 elements into a realm database at a very fast speed. For the sake of the test I am writing just 50 elements, but it's still very slow and writing 600 elements into realm will block JS thread for too long.

Expected Results

I expect that writing 50 elements into Realm to be faster than writing into AsyncStorage. Preferably to take no more than 1-50 ms.

Actual Results

Realm Benchmark

AsyncStorage Benchmark

Code Sample

/* Realm - Implementation */

const bench = Reactotron.benchmark!('Realm - addPeaks');

const realm = await Realm.open(this.realmConfig);

bench.step('open db');

realm.write(() => {
peaks.forEach((peak, index) => {
  realm.create('Peak', {
    index: this.blockIndex + index,
    amplitude: peak[0],
    IMU: {
      x: peak[1],
      y: peak[2],
      z: peak[3],
    },
    peakDetected: peak[4],
  });
});
});

bench.step('write in db');

this.blockIndex += peaks.length;

realm.close();

bench.stop('close db');
/* AsyncStorage - Implementation */

const bench = Reactotron.benchmark!('AsyncStorage - addPeaks');

const peakObjects = peaks.map((peak, index) => ({
  index: this.blockIndex + index,
  amplitude: peak[0],
  IMU: {
    x: peak[1],
    y: peak[2],
    z: peak[3],
  },
  peakDetected: peak[4],
}));

bench.step('create objects');

const jsonPeaks = JSON.stringify(peakObjects);

bench.step('stringify objects');

await AsyncStorage.setItem(
  `@sessionPeaks_${this.blockIndex}`,
  JSON.stringify(jsonPeaks)
);

bench.stop('write in AS');

this.blockIndex += peaks.length;

Version of Realm and Tooling

  • Realm JS SDK Version: 6.0.2
  • Node or React Native: React Native - 0.62.2
  • Client OS & Version: iPhone 6s - iOS 13.5.1 (development environment)
  • Which debugger for React Native: Reactotron
@vazra
Copy link

vazra commented Jun 19, 2020

there is some known performance issues while in the debug mode , refer to #491 & https://github.com/realm/realm-js#issues-with-debugging for more details. @MD3XTER did you check the same in the production mode? does that have the same performance problem?

for benchmarking multiple writes, have a look at the sample given by @bmunkholm at #2154 (comment) ,

const Realm = require('realm')

Realm.open({
    schema: [{
        name: 'Page',
        properties: {
            id: 'int'
        }
    }]
}).then(realm => {
    console.time('nothing')
    console.timeEnd('nothing')

    console.time('warmup')
    realm.write(() => {
        for (let i = 0; i < 10; i++) {
            realm.create('Page', {id: 5})
        }
    })
    console.timeEnd('warmup')

    for (let objects=0; objects<=1000; objects+=100) {
        let test = 'Objects ' + objects
        console.time(test)
        realm.write(() => {
            for (let i = 0; i < 100; i++) {
                realm.create('Page', {id: 5})
            }
        });
        console.timeEnd(test)
    }

    process.exit()
});

@andrei-zgirvaci
Copy link
Author

andrei-zgirvaci commented Jun 20, 2020

@vazra thank you for your quick reply. I am not using a debugger. I use Reactotron which could make realm-js slow. I have tested the app in production mode. How can I do this benchmark test in production mode? The code that you have attached above is using console.time function. How can I see this in production?

@vazra
Copy link

vazra commented Jun 20, 2020

@MD3XTER try logging to console w/o Reactotron, and check if it's different. By production I mean, not in the development mode, I don't think there's something blocking you from printing to console in non-dev mode. can you clarify?

@jimsideout
Copy link

jimsideout commented Jun 22, 2020

@vazra Same issue. On app install, I load a significant amount of data from a static json file into the realm database. Using version 4.0.0-beta.0 this data loads in a few seconds, using 6.0.0 the same load can take up to 10 minutes. Tests are run in production.

Within a single write, I loop through the json file and load the database.

@vazra
Copy link

vazra commented Jun 22, 2020

@jimsideout It shouldn't be that slow, what kind of data are you inserting? can you update with a reproducible sample. I ran the above code in realm 5 and 6 , the results are

With Realm 5.0.5 (with Node 12 , MacBook Pro)

nothing: 0.107ms
warmup: 1.972ms
Objects 0: 1.884ms
Objects 100: 1.636ms
Objects 200: 1.783ms
Objects 300: 1.879ms
Objects 400: 1.543ms
Objects 500: 1.621ms
Objects 600: 1.634ms
Objects 700: 1.648ms
Objects 800: 1.792ms
Objects 900: 1.773ms
Objects 1000: 1.611ms

With Realm 6.0.2 (with Node 12 , MacBook Pro)

nothing: 0.107ms
warmup: 1.992ms
Objects 0: 1.588ms
Objects 100: 1.511ms
Objects 200: 1.554ms
Objects 300: 1.767ms
Objects 400: 1.554ms
Objects 500: 1.467ms
Objects 600: 1.457ms
Objects 700: 1.495ms
Objects 800: 1.706ms
Objects 900: 1.498ms
Objects 1000: 1.421ms

@vazra
Copy link

vazra commented Jun 22, 2020

@jimsideout @MD3XTER In case, if you are working with electron, have a look at the Realm part of Electron React NativeDBs demo available at https://github.com/vazra/electron-react-ts-rxdb-realm-sqlite , in which you can add as much as dummy data and check the performance.

@jimsideout
Copy link

@vazra looks like the same issue as https://github.com/realm/realm-js/issues/2845

The problem is when creating an object with nested objects. My data is just JSON data that is being looped through. Nothing special, just objects containing about 40 values of strings, ints, bools, and 2 references to other objects.

@bmunkholm
Copy link
Contributor

bmunkholm commented Jun 24, 2020

Ok. Looks like your issue is indeed what's listed in #2845. @MD3XTER can you confirm that assumption?

@andrei-zgirvaci
Copy link
Author

@bmunkholm yes, I would say so. Let's close this issue and track #2845 instead. Thank you all for your help, hopefully, we can find a solution for this!

@RealmBot RealmBot closed this as completed Jul 3, 2020
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 15, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants