Showing posts with label Javascript. Show all posts
Showing posts with label Javascript. Show all posts

2019/05/08

How javascript prefers a "while loop" to a beautiful recursive algorithm





As it's explained here, most browsers will barf on "too much recursion". Here's an entry in the V8 bug tracker that is interesting reading.

If it's simple self-recursion, it's probably better to use explicit iteration rather than hoping for tail-call elimination.

Even if i prefer definitely the beauty of recursion :)

2018/11/21

How i discovered Nuxeo Docker Scaling


ECM is dead - Let me introduce you to this really good Content Service Platform: Nuxeo.

What i first noticed in Nuxeo Platform is its Cloud Ready architecture. It comes ready to use on AWS and it scales like described on the schema bellow.

To me, scalability means :

  1. no bottleneck
  2. modularity and extensibility

So far, i've experimented and checked :

1. DB agnostic and how simple it is to change from Postgres (SQL) to MongoDB (No SQL) for exemple. Brilliant ! My docker exemple is below with Mongo, but i also tested with Postgres and it really is as simple as edit a nuxeo.conf file. As DevOps, i can imagine it could should be a very useful pattern;
Modularity + 1

2. I started with the official Nuxeo Docker image. Then using docker-compose, i easily split my Nuxeo Project in one Nuxeo Server, one Elasticsearch and one Mongo. And later, it's possible to create an Elacticsearch node and clusterize Nuxeo Server too (as worker for asynchronous jobs) - again, a very useful pattern if you need to adjust your search or frontend performances; 
Modularity + 1

3. In general, performance and load distribution is difficult to test and measure. Nuxeo did their own benchmarks, but i wanted to make sure that nodes and specially the Nuxeo Server is ready to handle a huge web traffic. So i built a small Node js client based on nuxeo-js-client and tried to load test my Nuxeo Dockerized Platform (Nuxeo Server + Elastic + Mongo). I used Cadvisor as a monitoring tool. Here are the result I got:

Scenario 1, In one minute, more than 3 000 requests (read, update and create). CPU usage went up and fell down for all nodes similarly. Memory looks stable, no big peak. No real stress (10 ms request average).

windows (left to right) : Node js, Nuxeo, Elastic, Mongo.


Scenario 2, In two minutes, more than 12 000 requests (read, update and create), and then what i wanted to see: Memory following the speed-up ramp but CPU is staying moderate (compare to other scenarios) - other nodes receiving a continuous charge from Nuxeo Server Node. Is Nuxeo is dealing with memory to ramp up? yes, it allocates more memory to deal with the increase traffic, a good choice, specially in a Cloud context... but, most important here: 300 mixed reqs / sec - works like a charm! And Im pretty confident that it could handle more (regarding my config, docker, laptop etc...).

Notice : Nuxeo memory ramp-up, and what it looks like an Elasticsearch indexing in the middle - cool :)


Well done (30 ms request average) ! "No bottleneck" +1

I definitely need to go further, maybe in another post ? How and Elasticsearch is indexing all new contents ? Clustering ?...
But if you're interested in subject, the docs are here https://doc.nuxeo.com/nxdoc/nuxeo-cluster-scalability-options.

My Node Client, and Docker Gist, if you want to make your opinion :

Enjoy
@Mat

2017/06/12

How i've built a freemium angularjs mobile application in 10 minutes


As mobile & web full stack dev, i always look for the best tools to publish and maintain the apps i've built.

That’s why I contributed to github.com/miappio, which is like the graal for me.

And let me tell you how you can now use this tool to publish a freemium hybrid app in 10 minutes with this tool.

Why have I chosen this solution ?
- I love Ionic (especially their css and integration tools), but you then need to pay for a cloud solution that can't manage my users.
- I want to involve my free users (for free) and then make them pay on premium features
- miapp.io provide a generator, out of the box : angular.js, ionic, cordova mobile app
- it's free :)

That's why i've chosen this open and free platform for my first (very important) users and i know that, if needed, I can also freely manage my premium users ...

In three steps :

First : register your app



and get your app ID and salt



Second : create your app with yeoman


Requirements :
As web/mobile dev, You should be familiar with node.js, gulp, ionic etc ...

I've done a gist to help check your config https://gist.github.com/mlefree/2156f66dfb441f107bef157dde56a836

Then you're ready !

npm install -g generator-miappio

and in your new project directory

yo miappio

Just answer a few questions, all done !

ls -al




As you can see, all done and ready to use;
Want to check your tests

npm test

and start your new app in your favorite android device :

npm run android



Third - Publish

... and that's it, you’re done!
After signup, you could now see/manage your users on miapp.io app page (role etc ...)


That's it :-) ..

