Skip to content

Instantly share code, notes, and snippets.

View GaetanoPiazzolla's full-sized avatar
☠️
Sleep is for those without deadlines

Gaetano Piazzolla GaetanoPiazzolla

☠️
Sleep is for those without deadlines
View GitHub Profile
app.get('/cpu-intensive/:type/:num/:threads', (req,res) => {
console.log('Type: ',req.params.type)
console.log('Num:' ,req.params.num)
console.log('Threads:' ,req.params.threads)
// switch method to run based on TYPE
// increasing NUM means increasing CPU-operations
// THREADS are the threads to use when using arrays of workers
//worker.js
const {parentPort} = require("worker_threads");
const calculus = require('./calculus')
parentPort.on("message", data => {
parentPort.postMessage({input: data.num, output: calculus.execute(data.num)});
});
const worker_calculus = (num) => {
return new Promise((resolve, reject) => {
let numberOfThreads = 10;
const workerArray = [];
for (let n = 0; n < numberOfThreads; n++) {
workerArray.push(new Worker("./project_modules/worker.js"))
}
let i = 0;
const worker_array_calculus = (num,threads) => {
return new Promise((resolve, reject) => {
let workerNum = i % threads;
@GaetanoPiazzolla
GaetanoPiazzolla / search-full.js
Last active April 22, 2021 07:01
Search string in file NodeJS - FULL file in memory
const fs = require('fs');
const searchFull = (filename, text) => {
return new Promise((resolve) => {
const regEx = new RegExp(text, "i")
const result = [];
fs.readFile('file/' + filename + ".txt", 'utf8', function (err, contents) {
@GaetanoPiazzolla
GaetanoPiazzolla / search-stream.js
Last active October 30, 2021 17:01
Search string in file NodeJS - using streams
const fs = require('fs');
const readline = require('readline');
const stream = require('stream');
const searchStream = (filename, text) => {
return new Promise((resolve) => {
const inStream = fs.createReadStream('file/' + filename + ".txt");
const outStream = new stream;
const rl = readline.createInterface(inStream, outStream);
@GaetanoPiazzolla
GaetanoPiazzolla / search-os.js
Created April 21, 2021 15:44
Search string in file NodeJS - FULL file in memory
const osType = require('os').type();
const {spawn} = require('child_process');
const searchOs = (filename, text) => {
return new Promise((resolve) => {
if (osType === 'Windows_NT') {
console.log('Searching using WINDOWS FIDSTR');
let child = spawn('findstr', [
import com.fasterxml.jackson.databind.ObjectMapper;
String content = "{\"params\":{\"preference\":1620226203317,\"index\":\"filebeat*\",\"body\":{\"version\":true,\"size\":2000,\"sort\":[{\"@timestamp\":{\"order\":\"desc\",\"unmapped_type\":\"boolean\"}}],\"aggs\":{\"2\":{\"date_histogram\":{\"field\":\"@timestamp\",\"fixed_interval\":\"3h\",\"time_zone\":\"Europe/Rome\",\"min_doc_count\":1}}},\"stored_fields\":[\"*\"],\"script_fields\":{},\"docvalue_fields\":[{\"field\":\"@timestamp\",\"format\":\"date_time\"},{\"field\":\"azure.enqueued_time\",\"format\":\"date_time\"},{\"field\":\"event.created\",\"format\":\"date_time\"},{\"field\":\"event.end\",\"format\":\"date_time\"},{\"field\":\"event.ingested\",\"format\":\"date_time\"},{\"field\":\"event.start\",\"format\":\"date_time\"},{\"field\":\"file.accessed\",\"format\":\"date_time\"},{\"field\":\"file.created\",\"format\":\"date_time\"},{\"field\":\"file.ctime\",\"format\":\"date_time\"},{\"field\":\"file.mtime\",\"format\":\"date_time\"},{\"field\":\"kafk
List<String> bigList = Arrays.asList("1","2"....."6000")
for (int i = 0; i < bigList.size(); i++) {
String tokenToSearch = bigList.get(i);
if (i % 100 == 0) {
if (i != 0) {
bufferedWriter.close();
}
@GaetanoPiazzolla
GaetanoPiazzolla / client-direct.js
Last active May 17, 2021 14:47
Central part of Client of DirectCode
import React from "react";
import socketIOClient from "socket.io-client";
import {generateUniqueID} from "web-vitals/dist/lib/generateUniqueID";
import debounce from 'lodash.debounce';
import throttle from 'lodash.throttle';
class App extends React.Component {
@GaetanoPiazzolla
GaetanoPiazzolla / direct-server.js
Created May 16, 2021 21:15
Server core implementation of direct-code
const express = require("express");
const http = require("http");
const socketIo = require("socket.io");
const cors = require('cors')
const bodyParser = require('body-parser');
const port = process.env.PORT || 4001;
const app = express();
app.use(cors());