If you need to go further :

  1. The generator, if you don't have yest your app : https://www.npmjs.com/package/generator-miappio
  2. The SDK, if you already add your hybride app : https://github.com/miappio/miappio-sdk
  3. Some app built :

One last point, the project is open. it's evolving with many features in the backlog (ie #ContributorsWelcome?!).

Enjoy App Heroes !!
Mat


2016/06/21

#Tech : Node.js vs Java



#Node.JS #Angular.JS and / or #Spring #J2EE ... look as a familliar question for web dev & it projects now.
What is the usage ? the front, back end ? ... VS











To be honnest my preference comes defintly (after many years of using it) to javascript.

First argument to me - simplicity.

Second : everywhere .. thanks to Node.JS and ts packaging system NPM. It's so easy to deploy, to test on any OS or VM.

But Java has learn from JS !

Look at this code : Web server with Nodejs vs Spring Boot - How to create a Http server ?

Nodejs


var http = require(‘http’);
http.createServer(function (req, res) { res.writeHead(200, {‘Content-Type’: ‘text/plain’}); res.end(‘Hello World’);
}).listen(3000);

Spring Boot


@RestController
class ThisWillActuallyRun { @RequestMapping(“/”) String home() { return “Hello World!” }
}




Another argument for system or architectures should be performance.

Let's see a set of performance tests to be run against both a Java EE application and a Node.js application, both backed by the same CouchDB database.

A site i love (https://dzone.com/articles/performance-comparison-between)


J2EE


The following Java code is a servlet that fetches a document from CouchDB by id and forwards the data as a JSON object.




package com.shinetech.couchDB;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import com.fourspaces.couchdb.Database;
import com.fourspaces.couchdb.Document;
import com.fourspaces.couchdb.Session;

@SuppressWarnings("serial")
public class MyServlet extends HttpServlet {
 Logger logger = Logger.getLogger(this.getClass());
 Session s = new Session("localhost",5984);
 Database db = s.getDatabase("testdb");
 public void doGet(HttpServletRequest req,          HttpServletResponse res)
   throws IOException {
   String id = req.getPathInfo().substring(1);
   PrintWriter out = res.getWriter();
   Document doc = db.getDocument(id);
   if (doc==null){
     res.setContentType("text/plain");
     out.println("Error: no document with id " + id +" found.");
   } else {
     res.setContentType("application/json");
     out.println(doc.getJSONObject());
   }
   out.close();
  }
}

What can be seen is that the response time deteriorates as the number of concurrent requests increases. The response time was 23 ms on average at 10 concurrent requests, and 243 ms on average at 100 concurrent requests.


The interesting part is that the average response time has an almost linear correlation to the number of concurrent requests, so that a tenfold increase in concurrent requests leads to a tenfold increase in response time per request. This makes the number of requests that can be handled per second is pretty constant, regardless of whether we have 10 concurrent requests or 150 concurrent requests. At all observed concurrency level the number of requests served per second was roughly 420.

NodeJS


The Node.js application ran on Node.js 0.10.20 using the Cradle CouchDB driver version 0.57. The caching was turned off for the driver to create equal conditions.

The following shows the Node.js program that delivers the same JSON document from CouchDB for a given ID:




var http = require ('http'),
url = require('url'),
cradle = require('cradle'),
c = new(cradle.Connection)(
  '127.0.0.1',5984,{cache: false, raw: false}),
db = c.database('testdb'),
  port=8081;
  process.on('uncaughtException', function (err) {
  console.log('Caught exception: ' + err);
});
http.createServer(function(req,res) {
  var id = url.parse(req.url).pathname.substring(1);
  db.get(id,function(err, doc) {
  if (err) {
   console.log('Error'+err.message);
   res.writeHead(500,{'Content-Type': 'text/plain'});
   res.write('Error' + err.message);
   res.end();
  } else {
   res.writeHead(200,{'Content-Type': 'application/json'});
   res.write(JSON.stringify(doc));
   res.end();
  }
 });
}).listen(port);

As before the average response time has a linear correlation to the number of concurrent requests, keeping the requests that can be served per second pretty constant.

Conclusion

 Node.js is roughly 20% faster, e.g. 509 requests/second vs. 422 requests/second at ten concurrent requests.
 :)


But, again, it depends on your ecosystem, project etc ...

Tech Wind of Change ;)


2016/02/01

setTimeout() alternative as an Happy New Year wish ?




I'm late. But still want to wish you a
very Happy 2016 !

That's the perfect time to use a JS window.setTimeout() function.

Unfortunately, don't ask me why, i can't use .setTimeout on current window.

That's why i describe here a simple alternative to setTimeout() as an asynchrone XHR.

The fiddle

UPDATE :

a response based on jQuery version 3.0 .animate() here https://stackoverflow.com/questions/35133311/js-settimeout-alternative

@Mat